Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab_3.cpp
- #include "stdafx.h"
- #include <iostream>
- #include "MyTRow.h"
- #include "MyTTable.h"
- using namespace std;
- /*16. Метод поиска таблицы с наибольшим количеством строк;
- метод выводящий сведения о строках, по всем таблицам;
- метод перемещающий строку из одной таблицы в другую.*/
- //---------------------------------------------------
- // Инициализация глобальных переменных
- int TTable::count = 0;
- int Menu();
- int GetNumber(int, int);
- void ExitBack();
- class Processor
- {
- public:
- void Show(TTable*[], int);
- void ShowLines(TTable*[], int);
- void Move(TTable*[], int);
- void FindMax(TTable*[], int);
- };
- int main()
- {
- // Определение строк
- TRow r1("Stecenko", "Nikita"); TRow r2("Anichenko", "Vladimir"); TRow r3("Romanuta", "Maxim");
- TRow r4("Iliyn", "Denis"); TRow r5("Zaletskiy", "Evgeniy"); TRow r6("Bidnik", "Alina");
- TRow r7("Zubakin", "Maxim"); TRow r8("Kasperunas", "Igor"); TRow r9("Kulishova", "Ekaterina");
- // Определение таблиц
- TTable tableA(r1, r2, r3);
- TTable tableB(r4, r5, r6);
- TTable tableC("Lapshun", "Alexandr", "Mishenko", "Mihail", "Nevolnik", "Vladislav", "Pavlovsky", "Ivan");
- TTable tableD(r7, r8, r9);
- // Определение массива указателей на таблицы
- TTable* pTable[] = { &tableA,&tableB,&tableC,&tableD };
- Processor processor;
- int n = sizeof(pTable) / sizeof(pTable[0]);
- bool done = false; // Главный цикл
- while (!done)
- {
- switch (Menu())
- {
- case 1: processor.Show(pTable, n); break;
- case 2: processor.ShowLines(pTable, n); break;
- case 3: processor.Move(pTable, n); break;
- case 4: processor.FindMax(pTable, n); break;
- case 5: cout << "The End." << endl; done = true; break;
- }
- }
- return 0;
- }
- int Menu()// Вывод меню
- {
- int option;
- cout << "\n_______ Main Menu ____________" << endl;
- cout << "1 - Output all objects" << endl;
- cout << "2 - Output lines" << endl;
- cout << "3 - Move" << endl;
- cout << "4 - Find Max" << endl;
- cout << "5 - Exit" << endl;
- cin >> option;
- return option;
- }
- // Вывод всех таблиц
- void Processor::Show(TTable* p_table[], int k)
- {
- cout << "_______ Tables: _________" << endl;
- for (int i = 0; i<k; ++i)
- p_table[i]->Show();
- }
- void Processor::ShowLines(TTable* p_table[], int k)
- {
- int lines;
- cout << "Enter the line number: ";
- cin >> lines;
- for (int i = 0; i<k; ++i)
- p_table[i]->ShowOneLines(lines-1);
- }
- // Перемещение
- void Processor::Move(TTable* p_table[], int k)
- {
- int table1 = 0, table2 = 0, row1 = 0, row2 = 0;
- cout << "From which table (1-" << k << "):";
- cin >> table1;
- cout << "Which row with [" << table1 << "] tables (1-" << p_table[table1 - 1]->countRows << "):";
- cin >> row1;
- cout << "Which table (1-" << k << "):";
- cin >> table2;
- cout << "In which row [" << table2 << "] of the table (1-" << p_table[table2 - 1]->countRows << "):";
- cin >> row2;
- table1--;
- table2--;
- row1--;
- row2--;
- string name1 = "", surname1 = "";
- int year1 = 0;
- string name2 = "", surname2 = "";
- int year2 = 0;
- name1 = p_table[table1]->rows[row1].name;
- surname1 = p_table[table1]->rows[row1].surname;
- name2 = p_table[table2]->rows[row2].name;
- surname2 = p_table[table2]->rows[row2].name;
- cout << "Move: '";
- cout << name1 << " " << surname1 << " ";
- cout << " to Table[" << table2 + 1 << "] in row[" << row2 + 1 << "]" << endl;
- p_table[table2]->rows[row2].name = name1;
- p_table[table2]->rows[row2].surname = surname1;
- p_table[table1]->rows[row1].name = name2;
- p_table[table1]->rows[row1].surname = surname2;
- cout << "Moving successfully" << endl;
- Show(p_table, k);
- }
- // Поиск таблицы с максимальным количеством строк
- void Processor::FindMax(TTable* p_table[], int k)
- {
- cout << "__________ Find Max __________" << endl;
- int ii = 0, max = 0;
- for (int i = 0; i < k; i++)
- {
- if (p_table[i]->countRows >= max)
- {
- max = p_table[i]->countRows;
- ii = i;
- }
- }
- cout << "Max row =" << max << " in " << p_table[ii]->GetName() << ":" << endl;
- p_table[ii]->Show();
- }
- //MyTRow.h
- #pragma once
- #include <string>
- class TRow
- {
- public:
- std::string name, surname;
- // Конструктор по умолчанию
- TRow(std::string _name = "", std::string _surname = "") : name(_name), surname(_surname) {};
- // Метод отображения в текстовом режиме
- void Show()const;
- };
- //MyTRow.cpp
- #include "stdafx.h"
- #include "MyTRow.h"
- #include <iostream>
- #include <string>
- //---------------------------------------------------
- // Метод отображения в текстовом режиме
- void TRow::Show()const
- {
- std::cout << "\n|" << name << " - " << surname << "|";
- }
- //MyTTable.h
- #pragma once
- #include "MyTRow.h"
- #include <string>
- class TTable
- {
- private:
- std::string tableName;
- public:
- TRow rows[10]; // строки
- // Конструктор по умолчанию
- TTable();
- // Конструктор инициализатор
- TTable(TRow, TRow, TRow);
- // Перегруженный конструктор
- TTable(std::string n1, std::string s1, std::string n2, std::string s2, std::string n3, std::string s3, std::string n4, std::string s4);
- // Конструктор копирования
- TTable(const TTable&);
- // Деструктор
- ~TTable();
- std::string GetName()const { return this->tableName; };// Получить имя объекта
- void Show() const; // Показать в текстовом режиме
- void ShowOneLines(int lines) const; // Показать в текстовом режиме 1 строку
- public:
- static int count; //количество созданных объектов
- int countRows = 0;
- };
- //MyTTable.cpp
- #include "stdafx.h"
- #include <iostream>
- #include "MyTTable.h"
- #include <string>
- using namespace std;
- //---------------------------------------------------
- // Конструктор по умолчанию
- TTable::TTable()
- {
- // увеличиваем количество таблиц
- count++;
- // Наименование таблицы
- tableName = "Table[" + to_string(count) + "]";
- // вывод отладочного сообщения
- cout << "Default Constructor for " << tableName << endl;
- }
- // Конструктор инициализатор
- TTable::TTable(TRow _r1, TRow _r2, TRow _r3) : rows{ _r1, _r2, _r3 }
- {
- countRows += 3;
- // увеличиваем количество таблиц
- count++;
- // Наименование таблицы
- tableName = "Table[" + to_string(count) + "]";
- // вывод отладочного сообщения
- cout << "Constructor initiation for " << tableName << endl;
- }
- // Перегруженный конструктор
- TTable::TTable(string n1, string s1, string n2, string s2, string n3, string s3, string n4, string s4)
- {
- // увеличиваем количество таблиц
- count++;
- // Наименование таблицы
- tableName = "Table[" + to_string(count) + "]";
- rows[countRows].name = n1;
- rows[countRows].surname = s1;
- countRows++;
- rows[countRows].name = n2;
- rows[countRows].surname = s2;
- countRows++;
- rows[countRows].name = n3;
- rows[countRows].surname = s3;
- countRows++;
- rows[countRows].name = n4;
- rows[countRows].surname = s4;
- countRows++;
- // вывод отладочного сообщения
- cout << " Overloaded Constructor (by coordinates) for " << tableName << endl;
- }
- // Конструктор копирования
- TTable::TTable(const TTable& table)
- {
- cout << "Copy constructor for: " << table.GetName() << endl;
- tableName = table.GetName() + "(copy)";
- for (int i = 0; i <= 10; i++)
- {
- rows[i] = table.rows[i];
- }
- }
- // Деструктор
- TTable::~TTable()
- {
- cout << "Destructor for " << tableName << endl;
- }
- // Метод текстового показа объекта
- void TTable::Show()const
- {
- cout << tableName << ":";
- for (int i = 0; i < countRows; i++)
- {
- rows[i].Show();
- }
- cout << endl;
- }
- void TTable::ShowOneLines(int lines)const
- {
- cout << tableName << ":";
- rows[lines].Show();
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment