80LK

LR22 AKT

Dec 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.59 KB | None | 0 0
  1. class Date {
  2.     static const string fileBD;
  3.     static string getNameMonth(int month) {
  4.         if (month < 1)
  5.             throw "A month cannot be less than 1";
  6.  
  7.         if (month > 12)
  8.             throw "A month cannot be more than 12";
  9.  
  10.         string m[12] = {"january","february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
  11.         return m[month - 1];
  12.     }
  13.     int year, month, day;
  14.     int maxDayInMonth(int month) {
  15.         switch (month)
  16.         {
  17.             case 1:
  18.             case 3:
  19.             case 5:
  20.             case 7:
  21.             case 8:
  22.             case 10:
  23.             case 12:
  24.                 return 31;
  25.             break;
  26.  
  27.             case 4:
  28.             case 6:
  29.             case 9:
  30.             case 11:
  31.                 return 30;
  32.             break;
  33.            
  34.             case 2:
  35.                 if (year % 4 == 0)
  36.                     return 29;
  37.                 else
  38.                     return 28;
  39.             break;
  40.         }
  41.         return 0;
  42.     }
  43. public:
  44.     Date() {
  45.         day = 1;
  46.         month = 1;
  47.         year = 1900;
  48.     }
  49.     void setDay(int i) {
  50.         if (i < 1)
  51.             throw "Day cannot be less than 1";
  52.  
  53.         if (maxDayInMonth(month) < i)
  54.             throw "A day cannot be more than this month";
  55.  
  56.         day = i;
  57.     }
  58.  
  59.     void setMonth(int i ) {
  60.         if (i < 1)
  61.             throw "A month cannot be less than 1";
  62.  
  63.         if (i > 12)
  64.             throw "A month cannot be more than 12";
  65.  
  66.         month = i;
  67.         if (day > maxDayInMonth(i))
  68.             setDay(maxDayInMonth(i));
  69.     }
  70.  
  71.     void setYear(int i) {
  72.         if (i < 1900)
  73.             throw "A year cannot be less than 1900";
  74.  
  75.         year = i;
  76.     }
  77.  
  78.     void addInBD() {
  79.         ofstream bd(fileBD, ios::app);
  80.  
  81.         bd << day << " " << month << " " << year << endl;
  82.  
  83.         bd.close();
  84.     }
  85.  
  86.     void print() { print(false);  }
  87.     void print(bool a) {
  88.         if (a) {
  89.             cout << day << " " << getNameMonth(month) << " " << year << endl;
  90.         } else {
  91.             if (day < 10) cout << "0";
  92.             cout << day << ".";
  93.  
  94.             if (month < 10) cout << "0";
  95.             cout << month << "." << year << endl;
  96.         }
  97.     }
  98.  
  99.     static void printAllDates() {
  100.         ifstream bd(fileBD);
  101.         Date a; int i, l = 0;
  102.         while (bd >> i) {
  103.             a.setDay(i);
  104.             bd >> i; a.setMonth(i);
  105.             bd >> i; a.setYear(i);
  106.  
  107.             a.print();
  108.             l++;
  109.         }
  110.  
  111.         if (l == 0)
  112.             cout << "The database is empty" << endl;
  113.     }
  114.  
  115.     static void printAllSpringDates() {
  116.         ifstream bd(fileBD);
  117.         Date a; int i, n, l = 0;
  118.         while (bd >> i) {
  119.             a.setDay(i);
  120.             bd >> n; a.setMonth(n);
  121.             bd >> i; a.setYear(i);
  122.  
  123.             if (n >= 3 && n <= 5) {
  124.                 a.print();
  125.                 l++;
  126.             }
  127.         }
  128.         if (l == 0)
  129.             cout << "There are no spring dates" << endl;
  130.     }
  131.  
  132.     static void printAllDatesWithStartLetter(char s) {
  133.         ifstream bd(fileBD);
  134.         Date a; int i, n, l = 0;
  135.         while (bd >> i) {
  136.             a.setDay(i);
  137.             bd >> n; a.setMonth(n);
  138.             bd >> i; a.setYear(i);
  139.  
  140.             if (getNameMonth(n)[0] == s) {
  141.                 a.print(true);
  142.                 l++;
  143.             }
  144.         }
  145.  
  146.         if (l == 0)
  147.             cout << "No dates starting with letter " << s << endl;
  148.     }
  149. };
  150.  
  151. const string Date::fileBD = "bd.txt";
  152.  
  153. void addNewDate();
  154. void showAllDates();
  155. void showAllSpringDates();
  156. void showAllDatesWithStartLetter();
  157.  
  158. int main()
  159. {
  160.     system("cls");
  161.     int m;
  162.     cout << "Menu:" << endl
  163.          << "1 - Add new date" << endl
  164.          << "2 - Show all dates" << endl
  165.          << "3 - Show all spring dates" << endl
  166.          << "4 - Show all dates whose month begins with the specified letter" << endl
  167.          << "0/5 - Exit" << endl;
  168.  
  169.     cin >> m;
  170.     switch (m)
  171.     {
  172.         case 1: addNewDate(); break;
  173.         case 2: showAllDates(); break;
  174.         case 3: showAllSpringDates(); break;
  175.         case 4: showAllDatesWithStartLetter(); break;
  176.         case 0: case 5: system("cls"); break;
  177.         default: main(); break;
  178.     }
  179. }
  180.  
  181. void addNewDate() {
  182.     int i;
  183.     Date a;
  184.  
  185.     system("cls");
  186.     cout << "Add new date" << endl
  187.          << "0 - Exit in menu" << endl
  188.          << "Enter day: ";
  189.  
  190.     try {
  191.         cin >> i;
  192.         if (i == 0) return (void)main();
  193.         a.setDay(i);
  194.  
  195.         cout << "Enter the month number: ";
  196.         cin >> i;
  197.         if (i == 0) return (void)main();
  198.         a.setMonth(i);
  199.  
  200.         cout << "Enter year: ";
  201.         cin >> i;
  202.         if (i == 0) return (void)main();
  203.         a.setYear(i);
  204.     } catch (const char* e) {
  205.         cerr << e << endl;
  206.         system("pause");
  207.         return addNewDate();
  208.     }
  209.  
  210.     a.addInBD();
  211.     cout << "Date added to database" << endl;
  212.     system("pause");
  213.     addNewDate();
  214. }
  215.  
  216. void showAllDates() {
  217.     system("cls");
  218.     cout << "Show all dates" << endl;
  219.     Date::printAllDates();
  220.     system("pause");
  221.     main();
  222. }
  223.  
  224. void showAllSpringDates() {
  225.     system("cls");
  226.     cout << "Show all spring dates" << endl;
  227.     Date::printAllSpringDates();
  228.     system("pause");
  229.     main();
  230. }
  231.  
  232. void showAllDatesWithStartLetter() {
  233.     char a;
  234.  
  235.     system("cls");
  236.     cout << "Show all dates whose month begins with the specified letter" << endl
  237.          << "0 - Exit in Menu" << endl
  238.          << "Enter a letter: ";
  239.  
  240.     cin >> a;
  241.     if (a == '0') return (void)main();
  242.  
  243.     Date::printAllDatesWithStartLetter(a);
  244.     system("pause");
  245.     showAllDatesWithStartLetter();
  246. }
Add Comment
Please, Sign In to add comment