Advertisement
Garey

Kontrolno2_BP2_zad4

Mar 24th, 2018
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <conio.h>
  4. #include <fstream>
  5.  
  6. #if __has_include(<regex>)
  7. #define has_regex true
  8. #include <regex>
  9. #endif
  10.  
  11. using namespace std;
  12.  
  13. struct Cars {
  14.     string marka;
  15.     string nomer;
  16. };
  17.  
  18.  
  19. auto input_cars(Cars *, size_t) -> void;
  20. auto output_cars(Cars *, size_t) -> void;
  21. auto search_cars(Cars *, size_t) -> void;
  22. auto change_car_number(Cars *, size_t) -> void;
  23.  
  24. int main() {
  25.  
  26.     size_t choice, count(0);
  27.  
  28.     Cars *cars = nullptr;
  29.  
  30.     do {
  31.         system("cls");
  32.  
  33.         cout << "Menu: \n\n";
  34.         cout << "[1] Input cars\n";
  35.         cout << "[2] Search in file\n";
  36.         cout << "[3] Change car number\n";
  37.         cout << "[4] Output information from the file\n\n";
  38.         cout << "[0] Exit\n";
  39.  
  40.         cout << "Your choice: ";
  41.         cin >> choice;
  42.  
  43.         switch (choice) {
  44.         case 1:
  45.             system("cls");
  46.  
  47.             cout << "Enter number of cars: ";
  48.             cin >> count;
  49.  
  50.             if (!cin || !count)
  51.                 cout << "Error: invalid number of cars";
  52.  
  53.             cars = new Cars[count];
  54.  
  55.             input_cars(cars, count);
  56.  
  57.             cout << "\n\nPress any key to get back to the menu!";
  58.             _getch();
  59.  
  60.             break;
  61.  
  62.         case 2:
  63.             system("cls");
  64.  
  65.             search_cars(cars, count);
  66.  
  67.             cout << "\n\nPress any key to get back to the menu!";
  68.             _getch();
  69.  
  70.  
  71.             break;
  72.  
  73.         case 3: {
  74.             system("cls");
  75.  
  76.             change_car_number(cars, count);
  77.  
  78.             cout << "\n\nPress any key to get back to the menu!";
  79.             _getch();
  80.  
  81.             break;
  82.         }
  83.         case 4:
  84.             system("cls");
  85.  
  86.             output_cars(cars, count);
  87.  
  88.             cout << "\n\nPress any key to get back to the menu!";
  89.             _getch();
  90.  
  91.             break;
  92.         }
  93.  
  94.     } while (choice != 0);
  95.  
  96.     return 0;
  97. }
  98.  
  99. auto input_cars(Cars *cars, size_t count) -> void {
  100.  
  101.     fstream file("cars.dat", ios::binary | ios::out);
  102.  
  103.     cin.ignore();
  104.  
  105.     for (size_t i = 0; i < count; i++) {
  106.         bool isCorrect(false);
  107.  
  108.         cout << "Enter the brand of car #" << i << " : ";
  109.         getline(cin, cars[i].marka);
  110.  
  111.         do {
  112.  
  113.             cout << "Enter car number: ";
  114.             getline(cin, cars[i].nomer);
  115.  
  116. #if has_regex
  117.             if (regex_match(cars[i].nomer, static_cast<regex> ("[A-Z]{1} [0-9]{4} [A-Z]{2}")))
  118.                 for (size_t j = 0; j < count - 1; j++)
  119.                     if (cars[j].nomer == cars[i].nomer && j != i)
  120.                         cout << "\n\nThe car number must be 9 characters and UNIQUE!\n\n";
  121.                     else
  122.                         isCorrect = true;
  123. #else
  124.             if (cars[i].nomer.length() == 9) {
  125.                 for (size_t j = 0; j < count - 1; j++)
  126.                     if (cars[j].nomer == cars[i].nomer  && j != i)
  127.                         isCorrect = true;
  128.                     else
  129.                         cout << "\n\nThe car number must be 9 characters and UNIQUE!\n\n";
  130.             }
  131. #endif
  132.  
  133.         } while (!isCorrect);
  134.     }
  135.  
  136.  
  137.     file.write(reinterpret_cast<char *> (&cars), count * sizeof(Cars));
  138.  
  139.     file.close();
  140. }
  141.  
  142. auto output_cars(Cars *cars, size_t count) -> void {
  143.  
  144.     fstream file("cars.dat", ios::binary | ios::in);
  145.  
  146.     file.read(reinterpret_cast<char *> (&cars), count * sizeof(Cars));
  147.  
  148.     for (size_t i = 0; i < count; i++)
  149.         cout << "Marka: " + cars[i].marka + " | Nomer: " + cars[i].nomer << endl;
  150.  
  151.     file.close();
  152. }
  153.  
  154. auto search_cars(Cars *cars, size_t count) -> void {
  155.  
  156.     fstream file("cars.dat", ios::binary | ios::in);
  157.  
  158.     file.read(reinterpret_cast<char *> (&cars), count * sizeof(Cars));
  159.  
  160.     size_t counter(0);
  161.  
  162.     for (size_t i = 0; i < count; i++) {
  163.         if (strstr(cars[i].nomer.c_str(), "B 57")) {
  164.             cout << "Marka: " + cars[i].marka + " | Nomer: " + cars[i].nomer << endl;
  165.         }
  166.         else
  167.             counter++;
  168.     }
  169.  
  170.     if (counter == 0)
  171.         cout << "There are no cars with number that starts with \"B 57\"";
  172.  
  173.     file.close();
  174. }
  175.  
  176. auto change_car_number(Cars *cars, size_t count) -> void {
  177.  
  178.     fstream file("cars.dat", ios::binary | ios::in);
  179.  
  180.     file.read(reinterpret_cast<char *> (&cars), count * sizeof(Cars));
  181.     file.close();
  182.  
  183.     file.open("cars.dat", ios::binary | ios::out);
  184.  
  185.     cin.ignore();
  186.  
  187.     size_t counter(0);
  188.     string car_number;
  189.     bool isCorrect(false);
  190.  
  191.     cout << "Enter car number to change: ";
  192.     getline(cin, car_number);
  193.  
  194.     for (size_t i = 0; i < count; i++) {
  195.         if (car_number == cars[i].nomer) {
  196.             counter++;
  197.  
  198.             cout << "Current car number: " << cars[i].nomer << " | Car Brand: " << cars[i].marka << "\n\n";
  199.  
  200.             do {
  201.  
  202.                 cout << "Enter the new car number: ";
  203.                 getline(cin, cars[i].nomer);
  204.  
  205. #if has_regex
  206.                 if (regex_match(cars[i].nomer, static_cast<regex> ("[A-Z]{1} [0-9]{4} [A-Z]{2}")))
  207.                     for (size_t j = 0; j < count - 1; j++)
  208.                         if (cars[j].nomer == cars[i].nomer && j != i)
  209.                             cout << "\n\nThe car number must be 9 characters and UNIQUE!\n\n";
  210.                         else
  211.                             isCorrect = true;
  212. #else
  213.                 if (friends[i].nomer.length() == 9) {
  214.                     for (size_t j = 0; j < count; j++)
  215.                         if (cars[j].nomer == cars[i].nomer && j != i)
  216.                             isCorrect = true;
  217.                         else
  218.                             cout << "\n\nThe car number must be 9 characters and UNIQUE!\n\n";
  219.                 }
  220. #endif
  221.  
  222.             } while (!isCorrect);
  223.         }
  224.     }
  225.  
  226.     if (counter == 0)
  227.         cout << "There is no car with that number!";
  228.  
  229.     file.write(reinterpret_cast<char *> (&cars), count * sizeof(Cars));
  230.  
  231.     file.close();
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement