Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- struct date
- {
- unsigned int day;
- unsigned int month;
- unsigned int year;
- bool month_day_match (const date& d1)const
- {
- return ((d1.month == month) && (d1.day == day));
- };
- };
- struct friend_pair
- {
- date birthdate;
- string name;
- void print_friend_pair ()
- {
- cout << name << ": " << birthdate.day << "/" << birthdate.month << "/" << birthdate.year << endl;
- };
- bool operator < (const friend_pair & second)const /* We overload the < operator for later use */
- { /* Orders two dates without paying attention to the year */
- bool result;
- if (birthdate.month < second.birthdate.month)
- {
- result = true;
- } else if (birthdate.month > second.birthdate.month) {
- result = false;
- } else if (birthdate.day <= second.birthdate.day) {
- result = true;
- } else {
- result = false;
- }
- return result;
- };
- };
- class FriendsGroup
- {
- private:
- vector<friend_pair> friend_list;
- public:
- void add_friend (date birthdate, string friend_name)
- {
- friend_pair new_friend;
- new_friend.birthdate = birthdate;
- new_friend.name = friend_name;
- friend_list.push_back (new_friend);
- };
- void get_new_friend ()
- {
- string friend_name;
- date birthdate;
- cout << "Enter the Name \n";
- cin >> friend_name;
- cout << "Enter the Date, separated by spaces (dd mm yyyy) \n";
- cin >> birthdate.day >> birthdate.month >> birthdate.year;
- while (birthdate.day > 31 || birthdate.month > 12)
- {
- cout << "Please input the date again, separated by spaces (dd mm yyyy) \n";
- cin >> birthdate.day >> birthdate.month >> birthdate.year;
- }
- add_friend (birthdate, friend_name);
- };
- void show ()
- {
- int i;
- int printing;
- friend_pair current;
- friend_pair next;
- if (friend_list.size() == 0) /* If there are no friends to show, do nothing */
- {
- return;
- }
- std::sort (friend_list.begin(), friend_list.end() );
- printing = 0;
- current = friend_list[0];
- for (i=1; i<friend_list.size(); i++)
- {
- next = friend_list[i];
- if (current.birthdate.month_day_match(next.birthdate) )
- {
- current.print_friend_pair ();
- printing = 1;
- current = next;
- } else {
- if (printing == 1)
- {
- current.print_friend_pair ();
- current = next;
- printing = 0;
- } else {
- current = next;
- }
- }
- }
- if (printing == 1)
- {
- current.print_friend_pair ();
- }
- };
- FriendsGroup()
- {
- friend_list.resize (0);
- };
- };
- int main (void)
- {
- FriendsGroup people;
- string status;
- string input;
- status = "Receiving";
- while (status == "Receiving")
- {
- cout << "Choose your action: Add, Show, Exit \n";
- cin >> input;
- if (input == "Exit")
- {
- status = "Exit";
- } else if (input == "Show") {
- people.show();
- } else if (input == "Add") {
- people.get_new_friend ();
- } else {
- cout << "That is not a valid input \n";
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment