Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- class Train {
- string number;
- string station;
- string departure_time;
- public:
- Train()
- {
- }
- Train(string number, string station, string departure_time)
- {
- this->number = number;
- this->station = station;
- this->departure_time = departure_time;
- }
- string getNumber() { return number; }
- string getStation() { return station; }
- string getDeparture_time() { return departure_time; }
- void print()
- {
- cout << "Номер поезда:" << number << endl;
- cout << "Cтанция:" << " " << station << endl;
- cout << "Время отправления:" << departure_time << endl;
- }
- friend istream& operator>>(istream &s, Train &tmp);
- friend ostream& operator<<(ostream &s, Train &tmp);
- friend ofstream& operator<<(ofstream &s, Train &tmp);
- };
- istream& operator>> (istream &s, Train &tmp)
- {
- cout << "Введите номер поезда: ";
- getline(s,tmp.number);
- cout << "Введите станцию: ";
- getline(s, tmp.station);
- cout << "Введите дату отправления: ";
- getline(s, tmp.departure_time);
- return s;
- }
- ostream& operator<<(ostream &s, Train &tmp)
- {
- if (tmp.getNumber().empty())
- {
- s << "Элемент пуст.";
- return s;
- }
- s << "Номер поезда: " << tmp.number << endl << "Станция: " << tmp.station << endl;
- cout << "Дата отправления: " << endl;
- s << tmp.departure_time << endl;
- return s;
- }
- ofstream& operator<<(ofstream &s, Train &tmp)
- {
- s << tmp.number << endl;
- s << tmp.station << endl;
- s << tmp.departure_time << endl;
- return s;
- }
- template <class Tkey, class Tvalue>
- class AssocArray
- {
- struct KeyValue
- {
- Tkey key;
- Tvalue value;
- };
- KeyValue *arr;
- int current;
- int size;
- public:
- AssocArray()
- {
- size = 1;
- current = 0;
- arr = new KeyValue[size];
- }
- AssocArray(int size)
- {
- arr = new KeyValue[size];
- this->size = size;
- current = 0;
- }
- ~AssocArray() { if (arr) delete[] arr; }
- void add(Tkey &key, Tvalue &value)
- {
- if (current == size)
- {
- KeyValue *temp = new KeyValue[size + 10];
- for (int i = 0; i < size; i++)
- temp[i] = arr[i];
- delete[] arr;
- arr = temp;
- arr[current].key = key;
- arr[current].value = value;
- size += 10;
- current++;
- return;
- }
- arr[current].key = key;
- arr[current].value = value;
- current++;
- }
- void print()
- {
- if (!current)
- {
- cout << "Записей не найдено!" << endl;
- return;
- }
- for (short i = 0; i < current; i++)
- cout << i + 1 << ")" << endl << arr[i].value << endl;
- }
- void save(ofstream& s)
- {
- for (int i = 0; i < current; i++)
- s << arr[i].value;
- }
- /*Tvalue find(Tkey &keyfind)
- {
- if (!current)
- {
- Tvalue *tmp = new Tvalue();
- Tvalue &empty = *tmp;
- return empty;
- }
- for (int i = 0; i < size; i++)
- if (arr[i].key == keyfind)
- return arr[i].value;
- Tvalue *tmp = new Tvalue();
- Tvalue empty = *tmp;
- return empty;
- }*/
- Tvalue& operator[](int num) {
- return arr[num].value;
- }
- };
- class Vokzal
- {
- int count;
- AssocArray<string, Train> list;
- public:
- Vokzal() { count = 0; }
- void add(string &str, Train &train) {
- list.add(str, train);
- count++;
- }
- Train find(string &str) { return list.find(str); }
- void menu() {
- for (;;) {
- cout << "[1]Ввод данных в информационную систему" << endl;
- cout << "[2]Вывод информации по всем поездам" << endl;
- cout << "[3]Вывод сведений по поезду с запрошенным номером" << endl;
- cout << "[4]Вывод сведений по тем поездам, которые следуют до запрошенной станции назначения" << endl;
- cout << "[5]Выход из программы" << endl;
- int ans = 0;
- cin >> ans;
- cin.get();
- Train A;
- ifstream fileRead("Vokzal.txt");
- if (fileRead.is_open())
- {
- char filecount[7];
- fileRead.getline(filecount, 7);
- count = atoi(filecount);
- for (int i = 0; i < count; i++)
- {
- char bufnumber[50];
- char bufstation[100];
- char bufdeparture_time[50];
- fileRead.getline(bufnumber, 50);
- fileRead.getline(bufstation, 100);
- fileRead.getline(bufdeparture_time, 50);
- Train tmp(bufnumber, bufstation, bufdeparture_time);
- string name;
- add(name, tmp);
- }
- }
- fileRead.close();
- string number;
- system("cls");
- switch (ans) {
- case 1: {
- system("cls");
- cin >> A;
- number = A.getNumber();
- add(number, A);
- system("pause");
- system("cls");
- break;
- }
- case 2: {
- print();
- system("pause");
- system("cls");
- break;
- }
- case 3: {
- cout << "Введите номер поезда, информацию о котором вы хотите узнать:" << endl;
- string number;
- cin >> number;
- system("cls");
- for (int i = 0; i < count; i++) {
- if (list[i].getNumber() == number) {
- cout << list[i];
- }
- }
- /*cout << find(number) << endl;*/
- system("pause");
- system("cls");
- break;
- }
- case 4: {
- cout << "Введите станцию назначения для просмотра информации о поездах, следующих до неё:" << endl;
- string station;
- cin >> station;
- system("cls");
- for (int i = 0; i < count; i++) {
- if (list[i].getStation() == station) {
- cout << list[i];
- }
- }
- system("pause");
- system("cls");
- break;
- }
- case 5: {
- ofstream fileSave;
- fileSave.open("Vokzal.txt");
- fileSave << count << endl;
- list.save(fileSave);
- fileSave.close();
- exit(0);
- break;
- }
- }
- }
- }
- void print() { list.print(); }
- };
- int main() {
- setlocale(LC_ALL, "Russian");
- Vokzal example;
- example.menu();
- /*AssocArray <char , int> test;
- char a = 'A';
- int i = 4;
- test.add(a,i);
- cout << test.find(a)<<endl;
- a='B';
- i=8;
- test.add(a,i);
- cout << test.find(a)<<endl;*/
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement