Project 1 || Wong Edition || C++ Project || B.Tech Diaries

Project #1 - Sorted Date List

  • Read a sequence of dates from file input.
  • Permissible formats for dates include
    dd/mm/yyyy e.g. 01/01/1970
    
    d MMM yyyy e.g. 1 JAN 1970
    
    MMM d, 1970 e.g. JAN 1, 1970
    
    MMMM d, yyyy e.g. January 1, 1970.
    
  • Sort and output the dates such that the later dates come first.
  • Use functions from the STL's: `<string>`, `<algorithm>`, `<regex>`, `<iterator>`, `<array>`.
  • Create your customised MyDate class.

Solution:


#include <iostream>
#include <string>
#include <algorithm>
#include <regex>
#include <iterator>
#include <array>
#include <fstream>
#include <iomanip>

#define N 25

class Date
{
private:
    int day;
    int month;
    int year;
    std::array<std::string, 12> _months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

public:
    Date()
    {
        day = 9999;
        month = 9999;
        year = 9999;
    }
    Date(std::string str_date)
    {
        day = 1;
        month = 1;
        year = 1970;
        _date_parser(str_date);
    }
    ~Date() {}
    bool is_valid()
    {
        if (day > 31 || day < 1)
            return false;
        if (month > 12 || month < 1)
            return false;
        if (year > 9999 || year < 1000)
            return false;
        return true;
    }
    void _date_parser(std::string str_date)
    {
        std::string _months_abbr_pattern = "", _months_pattern = "";
        for (size_t i = 0; i < _months.size(); i++)
        {
            _months_abbr_pattern += _months[i].substr(0, 3);
            if (_months.size() - 1 != i)
            {
                _months_abbr_pattern += "|";
            }
        }
        for (size_t i = 0; i < _months.size(); i++)
        {
            _months_pattern += _months[i];
            if (_months.size() - 1 != i)
            {
                _months_pattern += "|";
            }
        }

        std::array<std::regex, 10> patterns({
            std::regex("([0-2][1-9]|3[0-1])\\s*[, /\\-]\\s*(0[1-9]|1[0-2])\\s*[, /\\-]\\s*([1-5]\\d{3})"),
            std::regex("([1-9]|[1-2][1-9]|3[0-1])\\s*[, /\\-]\\s*(" + _months_abbr_pattern + ")\\s*[, /\\-]\\s*([1-5]\\d{3})", std::regex::icase),
            std::regex("(" + _months_abbr_pattern + ")\\s*[, /\\-]\\s*([1-9]|[1-2][1-9]|3[0-1])\\s*[, /\\-]\\s*([1-5]\\d{3})", std::regex::icase),
            std::regex("(" + _months_pattern + ")\\s*[, /\\-]\\s*([1-9]|[1-2][1-9]|3[0-1])\\s*[, /\\-]\\s*([1-5]\\d{3})", std::regex::icase),
        });

        std::smatch match;
        bool check = false;
        for (size_t i = 0; i < patterns.size(); i++)
        {
            if (std::regex_match(str_date, patterns[i]))
            {
                std::regex_search(str_date, match, patterns[i]);
                switch (i)
                {
                case 0:
                    day = std::stoi(match[1].str());
                    month = std::stoi(match[2].str());
                    year = std::stoi(match[3]);
                    break;
                case 1:
                    day = std::stoi(match[1].str());
                    month = _months.size();
                    for (size_t i = 0; i < _months.size(); i++)
                    {
                        if (_months[i].substr(0, 3) == match[2].str())
                        {
                            month = i + 1;
                            break;
                        }
                    }
                    year = std::stoi(match[3]);
                    break;
                case 2:
                    day = std::stoi(match[2].str());
                    month = _months.size();
                    for (size_t i = 0; i < _months.size(); i++)
                    {
                        if (_months[i].substr(0, 3) == match[1].str())
                        {
                            month = i + 1;
                            break;
                        }
                    }
                    year = std::stoi(match[3]);
                    break;
                case 3:
                    day = std::stoi(match[2].str());
                    month = _months.size();
                    for (size_t i = 0; i < _months.size(); i++)
                    {
                        if (_months[i] == match[1].str())
                        {
                            month = i + 1;
                            break;
                        }
                    }
                    year = std::stoi(match[3]);
                    break;
                }
                check = true;
                break;
            }
        }
        if (!check)
            std::cout << "Invalid Format" << std::endl;
    }

    bool operator>(Date d)
    {
        if (year > d.year)
            return true;
        else if (year == d.year)
        {
            if (month > d.month)
                return true;
            else if (month == d.month)
            {
                if (day > d.day)
                    return true;
                else
                    return false;
            }
            else
                return false;
        }
        return false;
    }

    bool operator<(Date d)
    {
        if (year < d.year)
            return true;
        else if (year == d.year)
        {
            if (month < d.month)
                return true;
            else if (month == d.month)
            {
                if (day < d.day)
                    return true;
                else
                    return false;
            }
            else
                return false;
        }
        return false;
    }

    std::string _str()
    {
        std::string str_date = "";
        str_date += std::to_string(day) + " ";
        str_date += _months[month - 1] + " ";
        str_date += std::to_string(year);
        return str_date;
    }

    friend std::ostream &operator<<(std::ostream &os, Date &date)
    {
        os << date._str();
        return os;
    }
};

std::array<Date, N> get_dates(std::string filename)
{
    std::ifstream infile;
    std::string str_date;

    std::array<Date, N> date_array;
    std::cout << "Get Dates from " << filename << " ... " << std::endl;
    int i = 0;
    infile.open(filename);
    while (std::getline(infile, str_date))
    {
        if (str_date != "")
            date_array[i++] = Date(str_date);
    }
    infile.close();

    return date_array;
}

int main(int argc, char const *argv[])
{
    std::array<Date, N> dates = get_dates("date_list.txt");

    std::cout << "Sorting Dates ... " << std::endl;
    std::sort(dates.begin(), dates.end());

    std::array<Date, N>::iterator it;
    for (it = dates.begin(); it != dates.end(); it++)
    {
        if (it->is_valid())
            std::cout << *it << std::endl;
    }

    return 0;
}

Input (date_list.txt):


30/01/3055
30-02-4849
30 01, 3459
16/11/5509
30-06/1571
13-Aug-1768
30/ Apr/1002
2 May 2668
29 JAN, 5802
31 Jul 2224
Jan 7 3106
Apr 31 1632
Mar 6 ,3214
Feb 19, 5063
Sep 6-3652
April 30, 5889
January 13 ,4035
May 21 2671
June 30, 4785
JANUARY 4 2324

Output:


Get Dates from date_list.txt ... 
Sorting Dates ...
30 April 1002
30 June 1571
31 April 1632
13 August 1768
31 July 2224
4 December 2324
2 May 2668
21 May 2671
30 January 3055
7 January 3106
6 March 3214
30 January 3459
6 September 3652
13 January 4035
30 June 4785
30 February 4849
19 February 5063
16 November 5509
29 December 5802
30 April 5889

Share:

Programming with C++ - Day8 || Wong Edition || B.Tech Diaries

Lab #8 - Standard Template Library (STL)

Resources

Question 1 - Acrobatics with Strings

Write a program that creates 2 strings then prints out the letters they have in common.

Solution:


#include <bits/stdc++.h>

// Question 1
void commonLetters(std::string s1, std::string s2)
{
    std::string s = "";
    auto it = std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), std::back_inserter(s));
    std::cout << s << std::endl;
}

int main(int argc, char const *argv[])
{
    std::string s1 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Nec feugiat in fermentum posuere urna. Nunc sed blandit libero volutpat sed cras ornare. Commodo quis imperdiet massa tincidunt nunc pulvinar sapien et ligula. A scelerisque purus semper eget. Pellentesque elit ullamcorper dignissim cras tincidunt lobortis. Mattis aliquam faucibus purus in massa tempor nec feugiat nisl. Tortor id aliquet lectus proin. Nulla posuere sollicitudin aliquam ultrices sagittis. Sed adipiscing diam donec adipiscing. Bibendum enim facilisis gravida neque convallis a cras.",
                s2 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Sit amet nisl suscipit adipiscing bibendum. Sed viverra ipsum nunc aliquet bibendum enim facilisis gravida. Ipsum suspendisse ultrices gravida dictum fusce ut placerat orci nulla. Et magnis dis parturient montes. Blandit massa enim nec dui nunc mattis enim ut tellus. Tempus quam pellentesque nec nam aliquam. Aenean pharetra magna ac placerat vestibulum lectus mauris ultrices eros. Tristique nulla aliquet enim tortor at auctor urna nunc id. In fermentum et sollicitudin ac orci phasellus. Senectus et netus et malesuada fames ac.";
    commonLetters(s1, s2);
    return 0;
}

Output:


Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ut iumvvrsuuuussrvidauv

Question 2 - Sorting Vehicles

Create instances of the Vehicle class (from Lab #7) e.g. 3 BUSes, 4 CARs, 2 AUTOs, 5 SCOOTERs.

Store them in a Vector and sort them according to size.

Solution:


#include <bits/stdc++.h>

// Question 2
class Vehicle
{
private:
    std::string name;
    int legs;
    double cost;

public:
    Vehicle()
    {
    }
    Vehicle(std::string n, int l, double c)
    {
        legs = l;
        cost = c;
        name = n;
    }
    ~Vehicle()
    {
    }

    double _cost()
    {
        return cost;
    }

    bool operator>(Vehicle v)
    {
        return (legs > v.legs) || (legs == v.legs && cost > v.cost);
    }
    bool operator<(Vehicle v)
    {
        return (legs < v.legs) || (legs == v.legs && cost < v.cost);
    }

    bool operator==(Vehicle v)
    {
        return (legs == v.legs) && (cost == v.cost);
    }

    std::string to_string()
    {
        return name + "(" + std::to_string(legs) + "," + std::to_string(cost) + ")";
    }

    operator std::string() const
    {
        return name + "(" + std::to_string(legs) + "," + std::to_string(cost) + ")";
    }

    friend std::ostream &operator<<(std::ostream &out, Vehicle &v)
    {
        out << v.to_string();
        return out;
    }
};

void sortVehicles(std::vector<Vehicle> &vehicles)
{
    struct
    {
        bool operator()(Vehicle v1, Vehicle v2)
        {
            return v1._cost() > v2._cost();
        }
    } smallerSize;

    std::sort(vehicles.begin(), vehicles.end(), smallerSize);
    // return vehicles;
}

int main(int argc, char const *argv[])
{
    std::vector<Vehicle> vehicles;
    vehicles.push_back(Vehicle("car", 4, 100));
    vehicles.push_back(Vehicle("car", 4, 200));
    vehicles.push_back(Vehicle("car", 4, 300));
    vehicles.push_back(Vehicle("car", 4, 400));
    vehicles.push_back(Vehicle("bus", 4, 400));
    vehicles.push_back(Vehicle("bus", 4, 300));
    vehicles.push_back(Vehicle("bus", 4, 200));
    vehicles.push_back(Vehicle("auto", 3, 400));
    vehicles.push_back(Vehicle("auto", 3, 300));
    vehicles.push_back(Vehicle("scooter", 2, 50));
    vehicles.push_back(Vehicle("scooter", 2, 100));
    vehicles.push_back(Vehicle("scooter", 2, 120));
    vehicles.push_back(Vehicle("scooter", 2, 150));
    vehicles.push_back(Vehicle("scooter", 2, 200));
    sortVehicles(vehicles);
    for (Vehicle v : vehicles)
    {
        std::cout << v << std::endl;
    }
    return 0;
}

Output:


car(4,400.000000)
bus(4,400.000000)
auto(3,400.000000)
car(4,300.000000)
bus(4,300.000000)
auto(3,300.000000)
car(4,200.000000)
bus(4,200.000000)
scooter(2,200.000000)
scooter(2,150.000000)
scooter(2,120.000000)
car(4,100.000000)
scooter(2,100.000000)
scooter(2,50.000000)

Question 3 - Rectangles and Squares

Using the Rectangle class from Lab #3, create a Square class.

Store instances of Rectangle and Square in a single Vector.

Sort them according to their area. [Hint: use Vector<Rectangle *>]

Solution:


    #include <bits/stdc++.h>

    // Question 3
    class Rectangle
    {
    private:
        int length, breadth;
        std::string colour;
    
    public:
        Rectangle();
        Rectangle(int, int, std::string);
        ~Rectangle();
        int getArea();
        std::string print();
        int compare(Rectangle);
        int _length();
        int _breadth();
        std::string _colour();
    };
    
    int Rectangle::_length()
    {
        return length;
    }
    
    int Rectangle::_breadth()
    {
        return breadth;
    }
    
    std::string Rectangle::_colour()
    {
        return colour;
    }
    
    Rectangle::Rectangle()
    {
        length = 0;
        breadth = 0;
        colour = "";
    }
    
    Rectangle::Rectangle(int length, int breadth, std::string colour)
    {
        this->length = length;
        this->breadth = breadth;
        this->colour = colour;
    }
    
    Rectangle::~Rectangle()
    {
    }
    
    int Rectangle::getArea()
    {
        return this->length * this->breadth;
    }
    
    std::string Rectangle::print()
    {
        std::stringstream ps;
        ps << "\nLength: " << this->length
           << "\nBreadth: " << this->breadth
           << "\nColour: " << this->colour
           << "\nArea: " << this->getArea() << std::endl;
        return ps.str();
    }
    
    int Rectangle::compare(Rectangle rect)
    {
        if (this->getArea() > rect.getArea())
            return 1;
        else if (this->getArea() < rect.getArea())
            return -1;
        return 0;
    }
    
    class Square : public Rectangle
    {
    
    private:
    public:
        Square();
        Square(int, std::string);
        ~Square();
        std::string print();
    };
    
    Square::Square()
    {
    }
    Square::Square(int side, std::string color) : Rectangle(side, side, color)
    {
    }
    
    Square::~Square()
    {
    }
    
    std::string Square::print()
    {
        std::stringstream ps;
        ps << "\nSide: " << this->_length()
           << "\nColour: " << this->_colour()
           << "\nArea: " << this->getArea() << std::endl;
        return ps.str();
    }
    
    int main(int argc, char const *argv[])
    {
        std::vector<Rectangle> rectangles;
        rectangles.push_back(Square(5, "S red"));
        rectangles.push_back(Square(10, "S blue"));
        rectangles.push_back(Square(15, "S green"));
    
        rectangles.push_back(Rectangle(5, 10, "R red"));
        rectangles.push_back(Rectangle(10, 15, "R blue"));
        rectangles.push_back(Rectangle(15, 20, "R green"));
    
        std::sort(rectangles.begin(), rectangles.end(), [](Rectangle a, Rectangle b)
                  { return a.compare(b) < 0; });
    
        for (Rectangle r : rectangles)
        {
            std::cout << r.print() << std::endl;
        }
    
        return 0;
    }      

Output:


    Length: 5
    Breadth: 5
    Colour: S red
    Area: 25
    
    
    Length: 5
    Breadth: 10
    Colour: R red
    Area: 50
    
    
    Length: 10
    Breadth: 10
    Colour: S blue
    Area: 100
    
    
    Length: 10
    Breadth: 15
    Colour: R blue
    Area: 150
    
    
    Length: 15
    Breadth: 15
    Colour: S green
    Area: 225
    
    
    Length: 15
    Breadth: 20
    Colour: R green
    Area: 300

Share:

Programming with C++ - Day7 || Wong Edition || B.Tech Diaries

Lab #7 - Templates

Question 1 - Function Template

The format for function declaration is:

template <class identifier> function_declaration;

The format for function call is:

function_name <type> (parameters);
  • Write a function template getLarger that takes two parameters of any (same) type and returns the larger of the two.
  • Write a class Vehicle and provide the implementation for the ">" operator. Use the Vehicle in the getLarger function from (a). E.g. Bus > Car > Auto > Scooter

Solution:


#include <iostream>

// question 1
class Vehicle
{
private:
    std::string name;
    int legs;
    double cost;

public:
    Vehicle()
    {
    }
    Vehicle(std::string n, int l, double c)
    {
        legs = l;
        cost = c;
        name = n;
    }
    ~Vehicle()
    {
    }
    bool operator>(Vehicle v)
    {
        return (legs > v.legs) || (legs == v.legs && cost > v.cost);
    }
    bool operator<(Vehicle v)
    {
        return (legs < v.legs) || (legs == v.legs && cost < v.cost);
    }

    bool operator==(Vehicle v)
    {
        return (legs == v.legs) && (cost == v.cost);
    }

    operator std::string() const
    {
        return name + "(" + std::to_string(legs) + "," + std::to_string(cost) + ")";
    }

    std::string to_string()
    {
        return name + "(" + std::to_string(legs) + "," + std::to_string(cost) + ")";
    }

    friend std::ostream &operator<<(std::ostream &out, const Vehicle &v);
};

template <typename T>
T getLarger(T a, T b)
{
    return (a > b) ? a : b;
}

template <typename T>
T getSmaller(T a, T b)
{
    return (a < b) ? a : b;
}

int main(int argc, char const *argv[])
{
    int N = 10;

    Vehicle vehicles[N];

    for (int i = 0; i < N; i++)
    {
        int legs = rand() % 4 + 2;
        double cost = rand() % 700 + 100;
        std::string name = "Vehicle " + std::to_string(i + 1);
        vehicles[i] = Vehicle(name, legs, cost);
    }

    std::cout << std::endl
                << "========Comparison of Vehicles========"
                << std::endl;

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N - i - 1; j++)
        {
            Vehicle v1 = getLarger<Vehicle>(vehicles[j + 1], vehicles[j]);
            Vehicle v2 = getSmaller<Vehicle>(vehicles[j + 1], vehicles[j]);
            vehicles[j] = v1;
            vehicles[j + 1] = v2;
        }
    }

    for (int i = 0; i < N; i++)
    {
        std::cout << vehicles[i];
        if (i != N - 1)
            std::cout << " > ";
    }
    std::cout << std::endl
                << std::endl;

    return 0;
}


========Comparison of Vehicles========
Vehicle 1(5,286.000000) > Vehicle 8(5,226.000000) > Vehicle 4(4,592.000000) > Vehicle 7(4,559.000000) > Vehicle 6(4,427.000000) > Vehicle 3(3,635.000000) > Vehicle 5(3,621.000000) > Vehicle 2(3,315.000000) > Vehicle 9(2,626.000000) > Vehicle 10(2,536.000000)

Question 2 - Class Template

The format for function declaration is:

template <class identifier> class classname
  1. Write a class template MyArray that can store up to 10 elements of any type, with the following functions:-
    • addElement
    • sort
    • print
  2. Store Vehicle instances from Question 1 in MyArray and sort them.

Solution:


...
// question 2
template <typename T>
class MyArray
{
private:
    T *arr;
    int capacity, size;

public:
    MyArray()
    {
        capacity = 8;
        arr = new T[capacity];
        size = 0;
    }
    bool addElement(T item)
    {
        if (size == capacity)
        {
            T *temp = new T[capacity * 2];
            for (int i = 0; i < size; i++)
            {
                temp[i] = arr[i];
            }
            delete[] arr;
            arr = temp;
            capacity *= 2;
        }
        arr[size++] = item;
        return true;
    }
    void sort()
    {
        for (int i = 0; i < size; i++)
        {
            for (int j = i + 1; j < size; j++)
            {
                if (arr[j] > arr[i])
                {
                    std::swap(arr[i], arr[j]);
                }
            }
        }
    }
    T get(int index)
    {
        return arr[index];
    }
    T operator[](int index)
    {
        return arr[index];
    }
    std::string print()
    {
        std::string str = "[";
        for (int i = 0; i < size; i++)
        {
            str += arr[i].to_string();
            if (i != size - 1)
                str += ", ";
        }
        str += "]";
        return str;
    }
};

