Advertisement
Alexandre_lsv

Untitled

Dec 22nd, 2016
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.02 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int N=10;
  4. const int MaxLNLen=15,
  5.           MaxFNLen=15,
  6.           MaxNum=11,
  7.           Today=26,
  8.           Tomonth=12,
  9.           Toyear=2016;
  10.   string spaces(int num){
  11.     string spacess{};
  12.     for(int i=0; i<num; i++)
  13.       spacess+=' ';
  14.     return spacess;
  15.   }
  16.  
  17. struct NOTE{
  18.   string lastName{}, firstName{};
  19.   int * birthDay;
  20.   string phoneNumber{};
  21.   string operatorName{};
  22.  
  23.   NOTE(string lastName,
  24.       string firstName,
  25.       int*  birthDay,
  26.       string phoneNumber,
  27.       string operatorName){
  28.     this->lastName=lastName,
  29.     this->firstName=firstName,
  30.     this->birthDay=birthDay,
  31.     this->phoneNumber=phoneNumber,
  32.     this->operatorName=operatorName;
  33.   }
  34.  
  35.   NOTE(){
  36.   }
  37.  
  38.   void scanNOTE(ifstream &fin){
  39.     birthDay=new int[3];
  40.     fin >>  this->lastName >>
  41.             this->firstName >>
  42.             this->birthDay[0] >> this->birthDay[1] >> this->birthDay[2] >>
  43.             this->phoneNumber >>
  44.             this->operatorName;
  45.   }
  46.  
  47.   void printNOTE(){
  48.     cout << lastName << spaces(MaxLNLen-lastName.length()) <<
  49.             firstName << spaces(MaxFNLen-firstName.length()) <<
  50.             birthDay[0] << spaces(3-log(birthDay[0]+1)/log(10)) <<
  51.             birthDay[1] << spaces(3-log(birthDay[1]+1)/log(10)) <<
  52.             birthDay[2] << spaces(8-log(birthDay[2]+1)/log(10)) <<
  53.             phoneNumber << spaces(MaxNum-phoneNumber.length()) <<
  54.             operatorName << endl;
  55.   }
  56. };
  57.  
  58. bool cmpLN(const NOTE &A, const NOTE &B){ // Для сортировки по фамилии
  59.   return A.lastName<B.lastName;
  60. }
  61.  
  62. bool cmpNum(const NOTE &A, const NOTE &B){ // Для сортировки по номеру телефона
  63.   return A.phoneNumber<B.phoneNumber;
  64. }
  65.  
  66. bool cmpBD(const NOTE &A, const NOTE &B){ // Для сортировки по дню рождения
  67.   return A.birthDay[0]<B.birthDay[0];
  68. }
  69.  
  70. bool cmp3Num(const NOTE &A, const NOTE &B){ // Для сортировки по первым трем цифрам номера телефона
  71.   return A.phoneNumber.substr(0,3)<B.phoneNumber.substr(0,3);
  72. }
  73.  
  74. void printNOTE(NOTE* &testArNOTE){ // Вывод базы абонентов
  75.   cout << "Last Name" << spaces(MaxLNLen-9) <<
  76.           "First Name" << spaces(MaxFNLen-10) <<
  77.           "Birthday" << spaces(6) <<
  78.           "Number" << spaces(MaxNum-6) <<
  79.           "Operator\n";
  80.   for(int i=0; i<N; i++){
  81.     testArNOTE[i].printNOTE();
  82.   }
  83. }
  84.  
  85. void findSortAndPrintOper(string &oper, NOTE * &testArNOTE){
  86.   sort(testArNOTE, testArNOTE+N, cmpNum); //Сортируем по номеру телефона
  87.   int count{};
  88.   for(int i=0; i<N; i++)
  89.     if (testArNOTE[i].operatorName==oper)
  90.       count++;
  91.   if(count==0)
  92.     cout << "Abonents with operator name \"" << oper << "\" not found\n";
  93.   else{
  94.     cout << "Last Name" << spaces(MaxLNLen-9) <<
  95.             "First Name" << spaces(MaxFNLen-10) <<
  96.             "Birthday" << spaces(6) <<
  97.             "Number" << spaces(MaxNum-6) <<
  98.             "Operator" << spaces(5) <<
  99.             "Old\n";
  100.     for(int i=0; i<N; i++)
  101.       if (testArNOTE[i].operatorName==oper)
  102.         cout << testArNOTE[i].lastName << spaces(MaxLNLen-testArNOTE[i].lastName.length()) <<
  103.                 testArNOTE[i].firstName << spaces(MaxFNLen-testArNOTE[i].firstName.length()) <<
  104.                 testArNOTE[i].birthDay[0] << spaces(3-log(testArNOTE[i].birthDay[0]+1)/log(10)) <<
  105.                 testArNOTE[i].birthDay[1] << spaces(3-log(testArNOTE[i].birthDay[1]+1)/log(10)) <<
  106.                 testArNOTE[i].birthDay[2] << spaces(8-log(testArNOTE[i].birthDay[2]+1)/log(10)) <<
  107.                 testArNOTE[i].phoneNumber << spaces(MaxNum-testArNOTE[i].phoneNumber.length()) <<
  108.                 testArNOTE[i].operatorName << spaces(13-testArNOTE[i].operatorName.length()) <<
  109.                 Toyear-testArNOTE[i].birthDay[2] - (Tomonth==testArNOTE[i].birthDay[1]?Today<testArNOTE[i].birthDay[0]:Tomonth<testArNOTE[i].birthDay[1]) << endl;
  110.   }
  111. }
  112.  
  113. void findSortAndPrintMonth(int &month, NOTE * &testArNOTE){
  114.   sort(testArNOTE, testArNOTE+N, cmpBD); // Сортируем по дню рождения
  115.   int count{};
  116.   for(int i=0; i<N; i++)
  117.     if (testArNOTE[i].birthDay[1]==month)
  118.       count++;
  119.   if(count==0)
  120.     cout << "Abonents with birth month \"" << month << "\" not found\n";
  121.   else{
  122.     cout << "Last Name" << spaces(MaxLNLen-9) <<
  123.           "First Name" << spaces(MaxFNLen-10) <<
  124.           "Birthday" << spaces(6) <<
  125.           "Number" << spaces(MaxNum-6) <<
  126.           "Operator\n";
  127.     for(int i=0; i<N; i++)
  128.       if (testArNOTE[i].birthDay[1]==month)
  129.         cout << testArNOTE[i].lastName << spaces(MaxLNLen-testArNOTE[i].lastName.length()) <<
  130.                 testArNOTE[i].firstName << spaces(MaxFNLen-testArNOTE[i].firstName.length()) <<
  131.                 testArNOTE[i].birthDay[0] << spaces(3-log(testArNOTE[i].birthDay[0]+1)/log(10)) <<
  132.                 testArNOTE[i].birthDay[1] << spaces(3-log(testArNOTE[i].birthDay[1]+1)/log(10)) <<
  133.                 testArNOTE[i].birthDay[2] << spaces(8-log(testArNOTE[i].birthDay[2]+1)/log(10)) <<
  134.                 testArNOTE[i].phoneNumber << spaces(MaxNum-testArNOTE[i].phoneNumber.length()) <<
  135.                 testArNOTE[i].operatorName << endl;
  136.   }
  137. }
  138.  
  139. int main(){
  140.   ifstream fin("abonDataBase.txt");
  141.   fin >> N;
  142.   NOTE *testArNOTE = new NOTE[N];
  143.   for(int i=0; i<N; i++){ // Считываем данные из файла
  144.     testArNOTE[i].scanNOTE(fin);
  145.   }
  146.   char key{};
  147.   cout << "Menu:\t2 -- print database,\n\t3 -- sort of last name,\n\t4 -- same oper,\n\t5 -- birth month,\n\t6 -- sort 3 num,\n\tE -- Exit\n";
  148.   cin >> key;
  149.   while(key!='E'){ // Пока пользователь не нажет "Е"
  150.     switch(key){
  151.       case '2': // Нажал 2, выводим данные
  152.       {
  153.         while(key=='2' || key=='Y'){
  154.           printNOTE(testArNOTE);
  155.           cout << "Retry? Y -- Yes, N -- No, E -- Exit\n";
  156.           cin >> key;
  157.         }
  158.         break;
  159.       }
  160.       case '3': // Нажал 3, сортируем по фамилии
  161.       {
  162.         while(key=='3' || key=='Y'){         // Неочевидно, зачем повторять, но почему бы и нет...
  163.           sort(testArNOTE, testArNOTE+N, cmpLN);
  164.           cout << "Retry? Y -- Yes, N -- No, E -- Exit\n";
  165.           cin >> key;
  166.         }
  167.         break;
  168.       }
  169.       case '4':{ // Нажал 4, находим всех абонентов с заданным оператором, сортируем и выводим
  170.         string oper{};
  171.         while(key=='4' || key=='Y'){
  172.           cout << "Print operator name: ";
  173.           cin >> oper;
  174.           findSortAndPrintOper(oper, testArNOTE);
  175.           cout << "Retry? Y -- Yes, N -- No, E -- Exit\n";
  176.           cin >> key;
  177.         }
  178.         break;
  179.       }
  180.       case '5': // Наэал 5, находим всех абонентов с заданным месяцев дня рождения, сортируем по дню и выводим
  181.       {
  182.         int month{};
  183.         while(key=='5' || key=='Y'){
  184.           cout << "Print month: ";
  185.           cin >> month;
  186.           findSortAndPrintMonth(month, testArNOTE);
  187.           cout << "Retry? Y -- Yes, N -- No, E -- Exit\n";
  188.           cin >> key;
  189.         }
  190.         break;
  191.       }
  192.       case '6': // Нажал 6, сортируем по первым 3 цифрам номера телефона и выводим
  193.       {
  194.         while(key=='6' || key=='Y'){
  195.           sort(testArNOTE, testArNOTE+N, cmp3Num);
  196.           printNOTE(testArNOTE);
  197.           cout << "Retry? Y -- Yes, N -- No, E -- Exit\n";
  198.           cin >> key;
  199.         }
  200.         break;
  201.       }
  202.     }
  203.     if (key=='E') // Если вдруг в каком-то из пунктов пользователь нажал на выход, не спрашиваем у него второй раз
  204.       return 0;
  205.   cout << "Menu:\t2 -- print database,\n\t3 -- sort of last name,\n\t4 -- same oper,\n\t5 -- birth month,\n\t6 -- sort 3 num,\n\tE -- Exit\n";
  206.     cin >> key;
  207.   }
  208.   return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement