Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Весь код писал сам, List самодельный! (на шаблонах, т.к. для каждого задания очень много писать с этими списками)
- Немного не успел доделать задуманное, подсписки должны были быть реализованными
- у дочернего класса List для списка абонентов, сейчас у самого List'а
- */
- #include <iostream>
- #include <string>
- #include <chrono>
- using namespace std;
- //элемент списка с любым типом ключа для сортировки
- template <typename KEY>
- class ListElement
- {
- public:
- ListElement() { }
- ListElement(KEY k)
- {
- key = k;
- next = nullptr;
- }
- virtual void print()
- {
- cout << "Ключ: " << key << endl;
- }
- KEY key; //ключ
- ListElement<KEY>* next; //следующий
- };
- //звонки, наследуют ListElement
- class Call : public ListElement<string>
- {
- public:
- Call(string k, int t)
- {
- key = k;
- duration = t;
- price = PRICE_PER_MINUTE * (t / 60);
- timestamp = chrono::system_clock::now();
- }
- void print()
- {
- cout << "--> Телефон: " << key << ", ";
- cout << "Время: " << duration / 3600 << " часов " << duration / 60 << " минут " << duration % 60 << " секунд, ";
- cout << "Цена: " << price << " рублей." << endl;
- }
- Call* next;
- protected:
- chrono::system_clock::time_point timestamp; //метка времени (конец разговора в UNIX TIMESTAMP)
- int duration; //длительность в секундах
- double price; //цена звонка
- const double PRICE_PER_MINUTE = 10.6; //цена за минуту
- };
- //односвязный список, совместимый с ListElement
- template <typename T>
- class List
- {
- public:
- List() : begin(nullptr), end(nullptr) { }
- void addToBack(T* temp)
- {
- if (!begin)
- {
- begin = temp;
- end = temp;
- return;
- }
- if (begin == end)
- {
- end = temp;
- begin->next = end;
- return;
- }
- end->next = temp;
- end = temp;
- }
- void sortASC()
- {
- if (!begin && begin == end)
- {
- cout << "В этом списке слишком мало элементов для сортировки" << endl;
- return;
- }
- T* root = begin;
- T* new_root = nullptr;
- while (root)
- {
- T* node = root;
- root = root->next;
- if (!new_root || node->key < new_root->key)
- {
- node->next = new_root;
- new_root = node;
- }
- else
- {
- T* current = new_root;
- while (current->next && !(node->key < current->next->key))
- {
- current = current->next;
- }
- node->next = current->next;
- current->next = node;
- }
- }
- delete root;
- begin = new_root;
- }
- T* findByKey(string key)
- {
- T* temp = begin;
- while (temp)
- {
- if (temp->key == key) return temp;
- temp = temp->next;
- }
- delete temp;
- }
- void printASC()
- {
- if (!this) return;
- T* temp = begin;
- while (temp)
- {
- temp->print();
- temp = temp->next;
- }
- delete temp;
- }
- T* begin;
- T* end;
- };
- //абоненты, наследуют ListElement
- class Abonent : public ListElement<string>
- {
- public:
- Abonent(string k, string p) : subList(nullptr)
- {
- key = k;
- personalInfo = p;
- }
- void print()
- {
- cout << "Номер: " << key << ", ";
- cout << "ФИО: " << personalInfo << endl;
- subList->printASC();
- }
- void addToSubList(Call* el)
- {
- if (!subList)
- subList = new List<Call>;
- subList->addToBack(el);
- }
- List<Call>* subList; //список звонков
- Abonent* next;
- protected:
- string personalInfo; //ФИО
- };
- class Menu
- {
- public:
- bool isAlwaysRender;
- Menu(bool r, List<Abonent>*& abonents) : isAlwaysRender(r)
- {
- if (isAlwaysRender)
- while (isAlwaysRender) render(abonents);
- else
- render(abonents);
- }
- protected:
- void render(List<Abonent>*& abonents)
- {
- cout << endl << "****" << endl;
- cout << "Вывести все номера: 1" << endl;
- cout << "Ввести разговор: 2" << endl;
- cout << "Отсортировать: 3" << endl;
- cout << "Выйти из меню: 999" << endl;
- cout << "Ожидание ввода..." << endl;
- userPick(abonents);
- }
- void userPick(List<Abonent>*& abonents)
- {
- string input;
- cin >> input;
- int cmd = atoi(input.c_str());
- if (!cmd)
- {
- cout << "Команда должна быть числом!" << endl;
- return;
- }
- switch (cmd)
- {
- case 1:
- abonents->printASC();
- break;
- case 2:
- {
- cout << "Введите существующий номер и время!" << endl;
- string num; cin >> num;
- int t; cin >> t;
- Call* c = new Call(num, t);
- Abonent* temp = abonents->findByKey(c->key);
- if (temp)
- temp->addToSubList(c);
- break;
- }
- case 3:
- abonents->sortASC();
- break;
- case 999:
- isAlwaysRender = false;
- break;
- default:
- cout << "Такой команды нет!" << endl;
- break;
- }
- }
- };
- int main()
- {
- setlocale(0, "rus");
- List<Abonent>* abonents = new List<Abonent>();
- Abonent* a1 = new Abonent("8999", "Обуховский Б.И.");
- Abonent* a2 = new Abonent("7774", "Кукуев И.О.");
- Abonent* a3 = new Abonent("8997", "Улюкаев И.О.");
- Abonent* a4 = new Abonent("8996", "Забоев И.О.");
- Abonent* a5 = new Abonent("7775", "Кабуркин И.О.");
- Abonent* a6 = new Abonent("7300", "Кукуев И.О.");
- Abonent* a7 = new Abonent("7330", "Кукуев И.О.");
- abonents->addToBack(a1);
- abonents->addToBack(a2);
- abonents->addToBack(a3);
- abonents->addToBack(a4);
- abonents->addToBack(a5);
- abonents->addToBack(a6);
- abonents->addToBack(a7);
- Menu* menu = new Menu(true, abonents);
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment