SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <cstdlib> | |
| 2 | #include <iostream> | |
| 3 | #include <string> | |
| 4 | #include <vector> | |
| 5 | #include <algorithm> | |
| 6 | ||
| 7 | using namespace std; | |
| 8 | ||
| 9 | enum Status {
| |
| 10 | Receiving, | |
| 11 | Exit | |
| 12 | }; | |
| 13 | ||
| 14 | struct date | |
| 15 | {
| |
| 16 | unsigned int day; | |
| 17 | unsigned int month; | |
| 18 | unsigned int year; | |
| 19 | ||
| 20 | bool month_day_match (const date& d1)const | |
| 21 | {
| |
| 22 | return ((d1.month == month) && (d1.day == day)); | |
| 23 | }; | |
| 24 | }; | |
| 25 | ||
| 26 | struct friend_pair | |
| 27 | {
| |
| 28 | date birthdate; | |
| 29 | string name; | |
| 30 | ||
| 31 | void print_friend_pair () | |
| 32 | {
| |
| 33 | cout << name << ": " << birthdate.day << "/" << birthdate.month << "/" << birthdate.year << endl; | |
| 34 | }; | |
| 35 | bool operator < (const friend_pair & second)const /* We overload the < operator for later use */ | |
| 36 | { /* Orders two dates without paying attention to the year */
| |
| 37 | ||
| 38 | bool result; | |
| 39 | ||
| 40 | if (birthdate.month < second.birthdate.month) | |
| 41 | {
| |
| 42 | result = true; | |
| 43 | } else if (birthdate.month > second.birthdate.month) {
| |
| 44 | result = false; | |
| 45 | } else if (birthdate.day <= second.birthdate.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 | ||
| 59 | public: | |
| 60 | void add_friend (date birthdate, string friend_name) | |
| 61 | {
| |
| 62 | friend_pair new_friend; | |
| 63 | new_friend.birthdate = birthdate; | |
| 64 | new_friend.name = friend_name; | |
| 65 | ||
| 66 | friend_list.push_back (new_friend); | |
| 67 | }; | |
| 68 | void get_new_friend () | |
| 69 | {
| |
| 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 | {
| |
| 81 | cout << "Please input the date again, separated by spaces (dd mm yyyy) \n"; | |
| 82 | cin >> birthdate.day >> birthdate.month >> birthdate.year; | |
| 83 | } | |
| 84 | - | int printing; |
| 84 | + | |
| 85 | }; | |
| 86 | void show () | |
| 87 | {
| |
| 88 | int i; | |
| 89 | bool printing; | |
| 90 | friend_pair current; | |
| 91 | friend_pair next; | |
| 92 | ||
| 93 | if (friend_list.size() == 0) /* If there are no friends to show, do nothing */ | |
| 94 | {
| |
| 95 | return; | |
| 96 | } | |
| 97 | ||
| 98 | std::sort (friend_list.begin(), friend_list.end() ); | |
| 99 | ||
| 100 | printing = 0; | |
| 101 | current = friend_list[0]; | |
| 102 | for (i=1; i<friend_list.size(); i++) | |
| 103 | {
| |
| 104 | next = friend_list[i]; | |
| 105 | if (current.birthdate.month_day_match(next.birthdate) ) | |
| 106 | {
| |
| 107 | current.print_friend_pair (); | |
| 108 | printing = 1; | |
| 109 | current = next; | |
| 110 | } else {
| |
| 111 | if (printing == 1) | |
| 112 | {
| |
| 113 | current.print_friend_pair (); | |
| 114 | current = next; | |
| 115 | printing = 0; | |
| 116 | } else {
| |
| 117 | current = next; | |
| 118 | } | |
| 119 | } | |
| 120 | } | |
| 121 | if (printing == 1) | |
| 122 | {
| |
| 123 | current.print_friend_pair (); | |
| 124 | } | |
| 125 | }; | |
| 126 | FriendsGroup() | |
| 127 | {
| |
| 128 | friend_list.resize (0); | |
| 129 | }; | |
| 130 | - | string status; |
| 130 | + | |
| 131 | ||
| 132 | int main (void) | |
| 133 | - | status = "Receiving"; |
| 133 | + | |
| 134 | FriendsGroup people; | |
| 135 | - | while (status == "Receiving") |
| 135 | + | Status status; |
| 136 | string input; | |
| 137 | ||
| 138 | status = Receiving; | |
| 139 | ||
| 140 | while (status == Receiving) | |
| 141 | {
| |
| 142 | - | status = "Exit"; |
| 142 | + | |
| 143 | cin >> input; | |
| 144 | ||
| 145 | if (input == "Exit") | |
| 146 | {
| |
| 147 | status = Exit; | |
| 148 | } else if (input == "Show") {
| |
| 149 | people.show(); | |
| 150 | } else if (input == "Add") {
| |
| 151 | people.get_new_friend (); | |
| 152 | } else {
| |
| 153 | cout << "That is not a valid input \n"; | |
| 154 | } | |
| 155 | } | |
| 156 | ||
| 157 | return 0; | |
| 158 | } |