Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <Windows.h>
- using namespace std;
- class TrainStack {
- private:
- struct Node {
- int num;
- Node *next;
- };
- Node *top = NULL;
- public:
- void push(int num) {
- Node *pv = new Node;
- pv->num = num;
- pv->next = top;
- top = pv;
- }
- int pop() {
- if (isEmpty()) {
- return -1;
- }
- int temp = top->num;
- Node *pv = top;
- top = top->next;
- delete pv;
- return temp;
- }
- bool isEmpty() {
- return top ? false : true;
- }
- void print() {
- Node *temp = top;
- while (temp != NULL) {
- cout << temp->num << ' ';
- temp = temp->next;
- }
- cout << endl;
- }
- ~TrainStack() {
- while (top) {
- Node *pv = top;
- top = top->next;
- delete pv;
- }
- }
- };
- short menu();
- void input_by_file(TrainStack&, TrainStack&, TrainStack&);
- void input_by_keyboard(TrainStack&, TrainStack&, TrainStack&);
- void input(TrainStack&, TrainStack&);
- void print_trains(TrainStack&, TrainStack&);
- void print_trains(TrainStack&);
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- TrainStack MainDirection, oneDirection, twoDirection;
- short choice;
- do {
- system("cls");
- choice = menu();
- switch (choice) {
- case 1:
- system("cls");
- input_by_keyboard(MainDirection, oneDirection, twoDirection);
- system("pause");
- system("cls");
- break;
- case 2:
- system("cls");
- input_by_file(MainDirection, oneDirection, twoDirection);
- system("pause");
- system("cls");
- break;
- case 3:
- system("cls");
- print_trains(oneDirection, twoDirection);
- system("pause");
- system("cls");
- break;
- case 0:
- cout << "До свидания!" << endl;
- system("pause");
- return 0;
- break;
- default:
- cout << "Неверный выбор!" << endl;
- system("pause");
- system("cls");
- break;
- }
- } while (choice);
- cout << endl;
- return 0;
- }
- void input(int value, TrainStack& one, TrainStack& two) {
- if (value % 2 == 0) {
- one.push(value);
- }
- else {
- two.push(value);
- }
- }
- void input_by_keyboard(TrainStack& main, TrainStack& one, TrainStack& two) {
- int train;
- cout << "Вводите номера поездов, для остановки ввода напишите строку: \n";
- while (cin >> train) {
- main.push(train);
- }
- // Если пользователь написал строку, вместо числа, очищаем буфер cin
- cin.clear();
- while (cin.get() != '\n');
- print_trains(main);
- while (!main.isEmpty()) {
- int pr = main.pop();
- input(pr, one, two);
- }
- print_trains(one, two);
- }
- void input_by_file(TrainStack& main, TrainStack& one, TrainStack& two) {
- char filename[30];
- cout << "Введите название Вашего текстового файла для чтения: " << endl;
- cin >> filename;
- strcat_s(filename, ".txt");
- system("cls");
- ifstream in(filename, ios::in);
- if (!in) {
- cout << "Неудается открыть файл " << filename << " для чтения" << endl;
- return;
- }
- while (!in.eof()) {
- int pr;
- if (in >> pr) {
- main.push(pr);
- }
- else {
- in.clear();
- in.ignore(1, '\n');
- }
- }
- print_trains(main);
- while (!main.isEmpty()) {
- int pr = main.pop();
- input(pr, one, two);
- }
- in.close();
- print_trains(one, two);
- }
- void print_trains(TrainStack& one, TrainStack& two) {
- if (!one.isEmpty()) {
- cout << "Четное направление:" << endl;
- one.print();
- cout << endl;
- }
- else
- cout << "Четное направление пусто!" << endl << endl;
- if (!two.isEmpty()) {
- cout << "Нечетное направление:" << endl;
- two.print();
- cout << endl;
- }
- else
- cout << "Нечетное направление пусто!" << endl << endl;
- }
- void print_trains(TrainStack& main) {
- if (!main.isEmpty()) {
- cout << "Главное направление:" << endl;
- main.print();
- cout << endl;
- }
- else
- cout << "Главное направление пусто!" << endl << endl;
- }
- short menu() {
- short v;
- cout << "\n МЕНЮ" << endl;
- cout << " Введите [1] для формирования состава с клавиатуры\n";
- cout << " Введите [2] для формирования состава из файла\n";
- cout << " Введите [3] для просмотра отсортированных составов\n";
- cout << " Введите [0] для выхода\n";
- cout << " Пункт: ";
- cin.clear();
- cin >> v;
- return v;
- }
Add Comment
Please, Sign In to add comment