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;
- typedef struct {
- unsigned int day;
- unsigned int month;
- unsigned int year;
- } date;
- typedef struct {
- date birthdate;
- string name;
- } friend_pair;
- void print_friend_pair (friend_pair current) {
- cout << current.name << ": " << current.birthdate.day << "/" << current.birthdate.month << "/" << current.birthdate.year << endl;
- }
- bool date_order (friend_pair first, friend_pair second) { /* Orders two dates without paying attention to the year */
- date fst;
- date snd;
- bool result;
- fst = first.birthdate;
- snd = second.birthdate;
- if (fst.month < snd.month) {
- result = true;
- } else if (fst.month > snd.month) {
- result = false;
- } else if (fst.day <= snd.day) {
- result = true;
- } else {
- result = false;
- }
- return result;
- }
- class FriendsGroup {
- private:
- vector<friend_pair> friend_list;
- int friend_amount;
- bool equal_date (friend_pair first, friend_pair second) { /* Checks if the day and month of two dates coincide */
- date fst;
- date snd;
- fst = first.birthdate;
- snd = second.birthdate;
- return (fst.month == snd.month && fst.day == snd.day);
- };
- public:
- void add_friend (date birthdate, string friend_name) {
- ++ friend_amount;
- friend_list.resize (friend_amount);
- friend_pair new_friend;
- new_friend.birthdate = birthdate;
- new_friend.name = friend_name;
- friend_list[friend_amount-1] = 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 initialize () {
- friend_amount = 0;
- };
- void show () {
- int i;
- int printing;
- friend_pair current;
- friend_pair next;
- std::sort (friend_list.begin(), friend_list.end(), date_order);
- printing = 0;
- current = friend_list[0];
- for (i=1; i<friend_amount; i++) {
- next = friend_list[i];
- if (equal_date(current, next) ){
- print_friend_pair (current);
- printing = 1;
- current = next;
- } else {
- if (printing == 1) {
- print_friend_pair (current);
- current = next;
- printing = 0;
- } else {
- current = next;
- }
- }
- }
- if (printing == 1) {
- print_friend_pair (current);
- }
- };
- };
- int main (void) {
- FriendsGroup * people;
- people = new (FriendsGroup);
- string status;
- string input;
- status = "Recieving";
- while (status == "Recieving") {
- 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";
- }
- }
- delete (people);
- return 0;
- }
Add Comment
Please, Sign In to add comment