Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab_4.cpp
- #include "stdafx.h"
- #include <iostream>
- #include "MyTRow.h"
- #include "MyTTable.h"
- using namespace std;
- //---------------------------------------------------
- // Инициализация глобальных переменных
- 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 Plus(TTable*[], int);
- void Minus(TTable*[], int);
- void FindMax(TTable*[], int);
- };
- int main()
- {
- // Определение строк
- TRow r1("Stecenko", "Nikita", 1994); TRow r2("Anichenko", "Vladimir", 1995); TRow r3("Romanuta", "Maxim", 1996);
- TRow r4("Iliyn", "Denis", 1997); TRow r5("Zaletskiy", "Evgeniy", 1998); TRow r6("Bidnik", "Alina", 1999);
- TRow r7("Zubakin", "Maxim", 2000); TRow r8("Kasperunas", "Igor", 2001); TRow r9("Kulishova", "Ekaterina", 2002);
- // Определение таблиц
- TTable tableA(r1, r2, r3);
- TTable tableB(r4, r5, r6);
- TTable tableC("Lapshun", "Alexandr", 2003, "Mishenko", "Mihail", 2004, "Nevolnik", "Vladislav", 2005, "Pavlovsky", "Ivan", 2006);
- 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: processor.Plus(pTable, n); break;
- case 6: processor.Minus(pTable, n); break;
- case 7: 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 - Addition to the year" << endl;
- cout << "6 - Subtraction by year" << endl;
- cout << "7 - 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;
- TTable tableMax = *p_table[0];
- for (int i = 1; i < k; ++i)
- if (*p_table[i] > tableMax)
- tableMax = *p_table[i];
- cout << "Max table is " << tableMax.GetName() << endl;
- tableMax.Show();
- }
- void Processor::Plus(TTable* p_table[], int k)
- {
- int table = 0, row = 0, value = 0;
- cout << "Which table (1-" << k << "):";
- cin >> table;
- cout << "How many years to add: ";
- cin >> value;
- TRow temp;
- temp.year = value;
- p_table[table - 1]->Plus(temp);
- }
- void Processor::Minus(TTable* p_table[], int k)
- {
- int table = 0, row = 0, value = 0;
- cout << "Which table (1-" << k << "):";
- cin >> table;
- cout << "How many years to take away: ";
- cin >> value;
- TRow temp;
- temp.year = value;
- p_table[table - 1]->Minus(temp);
- }
- //MyTRow.h
- #pragma once
- #include <string>
- class TRow
- {
- public:
- std::string name, surname;
- int year;
- // Конструктор по умолчанию
- TRow(std::string _name = "", std::string _surname = "", int _year = 0) : name(_name), surname(_surname), year(_year) {};
- // Метод отображения в текстовом режиме
- void Show()const;
- void operator += (TRow&);
- void operator -= (TRow&);
- };
- //MyTRow.cpp
- #include "stdafx.h"
- #include "MyTRow.h"
- #include <iostream>
- #include <string>
- //---------------------------------------------------
- // Метод отображения в текстовом режиме
- void TRow::Show()const
- {
- std::cout << "\n|" << name << " - " << surname << " - " << year << "|";
- }
- //операция сложения
- void TRow::operator += (TRow &row)
- {
- year += row.year;
- }
- //операция вычитания
- void TRow::operator -= (TRow &row)
- {
- year -= row.year;
- }
- //MyTTable.h
- #pragma once
- #include "MyTRow.h"
- #include <array>
- #include <string>
- class TTable
- {
- private:
- std::string tableName;
- public:
- std::array<TRow, 10> rows;
- // Конструктор по умолчанию
- TTable();
- // Конструктор инициализатор
- TTable(TRow, TRow, TRow);
- // Перегруженный конструктор
- TTable(std::string n1, std::string s1, int y1, std::string n2, std::string s2, int y2, std::string n3, std::string s3, int y3, std::string n4, std::string s4, int y4);
- // Конструктор копирования
- TTable(const TTable&);
- // Деструктор
- ~TTable();
- std::string GetName()const { return this->tableName; };// Получить имя объекта
- void Show() const; // Показать в текстовом режиме
- void ShowOneLines(int lines) const; // Показать в текстовом режиме 1 строку
- static int count; //количество созданных объектов
- int countRows = 0;
- bool operator > (const TTable&)const;// объявление функции-операции
- bool operator < (const TTable&)const;// объявление функции-операции
- TTable& operator = (const TTable&); // объявление операции присваивания:
- void Plus(TRow);
- void Minus(TRow);
- };
- //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, int y1, string n2, string s2, int y2, string n3, string s3, int y3, string n4, string s4, int y4)
- {
- // увеличиваем количество таблиц
- count++;
- // Наименование таблицы
- tableName = "Table[" + to_string(count) + "]";
- rows[countRows].name = n1;
- rows[countRows].surname = s1;
- rows[countRows].year = y1;
- countRows++;
- rows[countRows].name = n2;
- rows[countRows].surname = s2;
- rows[countRows].year = y2;
- countRows++;
- rows[countRows].name = n3;
- rows[countRows].surname = s3;
- rows[countRows].year = y3;
- countRows++;
- rows[countRows].name = n4;
- rows[countRows].surname = s4;
- rows[countRows].year = y4;
- countRows++;
- // вывод отладочного сообщения
- cout << " Overloaded Constructor (by coordinates) for " << tableName << endl;
- }
- // Конструктор копирования
- TTable::TTable(const TTable& table) : tableName(table.tableName + "(copy)") , countRows(table.countRows), rows(table.rows)
- {
- cout << "Copy constructor for: " << table.GetName() << endl;
- }
- // Деструктор
- 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;
- }
- //операция сравнения
- bool TTable::operator > (const TTable& table)const
- {
- if (countRows > table.countRows)
- return true;
- else
- return false;
- }
- //операция сравнения
- bool TTable::operator < (const TTable& table)const
- {
- if (countRows < table.countRows)
- return true;
- else
- return false;
- }
- //операция присваивания
- TTable& TTable::operator = (const TTable& table)
- {
- countRows = table.countRows;
- tableName = table.tableName;
- for (int i = 0;i<10;i++)
- rows[i]=table.rows[i];
- cout << "Assign operator complete for " << tableName << endl;
- return *this;
- }
- //операция +=
- void TTable::Plus(TRow row)
- {
- for(int i=0;i<countRows;i++)
- rows[i] += row;
- }
- //операция -=
- void TTable::Minus(TRow row)
- {
- for (int i = 0; i<countRows; i++)
- rows[i] -= row;
- }
Advertisement
Add Comment
Please, Sign In to add comment