Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.09 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.     void printElemKey(Tkey &keyfind)
  144.     {
  145.        
  146.         bool flag = false;
  147.         for (int i = 0; i < size; i++)
  148.            
  149.             if (arr[i].key == keyfind)
  150.                 {
  151.                     cout << arr[i].value<<endl;
  152.                     flag = true;
  153.             }
  154.             if(!flag)
  155.                 cout<<"Элемент пуст"<<endl;
  156.        
  157.     }
  158.    
  159.     /*KeyValue* find(Tkey &keyfind)
  160.     {
  161.    
  162.         KeyValue *temp = new KeyValue[size + 10];
  163.             for (int i = 0; i < size; i++)
  164.             {
  165.                     if(arr[i].key == keyfind)
  166.                         {
  167.                             temp[i].value = arr[i].value;
  168.                     }
  169.             }
  170.             delete[] arr;
  171.         return temp;
  172.     }*/
  173.  
  174.     Tvalue& operator[](int num) {
  175.         return arr[num].value;
  176.     }
  177.  
  178. };
  179.  
  180. class RailwayStation
  181. {
  182.     int count;
  183.  
  184.     AssocArray<string, Train> list;
  185.  
  186. public:
  187.  
  188.     RailwayStation() { count = 0; }
  189.  
  190.     void add(string &str, Train &train) {
  191.         list.add(str, train);
  192.         count++;
  193.     }
  194.  
  195.         void printElemKey(string &str) {
  196.              list.printElemKey(str);
  197.         }
  198.  
  199.  
  200.     void menu() {
  201.         for (;;) {
  202.  
  203.             cout << "[1]Ввод данных в информационную систему" << endl;
  204.             cout << "[2]Вывод информации по всем поездам" << endl;
  205.             cout << "[3]Вывод сведений по поезду с запрошенным номером" << endl;
  206.             cout << "[4]Вывод сведений по тем поездам, которые следуют до запрошенной станции назначения" << endl;
  207.             cout << "[5]Выход из программы" << endl;
  208.             int ans = 0;
  209.             cin >> ans;
  210.             cin.get();
  211.             Train A;
  212.  
  213.             ifstream fileRead("Vokzal.txt");
  214.             if (fileRead.is_open())
  215.             {
  216.                 char filecount[7];
  217.                 fileRead.getline(filecount, 7);
  218.                 count = atoi(filecount);
  219.                 for (int i = 0; i < count; i++)
  220.                 {
  221.                     char bufnumber[50];
  222.                     char bufstation[100];
  223.                     char bufdeparture_time[50];
  224.                     fileRead.getline(bufnumber, 50);
  225.                     fileRead.getline(bufstation, 100);
  226.                     fileRead.getline(bufdeparture_time, 50);
  227.                     Train tmp(bufnumber, bufstation, bufdeparture_time);
  228.                     string name;
  229.                     add(name, tmp);
  230.                 }
  231.             }
  232.             fileRead.close();
  233.  
  234.  
  235.            
  236.             system("cls");
  237.             switch (ans) {
  238.  
  239.             case 1: {
  240.                 system("cls");
  241.                 string number;
  242.                 cin >> A;
  243.                 number = A.getNumber();
  244.                 add(number, A);
  245.                 system("pause");
  246.                 system("cls");
  247.                 break;
  248.             }
  249.  
  250.  
  251.  
  252.             case 2: {
  253.  
  254.                 print();
  255.  
  256.                 system("pause");
  257.                 system("cls");
  258.                 break;
  259.             }
  260.             case 3: {
  261.  
  262.                 cout << "Введите номер поезда, информацию о котором вы хотите узнать:" << endl;
  263.                 string number;
  264.                 cin >> number;
  265.                 system("cls");
  266.                 /* for (int i = 0; i < count; i++) {
  267.                     if (list[i].getNumber() == number) {
  268.                         cout << list[i];
  269.                     }
  270.                 }*/
  271.                 /*cout << find(number) << endl;*/
  272.                 printElemKey(number);
  273.                 system("pause");
  274.                 system("cls");
  275.                 break;
  276.             }
  277.             case 4: {
  278.                 cout << "Введите станцию назначения для просмотра информации о поездах, следующих до неё:" << endl;
  279.                 string station;
  280.                 cin >> station;
  281.                 system("cls");
  282.                 for (int i = 0; i < count; i++) {
  283.                     if (list[i].getStation() == station) {
  284.                         cout << list[i];
  285.                     }
  286.                 }
  287.  
  288.                 system("pause");
  289.                 system("cls");
  290.                 break;
  291.             }
  292.             case 5: {
  293.                 ofstream fileSave;
  294.                 fileSave.open("Vokzal.txt");
  295.                 fileSave << count << endl;
  296.                 list.save(fileSave);
  297.                 fileSave.close();
  298.                 exit(0);
  299.                 break;
  300.             }
  301.             }
  302.  
  303.         }
  304.     }
  305.  
  306.     void print() { list.print(); }
  307. };
  308.  
  309. int main() {
  310.     setlocale(LC_ALL, "Russian");
  311.     RailwayStation example;
  312.     example.menu();
  313.  
  314.     /*AssocArray <char , int> test;
  315.     char a = 'A';
  316.     int i = 4;
  317.     test.add(a,i);
  318.     cout << test[i]<<endl;
  319.  
  320.     a='B';
  321.     i=8;
  322.     test.add(a,i);
  323.     cout << test[a]<<endl;*/
  324.  
  325.     system("pause");
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement