Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class Train {
  8.     string number;
  9.     string station;
  10.     string departure_time;
  11.  
  12. public:
  13.     Train()
  14.     {
  15.        
  16.     }
  17.  
  18.     Train(string number, string station, string departure_time)
  19.     {
  20.         this->number = number;
  21.         this->station = station;
  22.         this->departure_time = departure_time;
  23.     }
  24.  
  25.     string getNumber() { return number; }
  26.  
  27.     string getStation() { return station; }
  28.    
  29.     string getDeparture_time() { return departure_time; }
  30.    
  31.     void print()
  32.     {
  33.         cout << "Номер поезда:" << number << endl;
  34.         cout << "Cтанция:" << " " << station << endl;
  35.         cout << "Время отправления:" << departure_time << endl;
  36.     }
  37.    
  38.     friend istream& operator>>(istream &s, Train &tmp);
  39.     friend ostream& operator<<(ostream &s, Train &tmp);
  40.     friend ofstream& operator<<(ofstream &s, Train &tmp);
  41.  
  42. };
  43.  
  44. istream& operator>> (istream &s, Train &tmp)
  45. {
  46.     cout << "Введите номер поезда: ";
  47.     getline(s,tmp.number);
  48.     cout << "Введите станцию: ";
  49.     getline(s, tmp.station);
  50.     cout << "Введите дату отправления: ";
  51.     getline(s, tmp.departure_time);
  52.     return s;
  53. }
  54.  
  55. ostream& operator<<(ostream &s, Train &tmp)
  56. {
  57.     if (tmp.getNumber().empty())
  58.     {
  59.         s << "Элемент пуст.";
  60.         return s;
  61.     }
  62.     s << "Номер поезда: " << tmp.number << endl << "Станция: " << tmp.station << endl;
  63.     cout << "Дата отправления: " << endl;
  64.     s << tmp.departure_time << endl;
  65.     return s;
  66. }
  67.  
  68. ofstream& operator<<(ofstream &s, Train &tmp)
  69. {
  70.     s << tmp.number << endl;
  71.     s << tmp.station << endl;
  72.     s << tmp.departure_time << endl;
  73.     return s;
  74. }
  75.  
  76. template <class Tkey, class Tvalue>
  77. class AssocArray
  78. {
  79.     struct KeyValue
  80.     {
  81.         Tkey key;
  82.         Tvalue value;
  83.     };
  84.     KeyValue *arr;
  85.     int current;
  86.     int size;
  87. public:
  88.     AssocArray()
  89.     {
  90.         size = 1;
  91.         current = 0;
  92.         arr = new KeyValue[size];
  93.     }
  94.  
  95.     AssocArray(int size)
  96.     {
  97.         arr = new KeyValue[size];
  98.         this->size = size;
  99.         current = 0;
  100.     }
  101.  
  102.     ~AssocArray() { if (arr) delete[] arr; }
  103.  
  104.     void add(Tkey &key, Tvalue &value)
  105.     {
  106.         if (current == size)
  107.         {
  108.             KeyValue *temp = new KeyValue[size + 10];
  109.             for (int i = 0; i < size; i++)
  110.                 temp[i] = arr[i];
  111.             delete[] arr;
  112.             arr = temp;
  113.             arr[current].key = key;
  114.             arr[current].value = value;
  115.             size += 10;
  116.             current++;
  117.             return;
  118.         }
  119.         arr[current].key = key;
  120.         arr[current].value = value;
  121.         current++;
  122.     }
  123.  
  124.  
  125.     void print()
  126.     {
  127.         if (!current)
  128.         {
  129.             cout << "Записей не найдено!" << endl;
  130.             return;
  131.         }
  132.         for (short i = 0; i < current; i++)
  133.             cout << i + 1 << ")" << endl << arr[i].value << endl;
  134.     }
  135.  
  136.  
  137.     void save(ofstream& s)
  138.     {
  139.         for (int i = 0; i < current; i++)
  140.             s << arr[i].value;
  141.     }
  142.  
  143.     /*Tvalue find(Tkey &keyfind)
  144.     {
  145.         if (!current)
  146.         {
  147.             Tvalue *tmp = new Tvalue();
  148.             Tvalue &empty = *tmp;
  149.             return empty;
  150.         }
  151.  
  152.         for (int i = 0; i < size; i++)
  153.             if (arr[i].key == keyfind)
  154.                 return arr[i].value;
  155.         Tvalue *tmp = new Tvalue();
  156.         Tvalue empty = *tmp;
  157.         return empty;
  158.     }*/
  159.  
  160.     Tvalue& operator[](int num) {
  161.         return arr[num].value;
  162.     }
  163.  
  164. };
  165.  
  166. class Vokzal
  167. {
  168.     int count;
  169.  
  170.     AssocArray<string, Train> list;
  171.  
  172. public:
  173.  
  174.     Vokzal() { count = 0; }
  175.  
  176.     void add(string &str, Train &train) {
  177.         list.add(str, train);
  178.         count++;
  179.     }
  180.  
  181.     Train find(string &str) { return list.find(str); }
  182.  
  183.  
  184.     void menu() {
  185.         for (;;) {
  186.  
  187.             cout << "[1]Ввод данных в информационную систему" << endl;
  188.             cout << "[2]Вывод информации по всем поездам" << endl;
  189.             cout << "[3]Вывод сведений по поезду с запрошенным номером" << endl;
  190.             cout << "[4]Вывод сведений по тем поездам, которые следуют до запрошенной станции назначения" << endl;
  191.             cout << "[5]Выход из программы" << endl;
  192.             int ans = 0;
  193.             cin >> ans;
  194.             cin.get();
  195.             Train A;
  196.  
  197.             ifstream fileRead("Vokzal.txt");
  198.             if (fileRead.is_open())
  199.             {
  200.                 char filecount[7];
  201.                 fileRead.getline(filecount, 7);
  202.                 count = atoi(filecount);
  203.                 for (int i = 0; i < count; i++)
  204.                 {
  205.                     char bufnumber[50];
  206.                     char bufstation[100];
  207.                     char bufdeparture_time[50];
  208.                     fileRead.getline(bufnumber, 50);
  209.                     fileRead.getline(bufstation, 100);
  210.                     fileRead.getline(bufdeparture_time, 50);
  211.                     Train tmp(bufnumber, bufstation, bufdeparture_time);
  212.                     string name;
  213.                     add(name, tmp);
  214.                 }
  215.             }
  216.             fileRead.close();
  217.  
  218.  
  219.             string number;
  220.             system("cls");
  221.             switch (ans) {
  222.  
  223.             case 1: {
  224.                 system("cls");
  225.  
  226.                 cin >> A;
  227.                 number = A.getNumber();
  228.                 add(number, A);
  229.                 system("pause");
  230.                 system("cls");
  231.                 break;
  232.             }
  233.  
  234.  
  235.  
  236.             case 2: {
  237.  
  238.                 print();
  239.  
  240.                 system("pause");
  241.                 system("cls");
  242.                 break;
  243.             }
  244.             case 3: {
  245.  
  246.                 cout << "Введите номер поезда, информацию о котором вы хотите узнать:" << endl;
  247.                 string number;
  248.                 cin >> number;
  249.                 system("cls");
  250.                  for (int i = 0; i < count; i++) {
  251.                     if (list[i].getNumber() == number) {
  252.                         cout << list[i];
  253.                     }
  254.                 }
  255.                 /*cout << find(number) << endl;*/
  256.                 system("pause");
  257.                 system("cls");
  258.                 break;
  259.             }
  260.             case 4: {
  261.                 cout << "Введите станцию назначения для просмотра информации о поездах, следующих до неё:" << endl;
  262.                 string station;
  263.                 cin >> station;
  264.                 system("cls");
  265.                 for (int i = 0; i < count; i++) {
  266.                     if (list[i].getStation() == station) {
  267.                         cout << list[i];
  268.                     }
  269.                 }
  270.  
  271.                 system("pause");
  272.                 system("cls");
  273.                 break;
  274.             }
  275.             case 5: {
  276.                 ofstream fileSave;
  277.                 fileSave.open("Vokzal.txt");
  278.                 fileSave << count << endl;
  279.                 list.save(fileSave);
  280.                 fileSave.close();
  281.                 exit(0);
  282.                 break;
  283.             }
  284.             }
  285.  
  286.         }
  287.     }
  288.  
  289.     void print() { list.print(); }
  290. };
  291.  
  292. int main() {
  293.     setlocale(LC_ALL, "Russian");
  294.     Vokzal example;
  295.     example.menu();
  296.  
  297.     /*AssocArray <char , int> test;
  298.     char a = 'A';
  299.     int i = 4;
  300.     test.add(a,i);
  301.     cout << test.find(a)<<endl;
  302.  
  303.     a='B';
  304.     i=8;
  305.     test.add(a,i);
  306.     cout << test.find(a)<<endl;*/
  307.  
  308.     system("pause");
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement