pabloliva87

Ej6C++

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