pabloliva87

Ej6C++

May 3rd, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. struct date
  10. {
  11.     unsigned int day;
  12.     unsigned int month;
  13.     unsigned int year;
  14.    
  15.     bool month_day_match (const date& d1)const
  16.     {
  17.         return ((d1.month == month) && (d1.day == day));
  18.     };
  19. };
  20.  
  21. struct friend_pair
  22. {
  23.     date birthdate;
  24.     string name;
  25.    
  26.     void print_friend_pair ()
  27.     {
  28.         cout << name << ": " << birthdate.day << "/" << birthdate.month << "/" << birthdate.year << endl;
  29.     };
  30.     bool operator < (const friend_pair & second)const   /* We overload the < operator for later use */
  31.     {                           /* Orders two dates without paying attention to the year */
  32.  
  33.         bool result;
  34.  
  35.         if (birthdate.month < second.birthdate.month)
  36.         {
  37.             result = true;
  38.         } else if (birthdate.month > second.birthdate.month) {
  39.             result = false;
  40.         } else if (birthdate.day <= second.birthdate.day) {
  41.             result = true;
  42.         } else {
  43.             result = false;
  44.         }
  45.         return result;
  46.     };
  47. };
  48.  
  49. class FriendsGroup
  50. {
  51.     private:
  52.     vector<friend_pair> friend_list;
  53.  
  54.     public:
  55.     void add_friend (date birthdate, string friend_name)
  56.     {
  57.         friend_pair new_friend;
  58.         new_friend.birthdate = birthdate;
  59.         new_friend.name = friend_name;
  60.        
  61.         friend_list.push_back (new_friend);
  62.     };
  63.     void get_new_friend ()
  64.     {
  65.         string friend_name;
  66.         date birthdate;
  67.        
  68.         cout << "Enter the Name \n";
  69.         cin >> friend_name;
  70.        
  71.         cout << "Enter the Date, separated by spaces (dd mm yyyy) \n";
  72.         cin >> birthdate.day >> birthdate.month >> birthdate.year;
  73.        
  74.         while (birthdate.day > 31 || birthdate.month > 12)
  75.         {
  76.             cout << "Please input the date again, separated by spaces (dd mm yyyy) \n";
  77.             cin >> birthdate.day >> birthdate.month >> birthdate.year;
  78.         }
  79.         add_friend (birthdate, friend_name);
  80.     };
  81.     void show ()
  82.     {
  83.         int i;
  84.         int printing;
  85.         friend_pair current;
  86.         friend_pair next;
  87.  
  88.         if (friend_list.size() == 0)            /* If there are no friends to show, do nothing */
  89.         {
  90.             return;
  91.         }
  92.  
  93.         std::sort (friend_list.begin(), friend_list.end() );
  94.  
  95.         printing = 0;
  96.         current = friend_list[0];
  97.         for (i=1; i<friend_list.size(); i++)
  98.         {
  99.             next = friend_list[i];
  100.             if (current.birthdate.month_day_match(next.birthdate) )
  101.             {
  102.                 current.print_friend_pair ();
  103.                 printing = 1;
  104.                 current = next;
  105.             } else {
  106.                 if (printing == 1)
  107.                 {
  108.                     current.print_friend_pair ();
  109.                     current = next;
  110.                     printing = 0;
  111.                 } else {
  112.                     current = next;
  113.                 }
  114.             }
  115.         }
  116.         if (printing == 1)
  117.         {
  118.             current.print_friend_pair ();
  119.         }
  120.     };
  121.     FriendsGroup()
  122.     {
  123.         friend_list.resize (0);
  124.     };
  125. };
  126.  
  127. int main (void)
  128. {
  129.     FriendsGroup people;
  130.     string status;
  131.     string input;
  132.    
  133.     status = "Receiving";
  134.    
  135.     while (status == "Receiving")
  136.     {
  137.         cout << "Choose your action: Add, Show, Exit \n";
  138.         cin >> input;
  139.        
  140.         if (input == "Exit")
  141.         {
  142.             status = "Exit";
  143.         } else if (input == "Show") {
  144.             people.show();
  145.         } else if (input == "Add") {
  146.             people.get_new_friend ();
  147.         } else {
  148.             cout << "That is not a valid input \n";
  149.         }
  150.     }
  151.  
  152.     return 0;
  153. }
Add Comment
Please, Sign In to add comment