Advertisement
Garey

Flights_Kursova

Nov 10th, 2017
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <random>
  5. #include <conio.h>
  6.  
  7. using namespace std;
  8.  
  9. struct Flights {
  10.     size_t id;
  11.  
  12.     string pilot_name;
  13.  
  14.     int day_of_the_month;
  15.  
  16.     int passengers_1st_class;
  17.     int passengers_2nd_class;
  18.  
  19.     int price_1st_class;
  20.     int price_2nd_class;
  21. };
  22.  
  23. void create_file(Flights[], size_t);
  24. void edit_file(Flights[], size_t);
  25. void flights_for_a_pilot(Flights[], size_t);
  26. void all_flights(Flights[], size_t);
  27.  
  28. int stefan() {
  29.  
  30.     size_t choice, array_size;
  31.  
  32.     cout << "Number of flights: ";
  33.     cin >> array_size;
  34.  
  35.     Flights *flights = new Flights[array_size];
  36.  
  37.     do {
  38.         system("cls");
  39.         cout << "Menu: \n";
  40.         cout << "[1] Enter information about flights \n";
  41.         cout << "[2] Edit information about flights \n";
  42.         cout << "[3] Flights for a pilot \n";
  43.         cout << "[4] All flights for the month\n\n";
  44.         cout << "[0] Exit\n\n";
  45.  
  46.         cout << "Your choice:";
  47.         cin >> choice;
  48.  
  49.         switch (choice) {
  50.             case 1:
  51.                 system("cls");
  52.  
  53.                 create_file(flights, array_size);
  54.  
  55.                 break;
  56.             case 2:
  57.                 system("cls");
  58.  
  59.                 edit_file(flights, array_size);
  60.  
  61.                 cout << "\n\nFile modified successfully! -- Press any key to get back to the menu!";
  62.                 _getch();
  63.                 break;
  64.             case 3:
  65.                 system("cls");
  66.  
  67.                 flights_for_a_pilot(flights, array_size);
  68.  
  69.                 cout << "\n\nPress any key to get back to the menu!";
  70.                 _getch();
  71.                 break;
  72.             case 4:
  73.                 system("cls");
  74.                
  75.                 all_flights(flights, array_size);
  76.  
  77.                 cout << "\n\nPress any key to get back to the menu!";
  78.                 _getch();
  79.                 break;
  80.         }
  81.     } while (choice != 0);
  82.  
  83.     return 0;
  84. }
  85.  
  86. void create_file(Flights array[], size_t array_size) {
  87.  
  88.     fstream file;
  89.  
  90.     random_device rd;
  91.     mt19937 generator(rd());
  92.     uniform_int_distribution<> dist(1, 1000);
  93.  
  94.     file.open("flights.dat", ios::binary | ios::out);
  95.  
  96.     for (size_t i = 0; i < array_size; i++) {
  97.  
  98.         cin.ignore();
  99.  
  100.         array[i].id = dist(generator);
  101.  
  102.         cout << "Enter pilot's full name: ";
  103.         getline(cin, array[i].pilot_name);
  104.  
  105.         cout << "Enter flight #" << array[i].id << " day of the month: ";
  106.         cin >> array[i].day_of_the_month;
  107.  
  108.         cout << "Enter flight #" << array[i].id << " 1st class passengers: ";
  109.         cin >> array[i].passengers_1st_class;
  110.  
  111.         cout << "Enter flight #" << array[i].id << " 2nd class passengers: ";
  112.         cin >> array[i].passengers_2nd_class;
  113.  
  114.         cout << "Enter flight #" << array[i].id << " 1st class price: ";
  115.         cin >> array[i].price_1st_class;
  116.  
  117.         cout << "Enter flight #" << array[i].id << " 2nd class price: ";
  118.         cin >> array[i].price_2nd_class;
  119.     }
  120.  
  121.     file.write(reinterpret_cast<char *> (&array), array_size * sizeof(array));
  122.  
  123.     file.close();
  124. }
  125.  
  126. void edit_file(Flights array[], size_t array_size) {
  127.  
  128.     fstream file;
  129.  
  130.     file.open("flights.dat", ios::binary | ios::in);
  131.  
  132.     file.read(reinterpret_cast<char *> (&array), array_size * sizeof(array));
  133.  
  134.     size_t flight_id;
  135.  
  136.     cout << "Enter flight id to search by: ";
  137.     cin >> flight_id;
  138.  
  139.     cin.ignore();
  140.  
  141.     file.close();
  142.  
  143.     for (size_t i = 0; i < array_size; i++) {
  144.         if (array[i].id == flight_id) {
  145.             cout << "--Edit number of passengers--\n\n";
  146.  
  147.             cout << "Enter flight #" << array[i].id << " 1st class passengers: ";
  148.             cin >> array[i].passengers_1st_class;
  149.  
  150.             cout << "Enter flight #" << array[i].id << " 2nd class passengers: ";
  151.             cin >> array[i].passengers_2nd_class;
  152.         }
  153.     }
  154.  
  155.  
  156.     file.open("flights.dat", ios::binary | ios::out);
  157.  
  158.     file.write(reinterpret_cast<char *> (&array), array_size * sizeof(array));
  159.  
  160.     file.close();
  161. }
  162.  
  163. void flights_for_a_pilot(Flights array[], size_t array_size) {
  164.  
  165.     fstream file;
  166.  
  167.     string pilot_name;
  168.  
  169.     bool found = false;
  170.  
  171.     file.open("flights.dat", ios::binary | ios::in);
  172.  
  173.     file.read(reinterpret_cast<char *> (&array), array_size * sizeof(array));
  174.  
  175.     cin.ignore();
  176.  
  177.     cout << "Enter pilot name to search by: ";
  178.     getline(cin, pilot_name);
  179.  
  180.     for (size_t i = 0; i < array_size; i++) {
  181.         if (strcmp(array[i].pilot_name.c_str(), pilot_name.c_str()) == 0) {
  182.             cout << "--Information about pilot name " << pilot_name << "--\n\n";
  183.  
  184.             cout << "Flight's number: " << array[i].id << endl;
  185.             cout << "Flight's day of the month: " << array[i].day_of_the_month << endl;
  186.  
  187.             cout << "Flight's passengers 1st class: " << array[i].passengers_1st_class << endl;
  188.             cout << "Flight's passengers 2nd class: " << array[i].passengers_2nd_class << endl;
  189.  
  190.             cout << "Flight's price 1st class: " << array[i].price_1st_class << endl;
  191.             cout << "Flight's price 2nd class: " << array[i].price_2nd_class << endl << endl;
  192.  
  193.             found = !found;
  194.         }
  195.     }
  196.  
  197.     if (!found)
  198.         cout << "\nNo records found in the database!\n";
  199.  
  200.     file.close();
  201. }
  202.  
  203. void all_flights(Flights array[], size_t array_size) {
  204.  
  205.     fstream file;
  206.  
  207.     file.open("flights.dat", ios::binary | ios::in);
  208.  
  209.     file.read(reinterpret_cast<char *> (&array), array_size * sizeof(array));
  210.  
  211.     for (size_t i = 0; i < array_size; i++) {
  212.         cout << "Flight's number: " << array[i].id << endl;
  213.         cout << "Flight's pilot name: " << array[i].pilot_name << endl;
  214.  
  215.         cout << "Flight's day of the month: " << array[i].day_of_the_month << endl;
  216.         cout << "Flight's passengers 1st class: " << array[i].passengers_1st_class << endl;
  217.         cout << "Flight's passengers 2nd class: " << array[i].passengers_2nd_class << endl;
  218.  
  219.         cout << "Flight's price 1st class: " << array[i].price_1st_class << endl;
  220.         cout << "Flight's price 2nd class: " << array[i].price_2nd_class << endl << endl;
  221.     }
  222.  
  223.     file.close();
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement