pabloliva87

Ej6C++

Mar 20th, 2012
29
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 operator == (const date& d1)const          /* We overload the == operator for later use */
  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.         date fst;
  33.         date snd;
  34.  
  35.         bool result;
  36.  
  37.         fst = birthdate;
  38.         snd = second.birthdate;
  39.  
  40.         if (fst.month < snd.month)
  41.         {
  42.             result = true;
  43.         } else if (fst.month > snd.month) {
  44.             result = false;
  45.         } else if (fst.day <= snd.day) {
  46.             result = true;
  47.         } else {
  48.             result = false;
  49.         }
  50.         return result;
  51.     };
  52. };
  53.  
  54. class FriendsGroup
  55. {
  56.     private:
  57.     vector<friend_pair> friend_list;
  58.     int friend_amount;
  59.  
  60.     public:
  61.     void add_friend (date birthdate, string friend_name)
  62.     {
  63.         ++ friend_amount;
  64.         friend_list.resize (friend_amount);
  65.        
  66.         friend_pair new_friend;
  67.         new_friend.birthdate = birthdate;
  68.         new_friend.name = friend_name;
  69.        
  70.         friend_list[friend_amount-1] = new_friend;
  71.     };
  72.     void get_new_friend ()
  73.     {
  74.         string friend_name;
  75.         date birthdate;
  76.        
  77.         cout << "Enter the Name \n";
  78.         cin >> friend_name;
  79.        
  80.         cout << "Enter the Date, separated by spaces (dd mm yyyy) \n";
  81.         cin >> birthdate.day >> birthdate.month >> birthdate.year;
  82.        
  83.         while (birthdate.day > 31 || birthdate.month > 12)
  84.         {
  85.             cout << "Please input the date again, separated by spaces (dd mm yyyy) \n";
  86.             cin >> birthdate.day >> birthdate.month >> birthdate.year;
  87.         }
  88.         add_friend (birthdate, friend_name);
  89.     };
  90.     void show ()
  91.     {
  92.         int i;
  93.         int printing;
  94.         friend_pair current;
  95.         friend_pair next;
  96.  
  97.         if (friend_amount == 0)         /* If there are no friends to show, do nothing */
  98.         {
  99.             return;
  100.         }
  101.  
  102.         std::sort (friend_list.begin(), friend_list.end() );
  103.  
  104.         printing = 0;
  105.         current = friend_list[0];
  106.         for (i=1; i<friend_amount; i++)
  107.         {
  108.             next = friend_list[i];
  109.             if (current.birthdate == next.birthdate)
  110.             {
  111.                 current.print_friend_pair ();
  112.                 printing = 1;
  113.                 current = next;
  114.             } else {
  115.                 if (printing == 1)
  116.                 {
  117.                     current.print_friend_pair ();
  118.                     current = next;
  119.                     printing = 0;
  120.                 } else {
  121.                     current = next;
  122.                 }
  123.             }
  124.         }
  125.         if (printing == 1)
  126.         {
  127.             current.print_friend_pair ();
  128.         }
  129.     };
  130.     FriendsGroup()
  131.     {
  132.         friend_amount = 0;
  133.         friend_list.resize (friend_amount);
  134.     };
  135. };
  136.  
  137. int main (void)
  138. {
  139.     FriendsGroup people;
  140.     string status;
  141.     string input;
  142.    
  143.     status = "Receiving";
  144.    
  145.     while (status == "Receiving")
  146.     {
  147.         cout << "Choose your action: Add, Show, Exit \n";
  148.         cin >> input;
  149.        
  150.         if (input == "Exit")
  151.         {
  152.             status = "Exit";
  153.         } else if (input == "Show") {
  154.             people.show();
  155.         } else if (input == "Add") {
  156.             people.get_new_friend ();
  157.         } else {
  158.             cout << "That is not a valid input \n";
  159.         }
  160.     }
  161.  
  162.     return 0;
  163. }
Add Comment
Please, Sign In to add comment