int main(int argc, char const *argv[])
{
    int N = 15;

    MyArray<Vehicle> vehicles;

    for (int i = 0; i < N; i++)
    {
        int legs = rand() % 4 + 2;
        double cost = rand() % 1000 * 0.1234 + 100;
        std::string name = "Vehicle " + std::to_string(i + 1);
        vehicles.addElement(Vehicle(name, legs, cost));
    }

    std::cout << "Vehicles before sorting" << std::endl;
    std::cout << vehicles.print()
              << std::endl
              << std::endl;

    std::cout << "Vehicles after sorting" << std::endl;

    vehicles.sort();

    return 0;
}


    Vehicles before sorting
[Vehicle 1(5,209.332400), Vehicle 2(3,212.911000), Vehicle 3(3,141.339000), Vehicle 4(4,160.712800), Vehicle 5(3,151.951400), Vehicle 6(4,103.331800), Vehicle 7(4,107.280600), Vehicle 8(5,214.268400), Vehicle 9(2,152.568400), Vehicle 10(2,190.822400), Vehicle 11(5,145.411200), Vehicle 12(5,152.938600), Vehicle 13(4,165.402000), Vehicle 14(4,115.178200), Vehicle 15(5,116.659000)]

Question 3 - Friend Function

In Question 1 and Question 2, the `printTypeString()` function in the Vehicle class was used to output Vehicle information.

Overload the cout "<<" operator to output Vehicle information. Declare the "<<" function as friend in the Vehicle class, so as to access Vehicle's private attributes:-

friend ostream& operator<<(ostream &out, const Vehicle& v)

Note: friend functions and classes should be used sparingly as they break the good idea of encapsulation.

Solution:


...
// question 3
std::ostream &operator<<(std::ostream &out, const Vehicle &v)
{
    out << v.operator std::string();
    return out;
}

int main(int argc, char const *argv[])
{
    int N = 15;

    MyArray<Vehicle> vehicles;

    for (int i = 0; i < N; i++)
    {
        int legs = rand() % 4 + 2;
        double cost = rand() % 1000 * 0.1234 + 100;
        std::string name = "Vehicle " + std::to_string(i + 1);
        vehicles.addElement(Vehicle(name, legs, cost));
    }

    std::cout << "Vehicles before sorting" << std::endl;
    std::cout << vehicles.print()
                << std::endl
                << std::endl;

    std::cout << "Vehicles after sorting" << std::endl;

    vehicles.sort();

    std::cout << vehicles.print() << std::endl;
    std::cout << std::endl;

    std::cout << vehicles[0] << std::endl;

    return 0;
}    

Output:


Vehicles before sorting
[Vehicle 1(5,209.332400), Vehicle 2(3,212.911000), Vehicle 3(3,141.339000), Vehicle 4(4,160.712800), Vehicle 5(3,151.951400), Vehicle 6(4,103.331800), Vehicle 7(4,107.280600), Vehicle 8(5,214.268400), Vehicle 9(2,152.568400), Vehicle 10(2,190.822400), Vehicle 11(5,145.411200), Vehicle 12(5,152.938600), Vehicle 13(4,165.402000), Vehicle 14(4,115.178200), Vehicle 15(5,116.659000)]

Vehicles after sorting
[Vehicle 8(5,214.268400), Vehicle 1(5,209.332400), Vehicle 12(5,152.938600), Vehicle 11(5,145.411200), Vehicle 15(5,116.659000), Vehicle 13(4,165.402000), Vehicle 4(4,160.712800), Vehicle 14(4,115.178200), Vehicle 7(4,107.280600), Vehicle 6(4,103.331800), Vehicle 2(3,212.911000), Vehicle 5(3,151.951400), Vehicle 3(3,141.339000), Vehicle 10(2,190.822400), Vehicle 9(2,152.568400)]

Vehicle 8(5,214.268400)

Share: