Savelyev_Vyacheslav

6 lab c++

Oct 24th, 2020 (edited)
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib> // для использования функции exit()
  5.  
  6.  
  7. #define M 30
  8. #define N 3
  9. using namespace std;
  10.  
  11. struct AEROFLOT {
  12.     char punkt[M];
  13.     char numb[M];
  14.     char type[M];
  15. };
  16.  
  17.  
  18. int reed() {
  19.     using namespace std;
  20.  
  21.     // ifstream используется для чтения содержимого файлов.
  22.     // Мы попытаемся прочитать содержимое файла SetText.txt
  23.     ifstream inf("SetText.txt");
  24.  
  25.     // Если мы не можем открыть файл для чтения его содержимого,
  26.     if (!inf)
  27.     {
  28.         // то выводим следующее сообщение об ошибке и выполняем функцию exit()
  29.         cerr << "SetText.txt could not be opened for reading!" << endl;
  30.         exit(1);
  31.     }
  32.  
  33.     // Пока есть, что читать,
  34.     while (inf)
  35.     {
  36.         // то перемещаем то, что можем прочитать, в строку, а затем выводим эту строку на экран
  37.         string strInput;
  38.         getline(inf, strInput);
  39.         cout << strInput << endl;
  40.     }
  41.  
  42.     return 0;
  43.  
  44. }
  45.  
  46. int main()
  47. {
  48.     AEROFLOT fly[N];
  49.     int i;
  50.     int key = 0;
  51.  
  52. tryAgain:
  53.     cout << "1 - zadat massiv" << endl;
  54.     cout << "2 - vyvod massiva" << endl;
  55.     cout << "3 - sohranenie v fail" << endl;
  56.     cout << "4 - vyvod po typ plan" << endl;
  57.     cout << "5 - vyvod massiva iz faila" << endl;
  58.     cout << "6 - Exit" << endl;
  59.     cin >> key;
  60.  
  61.     if (key == 1) {
  62.  
  63.         for (i = 0; i < N; i++) {
  64.             cout << "\nSET N reis: "; cin >> fly[i].numb;
  65.             cout << "\nSET TYPE plane: "; cin >> fly[i].type;
  66.             cout << "\nSET destination "; cin >> fly[i].punkt;
  67.         }
  68.         char cur[M];
  69.         for (i = 0; i < N - 1; i++)
  70.             for (int j = i + 1; j < N; j++)
  71.                 if (strcmp(fly[i].punkt, fly[j].punkt) > 0)
  72.                 {
  73.                     strcpy_s(cur, fly[i].punkt);
  74.                     strcpy_s(fly[i].punkt, fly[j].punkt);
  75.                     strcpy_s(fly[j].punkt, cur);
  76.                 }
  77.     }
  78.  
  79.     if (key == 2) {
  80.         cout << "\n________________________________________________________\n";
  81.         for (i = 0; i < N; i++) {
  82.             cout << fly[i].punkt << "    " << fly[i].numb << "    " << fly[i].type << endl;
  83.  
  84.         }
  85.     }
  86.     char temp[M];
  87.     int count;
  88.     if (key == 3) {
  89.         using namespace std;
  90.         ofstream outf("SetText.txt");
  91.         if (!outf)
  92.         {
  93.  
  94.             cerr << "SetText.txt could not be opened for writing!" << endl;
  95.             exit(1);
  96.         }
  97.         for (i = 0; i < N; i++) {
  98.             outf << fly[i].punkt << "    " << fly[i].numb << "    " << fly[i].type << endl;
  99.         }
  100.     }
  101.     if (key == 4) {
  102.         while (strcmp(temp, "exit")) {
  103.             cout << "\nDlay vyhoda set 'exit'";
  104.             cout << "\nSpecify typ plan  : "; cin >> temp;
  105.             count = 0;
  106.             for (i = 0; i < N; i++) {
  107.                 if (strcmp(fly[i].type, temp) == 0) {
  108.                     cout << endl << fly[i].punkt << "    " << fly[i].numb << endl;
  109.                     count++;
  110.                 }
  111.             }
  112.             if (count == 0) cout << endl << " None the plan \n";
  113.         }
  114.     }
  115.  
  116.     if (key == 5) {
  117.         reed();
  118.     }
  119.     if (key == 6) {
  120.         return 0;
  121.     }
  122.  
  123.     goto tryAgain;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment