Advertisement
Guest User

Untitled

a guest
May 21st, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7.  
  8.  
  9.  
  10. struct Car
  11. {
  12.     struct Fio
  13.     {
  14.         char *Name;
  15.         char *Surname;
  16.         char *SecondName;
  17.     } Owner;
  18.     struct Date
  19.     {
  20.         int day;
  21.         char *month;
  22.         int year;
  23.     } checkup;
  24.     char *model; //Марка машины
  25.     int enginePower; //Мощность двигателя
  26.     int mileage; //Пробег
  27. };
  28.  
  29.  
  30.  
  31.  
  32. void createNewCar(Car *&cars, int &size);
  33. void showInfo(Car someCar);
  34. void push_back(Car *&cars, int &size, Car newCar);
  35. void showArrayFullInfo(Car *cars, int size);
  36. void showCarInfo(Car someCar);
  37. void showCarCatalog(Car *cars, int size);
  38. void mainMenu(Car *cars, int size);
  39. void findModel(Car *cars, int size);
  40. void mileageSort(Car *cars, int size);
  41. void saveData(Car newCar);
  42. void loadData(Car *&cars, int &size);
  43. void findCheckup(Car *cars, int size);
  44.  
  45. int main() {
  46.  
  47.     int size = 0;
  48.     Car *cars = new Car[size];
  49.    
  50.     loadData(cars, size);
  51.     mainMenu(cars, size);
  52.  
  53.  
  54.     return(0);
  55. }
  56.  
  57.  
  58. //Создаем новый элемент (машину)
  59. void createNewCar(Car *&cars, int &size) {
  60.     Car newCar;
  61.  
  62.     int buffSize = 0,
  63.         intBuff = 0;
  64.     char buff[100];
  65.  
  66.     cout << "Enter car model: " << endl;
  67.     cin.get(buff, 100); //Принимаем строку
  68.     buffSize = strlen(buff) + 1; //Определяем ее длину
  69.     newCar.model = new char[buffSize];  //Выделяем память
  70.     strcpy_s(newCar.model, buffSize, buff); //Копируем строку в элемент структуры
  71.     cin.ignore(numeric_limits<streamsize>::max(), '\n');  // чистим поток
  72.  
  73.     cout << "Enter owner surname: " << endl;
  74.     cin.get(buff, 100);
  75.     buffSize = strlen(buff) + 1;
  76.     newCar.Owner.Surname = new char[buffSize];
  77.     strcpy_s(newCar.Owner.Surname, buffSize, buff);
  78.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  79.  
  80.     cout << "Enter owner name: " << endl;
  81.     cin.get(buff, 100);
  82.     buffSize = strlen(buff) + 1;
  83.     newCar.Owner.Name = new char[buffSize];
  84.     strcpy_s(newCar.Owner.Name, buffSize, buff);
  85.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  86.  
  87.     cout << "Enter owner second name: " << endl;
  88.     cin.get(buff, 100);
  89.     buffSize = strlen(buff) + 1;
  90.     newCar.Owner.SecondName = new char[buffSize];
  91.     strcpy_s(newCar.Owner.SecondName, buffSize, buff);
  92.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  93.  
  94.  
  95.     bool bFail = true;
  96.     do {
  97.  
  98.         cout << "Enter engine power (in h.p.): " << endl;
  99.         cin >> newCar.enginePower;
  100.  
  101.         bFail = cin.fail();
  102.         cin.clear();
  103.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  104.  
  105.     } while (bFail == true);
  106.  
  107.     do {
  108.  
  109.         cout << "Enter mileage: " << endl;
  110.         cin >> newCar.mileage;
  111.  
  112.         bFail = cin.fail();
  113.         cin.clear();
  114.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  115.  
  116.     } while (bFail == true);
  117.  
  118.     cout << "Enter the last checkup date: " << endl;
  119.  
  120.     do {
  121.  
  122.         cout << "Enter day: " << endl;
  123.         cin >> newCar.checkup.day;
  124.  
  125.         bFail = cin.fail();
  126.         cin.clear();
  127.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  128.  
  129.     } while (bFail == true);
  130.  
  131.     cout << "Enter month (January, May, September etc.): " << endl;
  132.     cin.get(buff, 100);
  133.     buffSize = strlen(buff) + 1;
  134.     newCar.checkup.month = new char[buffSize];
  135.     strcpy_s(newCar.checkup.month, buffSize, buff);
  136.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  137.  
  138.     do {
  139.  
  140.         cout << "Enter year: " << endl;
  141.         cin >> newCar.checkup.year;
  142.  
  143.         bFail = cin.fail();
  144.         cin.clear();
  145.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  146.  
  147.     } while (bFail == true);
  148.  
  149.     saveData(newCar);
  150.  
  151.     push_back(cars, size, newCar);
  152.  
  153. }
  154.  
  155. void showInfo(Car someCar)
  156. {  
  157.    
  158.  
  159.     cout.width(20);
  160.     cout << someCar.model;
  161.     cout.width(20);
  162.     cout << someCar.Owner.Surname;
  163.     cout.width(20);
  164.     cout << someCar.Owner.Name;
  165.     cout.width(20);
  166.     cout << someCar.Owner.SecondName;
  167.     cout.width(20);
  168.     cout << someCar.enginePower;
  169.     cout.width(10);
  170.     cout << someCar.mileage;
  171.     cout.width(6);
  172.     cout << someCar.checkup.day;
  173.     cout << ".";
  174.     cout << someCar.checkup.month << "." << someCar.checkup.year << endl;
  175.  
  176. }
  177.  
  178. //Функция добавления элемента в динамический массив
  179. void push_back(Car *&cars, int &size, Car newCar)
  180. {
  181.     Car *newArray = new Car[size + 1];
  182.     for (int i = 0; i < size; i++)
  183.     {
  184.         newArray[i] = cars[i];
  185.     }
  186.  
  187.     newArray[size++] = newCar;
  188.  
  189.     delete[] cars;
  190.  
  191.     cars = newArray;
  192. }
  193.  
  194. void showArrayFullInfo(Car *cars, int size)
  195. {
  196.     if (size == 0)
  197.     {
  198.         cout << "There are no cars in database!" << endl;
  199.     }
  200.     else
  201.     {
  202.         cout.width(20);
  203.         cout << "Car model";
  204.         cout.width(20);
  205.         cout << "Owner Surname";
  206.         cout.width(20);
  207.         cout << "Owner name";
  208.         cout.width(20);
  209.         cout << "Owner second name";
  210.         cout.width(20);
  211.         cout << "Engine power (hp)";
  212.         cout.width(10);
  213.         cout << "Mileage";
  214.         cout.width(20);
  215.         cout << "Last checkup date" << endl << endl;
  216.         for (int i = 0; i < size; i++)
  217.         {
  218.             showInfo(cars[i]);
  219.         }
  220.     }
  221. }
  222.  
  223. void showCarInfo(Car someCar)
  224. {
  225.     cout.width(20);
  226.     cout << "Car model";
  227.     cout.width(20);
  228.     cout << "Owner Surname";
  229.     cout.width(20);
  230.     cout << "Owner name";
  231.     cout.width(20);
  232.     cout << "Owner second name";
  233.     cout.width(20);
  234.     cout << "Engine power (hp)";
  235.     cout.width(10);
  236.     cout << "Mileage";
  237.     cout.width(20);
  238.     cout << "Last checkup date" << endl << endl;
  239.    
  240.     showInfo(someCar);
  241. }
  242.  
  243. void showCarCatalog(Car *cars, int size)
  244. {
  245.     if (size == 0)
  246.     {
  247.         cout << "There are no cars in database!" << endl;
  248.     }
  249.     else
  250.     {
  251.         for (int i = 0; i < size; i++)
  252.         {
  253.             cout << i + 1 << ") " << cars[i].model << endl;
  254.         }
  255.     }
  256. }
  257.  
  258. //Основная функция - главное меню
  259. void mainMenu(Car *cars, int size)
  260. {
  261.     while (true)
  262.     {
  263.         cout << endl << endl << "------------------------------------------------" << endl << endl;
  264.         cout << "Choose any action: " << endl;
  265.         cout << "1) Show all cars catalog" << endl; //Вывести все машины в каталоге
  266.         cout << "2) Show car info" << endl;  //Вывести информацию по определенной машине
  267.         cout << "3) Add a new car to catalog" << endl;  // Добавить новую машину в каталог
  268.         cout << "4) Show all cars catalog (FULL INFO)" << endl; //Вывод каталога с полной информацией
  269.         cout << "5) Find an exact model" << endl; //Поиск и вывод введенной модели
  270.         cout << "6) Sort cars with mileage bigger than ... by owners " << endl; //Поиск и вывод отсортированных по алфавиту владельцев машин, у которых пробег большего введеного
  271.         cout << "7) Show cars with checkup made more than 18 months ago" << endl;
  272.         cout << "0) Exit" << endl;
  273.         int answer = -1;
  274.         bool bFail = true;
  275.         while (answer < 0 || answer > 7 || bFail == true)
  276.         {
  277.             cout << "Enter the number: ";
  278.             cin >> answer;
  279.             bFail = cin.fail();
  280.             cin.clear();
  281.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  282.         }
  283.         cout << "------------------------------------------------" << endl << endl;
  284.         switch (answer)
  285.         {
  286.         case 1:
  287.             showCarCatalog(cars, size);
  288.             break;
  289.         case 2:
  290.             if (size == 0)
  291.             {
  292.                 cout << "There are no cars in database!" << endl;
  293.             }
  294.             else
  295.             {
  296.                 cout << "Enter car catalog number to get information (or enter 0 to exit): " << endl;
  297.                 answer = -1;
  298.                 showCarCatalog(cars, size);
  299.                 while (answer < 0 || answer > size)
  300.                 {
  301.                     cout << "Enter the number: ";
  302.                     cin >> answer;
  303.                     cin.clear();
  304.                     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  305.                 }
  306.                 if (answer == 0)
  307.                 {
  308.                     break;
  309.                 }
  310.                 showCarInfo(cars[answer - 1]);
  311.             }
  312.             break;
  313.         case 3:
  314.             createNewCar(cars, size);
  315.             break;
  316.         case 4:
  317.             showArrayFullInfo(cars, size);
  318.             break;
  319.         case 5:
  320.             findModel(cars, size);
  321.             break;
  322.         case 6:
  323.             mileageSort(cars, size);
  324.             break;
  325.         case 7:
  326.             findCheckup(cars, size);
  327.             break;
  328.         case 0:
  329.             exit(0);
  330.         }
  331.     }
  332. }
  333.  
  334. void findModel(Car *cars, int size)
  335. {
  336.     if (size == 0)
  337.     {
  338.         cout << "There are no cars in database!" << endl;
  339.     }
  340.     else
  341.     {
  342.         char model[100];
  343.         cout << "Enter the interested model: ";
  344.         cin.get(model, 100);
  345.         cout << model << endl;
  346.         for (int i = 0; i < size; i++)
  347.         {
  348.             if (!_stricmp(cars[i].model, model))  // Сравнение независимо от регистра
  349.             {
  350.                 showCarInfo(cars[i]);
  351.             }
  352.         }
  353.     }
  354. }
  355.  
  356. void mileageSort(Car *cars, int size)
  357. {  
  358.     if (size == 0)
  359.     {
  360.         cout << "There are no cars in database!" << endl;
  361.     }
  362.     else
  363.     {
  364.         int givenMileage = 0,
  365.             sortedSize = 0;
  366.         bool bFail = true;
  367.         Car *mileageSortCars = new Car[sortedSize]; //создаем массив структур и выделяем под него память
  368.  
  369.         do {
  370.  
  371.             cout << "Enter mileage: " << endl;
  372.             cin >> givenMileage;
  373.  
  374.             bFail = cin.fail();
  375.             cin.clear();
  376.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  377.  
  378.         } while (bFail == true);
  379.  
  380.         for (int i = 0; i < size; i++)
  381.         {
  382.             if (cars[i].mileage > givenMileage)
  383.             {
  384.                 push_back(mileageSortCars, sortedSize, cars[i]); //Добавляем подходящие машины в массив на вывод
  385.             }
  386.         }
  387.  
  388.  
  389.         //Сортировка массива по алфавиту
  390.  
  391.         for (int i = 0; i < sortedSize - 1; i++)
  392.         {
  393.             for (int j = i + 1; j < sortedSize; j++)
  394.             {
  395.                 if (_stricmp(mileageSortCars[i].Owner.Surname, mileageSortCars[j].Owner.Surname) == 0)  // Если фамилии одинаковы, сравниваем по имени
  396.                 {
  397.                     if (_stricmp(mileageSortCars[i].Owner.Name, mileageSortCars[j].Owner.Name) > 0)
  398.                     {
  399.                         Car q = mileageSortCars[i];
  400.                         mileageSortCars[i] = mileageSortCars[j];
  401.                         mileageSortCars[j] = q;
  402.                     }
  403.                 }
  404.  
  405.                 if (_stricmp(mileageSortCars[i].Owner.Surname, mileageSortCars[j].Owner.Surname) > 0)
  406.                 {
  407.                     Car q = mileageSortCars[i];
  408.                     mileageSortCars[i] = mileageSortCars[j];
  409.                     mileageSortCars[j] = q;
  410.                 }
  411.             }
  412.  
  413.         }
  414.  
  415.  
  416.         for (int i = 0; i < sortedSize; i++)
  417.         {
  418.             cout << mileageSortCars[i].Owner.Surname << " " << mileageSortCars[i].Owner.Name << endl;
  419.         }
  420.  
  421.         delete[] mileageSortCars;  //Освобождаем память
  422.         mileageSortCars = nullptr;
  423.     }
  424. }
  425.  
  426. void saveData(Car newCar) {
  427.     ofstream fout;
  428.  
  429.     fout.open("database.txt", ofstream::app);
  430.    
  431.     if (!fout.is_open())
  432.     {
  433.         cout << "Error opening file" << endl;
  434.         fout.open("database.txt");
  435.     }
  436.  
  437.     if (!fout.is_open())
  438.     {
  439.         cout << "Error opening file" << endl;
  440.     }
  441.     else
  442.     {
  443.             fout << newCar.model << endl;
  444.             fout << newCar.Owner.Name << endl;
  445.             fout << newCar.Owner.Surname << endl;
  446.             fout << newCar.Owner.SecondName << endl;
  447.             fout << newCar.enginePower << endl;
  448.             fout << newCar.mileage << endl;
  449.             fout << newCar.checkup.day << endl;
  450.             fout << newCar.checkup.month << endl;
  451.             fout << newCar.checkup.year << endl;
  452.             cout << "data was written" << endl;
  453.     }
  454.  
  455.     fout.close();
  456. }
  457.  
  458. void loadData(Car *&cars, int &size) {
  459.     ifstream fin;
  460.     fin.open("database.txt");
  461.  
  462.     if (!fin.is_open())
  463.     {
  464.         cout << "Can't download database!" << endl;
  465.     }
  466.     else
  467.     {  
  468.         cout << "Database is opened!" << endl;
  469.         int buffSize = 0,
  470.             intBuff = 0,
  471.             num = 0;
  472.         char buff[100];
  473.         while (!fin.eof())
  474.         {
  475.             Car newCar;
  476.  
  477.             fin.getline(buff, 100);
  478.             if (strlen(buff) == 0)
  479.             {
  480.                 break;
  481.             }
  482.             buffSize = strlen(buff) + 1; //Определяем ее длину
  483.             newCar.model = new char[buffSize];  //Выделяем память
  484.             strcpy_s(newCar.model, buffSize, buff); //Копируем строку в элемент структуры
  485.  
  486.             fin.getline(buff, 100);
  487.             buffSize = strlen(buff) + 1; //Определяем ее длину
  488.             newCar.Owner.Surname = new char[buffSize];  //Выделяем память
  489.             strcpy_s(newCar.Owner.Surname, buffSize, buff); //Копируем строку в элемент структуры
  490.  
  491.  
  492.             fin.getline(buff, 100);
  493.             buffSize = strlen(buff) + 1; //Определяем ее длину
  494.             newCar.Owner.Name = new char[buffSize];  //Выделяем память
  495.             strcpy_s(newCar.Owner.Name, buffSize, buff); //Копируем строку в элемент структуры
  496.            
  497.  
  498.             fin.getline(buff, 100);
  499.             buffSize = strlen(buff) + 1; //Определяем ее длину
  500.             newCar.Owner.SecondName = new char[buffSize];  //Выделяем память
  501.             strcpy_s(newCar.Owner.SecondName, buffSize, buff); //Копируем строку в элемент структуры
  502.  
  503.             fin.getline(buff, 100);
  504.             num = atoi(buff);
  505.             newCar.enginePower = num;
  506.             fin.getline(buff, 100);
  507.             num = atoi(buff);
  508.             newCar.mileage = num;
  509.             fin.getline(buff, 100);
  510.             num = atoi(buff);
  511.             newCar.checkup.day = num;
  512.  
  513.             fin.getline(buff, 100);
  514.             buffSize = strlen(buff) + 1; //Определяем ее длину
  515.             newCar.checkup.month = new char[buffSize];  //Выделяем память
  516.             strcpy_s(newCar.checkup.month, buffSize, buff); //Копируем строку в элемент структуры
  517.    
  518.             fin.getline(buff, 100);
  519.             num = atoi(buff);
  520.             newCar.checkup.year = num;
  521.             push_back(cars, size, newCar);
  522.  
  523.         };
  524.     }
  525. }
  526.  
  527. void findCheckup(Car *cars, int size) {
  528.     if (size == 0)
  529.     {
  530.         cout << "There are no cars in database!" << endl;
  531.     }
  532.     else
  533.     {
  534.         int sortedSize = 0;
  535.         Car *checkupSortCars = new Car[sortedSize];
  536.  
  537.         time_t now;
  538.         struct tm nowLocal;
  539.         now = time(NULL);
  540.         localtime_s(&nowLocal, &now);
  541.         int currentMonthAmount = (nowLocal.tm_year + 1900) * 12 + nowLocal.tm_mon;
  542.         for (int i = 0; i < size; i++)
  543.         {
  544.             int month = 0;
  545.             if (_stricmp(cars[i].checkup.month, "January") == 0) {
  546.                 month = 0;
  547.             };
  548.             if (_stricmp(cars[i].checkup.month, "February") == 0) {
  549.                 month = 1;
  550.             };
  551.             if (_stricmp(cars[i].checkup.month, "March") == 0) {
  552.                 month = 2;
  553.             };
  554.             if (_stricmp(cars[i].checkup.month, "April") == 0) {
  555.                 month = 3;
  556.             };
  557.             if (_stricmp(cars[i].checkup.month, "May") == 0) {
  558.                 month = 4;
  559.             };
  560.             if (_stricmp(cars[i].checkup.month, "June") == 0) {
  561.                 month = 5;
  562.             };
  563.             if (_stricmp(cars[i].checkup.month, "July") == 0) {
  564.                 month = 6;
  565.             };
  566.             if (_stricmp(cars[i].checkup.month, "August") == 0) {
  567.                 month = 7;
  568.             };
  569.             if (_stricmp(cars[i].checkup.month, "September") == 0) {
  570.                 month = 8;
  571.             };
  572.             if (_stricmp(cars[i].checkup.month, "October") == 0) {
  573.                 month = 9;
  574.             };
  575.             if (_stricmp(cars[i].checkup.month, "November") == 0) {
  576.                 month = 10;
  577.             };
  578.             if (_stricmp(cars[i].checkup.month, "December") == 0) {
  579.                 month = 11;
  580.             };
  581.  
  582.             int monthAmount = cars[i].checkup.year * 12 + month;
  583.  
  584.             if (currentMonthAmount - monthAmount > 18)
  585.             {
  586.                 push_back(checkupSortCars, sortedSize, cars[i]);
  587.             }
  588.  
  589.             for (int i = 0; i < sortedSize; i++)
  590.             {
  591.                 cout << "This cars had checkup more than 18 months ago:" << endl;
  592.                 cout << checkupSortCars[i].model << " " << checkupSortCars[i].Owner.Surname << " " << checkupSortCars[i].Owner.Name << endl;
  593.             }
  594.  
  595.             delete[] checkupSortCars;  //Освобождаем память
  596.             checkupSortCars = nullptr;
  597.         };
  598.     }
  599.    
  600. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement