Xom9ik

Lab_4/16 var (IIl semester)

Oct 7th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.35 KB | None | 0 0
  1. //Lab_4.cpp
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include "MyTRow.h"
  5. #include "MyTTable.h"
  6. using namespace std;
  7. //---------------------------------------------------
  8. // Инициализация глобальных переменных
  9. int TTable::count = 0;
  10. int Menu();
  11. int GetNumber(int, int);
  12. void ExitBack();
  13.  
  14. class Processor
  15. {
  16. public:
  17.     void Show(TTable*[], int);
  18.     void ShowLines(TTable*[], int);
  19.     void Move(TTable*[], int);
  20.     void Plus(TTable*[], int);
  21.     void Minus(TTable*[], int);
  22.     void FindMax(TTable*[], int);
  23. };
  24.  
  25. int main()
  26. {
  27.     // Определение строк
  28.     TRow r1("Stecenko", "Nikita", 1994); TRow r2("Anichenko", "Vladimir", 1995); TRow r3("Romanuta", "Maxim", 1996);
  29.     TRow r4("Iliyn", "Denis", 1997); TRow r5("Zaletskiy", "Evgeniy", 1998); TRow r6("Bidnik", "Alina", 1999);
  30.     TRow r7("Zubakin", "Maxim", 2000); TRow r8("Kasperunas", "Igor", 2001); TRow r9("Kulishova", "Ekaterina", 2002);
  31.     // Определение таблиц
  32.     TTable tableA(r1, r2, r3);
  33.     TTable tableB(r4, r5, r6);
  34.     TTable tableC("Lapshun", "Alexandr", 2003, "Mishenko", "Mihail", 2004, "Nevolnik", "Vladislav", 2005, "Pavlovsky", "Ivan", 2006);
  35.     TTable tableD(r7, r8, r9);
  36.     // Определение массива указателей на таблицы
  37.     TTable* pTable[] = { &tableA,&tableB,&tableC,&tableD };
  38.     Processor processor;
  39.     int n = sizeof(pTable) / sizeof(pTable[0]);
  40.     bool done = false; // Главный цикл
  41.     while (!done)
  42.     {
  43.         switch (Menu())
  44.         {
  45.         case 1: processor.Show(pTable, n); break;
  46.         case 2: processor.ShowLines(pTable, n); break;
  47.         case 3: processor.Move(pTable, n); break;
  48.         case 4: processor.FindMax(pTable, n); break;
  49.         case 5: processor.Plus(pTable, n); break;
  50.         case 6: processor.Minus(pTable, n); break;
  51.         case 7: cout << "The End." << endl; done = true; break;
  52.         }
  53.     }
  54.     return 0;
  55. }
  56. int Menu()// Вывод меню
  57. {
  58.     int option;
  59.     cout << "\n_______ Main Menu ____________" << endl;
  60.     cout << "1 - Output all objects" << endl;
  61.     cout << "2 - Output lines" << endl;
  62.     cout << "3 - Move" << endl;
  63.     cout << "4 - Find Max" << endl;
  64.     cout << "5 - Addition to the year" << endl;
  65.     cout << "6 - Subtraction by year" << endl;
  66.     cout << "7 - Exit" << endl;
  67.     cin >> option;
  68.     return option;
  69. }
  70. // Вывод всех таблиц
  71. void Processor::Show(TTable* p_table[], int k)
  72. {
  73.     cout << "_______ Tables: _________" << endl;
  74.     for (int i = 0; i<k; ++i)
  75.         p_table[i]->Show();
  76. }
  77. void Processor::ShowLines(TTable* p_table[], int k)
  78. {
  79.     int lines;
  80.     cout << "Enter the line number: ";
  81.     cin >> lines;
  82.     for (int i = 0; i<k; ++i)
  83.         p_table[i]->ShowOneLines(lines - 1);
  84. }
  85. // Перемещение
  86. void Processor::Move(TTable* p_table[], int k)
  87. {
  88.     int table1 = 0, table2 = 0, row1 = 0, row2 = 0;
  89.     cout << "From which table (1-" << k << "):";
  90.     cin >> table1;
  91.     cout << "Which row with [" << table1 << "] tables (1-" << p_table[table1 - 1]->countRows << "):";
  92.     cin >> row1;
  93.     cout << "Which table (1-" << k << "):";
  94.     cin >> table2;
  95.     cout << "In which row [" << table2 << "] of the table (1-" << p_table[table2 - 1]->countRows << "):";
  96.     cin >> row2;
  97.     table1--;
  98.     table2--;
  99.     row1--;
  100.     row2--;
  101.     string name1 = "", surname1 = "";
  102.     int year1 = 0;
  103.     string name2 = "", surname2 = "";
  104.     int year2 = 0;
  105.     name1 = p_table[table1]->rows[row1].name;
  106.     surname1 = p_table[table1]->rows[row1].surname;
  107.  
  108.     name2 = p_table[table2]->rows[row2].name;
  109.     surname2 = p_table[table2]->rows[row2].name;
  110.  
  111.     cout << "Move: '";
  112.     cout << name1 << " " << surname1 << " ";
  113.  
  114.     cout << " to Table[" << table2 + 1 << "] in row[" << row2 + 1 << "]" << endl;
  115.  
  116.     p_table[table2]->rows[row2].name = name1;
  117.     p_table[table2]->rows[row2].surname = surname1;
  118.  
  119.     p_table[table1]->rows[row1].name = name2;
  120.     p_table[table1]->rows[row1].surname = surname2;
  121.  
  122.     cout << "Moving successfully" << endl;
  123.     Show(p_table, k);
  124. }
  125. // Поиск таблицы с максимальным количеством строк
  126. void Processor::FindMax(TTable* p_table[], int k)
  127. {
  128.     cout << "__________ Find Max __________" << endl;
  129.     TTable tableMax = *p_table[0];
  130.     for (int i = 1; i < k; ++i)
  131.         if (*p_table[i] > tableMax)
  132.             tableMax = *p_table[i];
  133.     cout << "Max table is " << tableMax.GetName() << endl;
  134.     tableMax.Show();
  135. }
  136. void Processor::Plus(TTable* p_table[], int k)
  137. {
  138.     int table = 0, row = 0, value = 0;
  139.     cout << "Which table (1-" << k << "):";
  140.     cin >> table;
  141.     cout << "How many years to add: ";
  142.     cin >> value;
  143.     TRow temp;
  144.     temp.year = value;
  145.     p_table[table - 1]->Plus(temp);
  146. }
  147. void Processor::Minus(TTable* p_table[], int k)
  148. {
  149.     int table = 0, row = 0, value = 0;
  150.     cout << "Which table (1-" << k << "):";
  151.     cin >> table;
  152.     cout << "How many years to take away: ";
  153.     cin >> value;
  154.     TRow temp;
  155.     temp.year = value;
  156.     p_table[table - 1]->Minus(temp);
  157. }
  158. //MyTRow.h
  159. #pragma once
  160. #include <string>
  161. class TRow
  162. {
  163. public:
  164.     std::string name, surname;
  165.     int year;
  166.     // Конструктор по умолчанию
  167.     TRow(std::string _name = "", std::string _surname = "", int _year = 0) : name(_name), surname(_surname), year(_year) {};
  168.     // Метод отображения в текстовом режиме
  169.     void Show()const;
  170.     void operator += (TRow&);
  171.     void operator -= (TRow&);
  172. };
  173. //MyTRow.cpp
  174. #include "stdafx.h"
  175. #include "MyTRow.h"
  176. #include <iostream>
  177. #include <string>
  178. //---------------------------------------------------
  179. // Метод отображения в текстовом режиме
  180. void TRow::Show()const
  181. {
  182.     std::cout << "\n|" << name << " - " << surname << " - " << year << "|";
  183. }
  184. //операция сложения
  185. void TRow::operator += (TRow &row)
  186. {
  187.     year += row.year;
  188. }
  189. //операция вычитания
  190. void TRow::operator -= (TRow &row)
  191. {
  192.     year -= row.year;
  193. }
  194. //MyTTable.h
  195. #pragma once
  196. #include "MyTRow.h"
  197. #include <array>
  198. #include <string>
  199. class TTable
  200. {
  201. private:
  202.     std::string tableName;
  203. public:
  204.     std::array<TRow, 10> rows;
  205.     // Конструктор по умолчанию
  206.     TTable();
  207.     // Конструктор инициализатор
  208.     TTable(TRow, TRow, TRow);
  209.     // Перегруженный конструктор
  210.     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);
  211.     // Конструктор копирования
  212.     TTable(const TTable&);
  213.     // Деструктор
  214.     ~TTable();
  215.     std::string GetName()const { return this->tableName; };// Получить имя объекта
  216.     void Show() const; // Показать в текстовом режиме
  217.     void ShowOneLines(int lines) const; // Показать в текстовом режиме 1 строку
  218.     static int count; //количество созданных объектов
  219.     int countRows = 0;
  220.     bool operator > (const TTable&)const;// объявление функции-операции
  221.     bool operator < (const TTable&)const;// объявление функции-операции
  222.     TTable& operator = (const TTable&); // объявление операции присваивания:
  223.     void Plus(TRow);
  224.     void Minus(TRow);
  225. };
  226. //MyTTable.cpp
  227. #include "stdafx.h"
  228. #include <iostream>
  229. #include "MyTTable.h"
  230. #include <string>
  231. using namespace std;
  232. //---------------------------------------------------
  233. // Конструктор по умолчанию
  234. TTable::TTable()
  235. {
  236.     // увеличиваем количество таблиц
  237.     count++;
  238.     // Наименование таблицы
  239.     tableName = "Table[" + to_string(count) + "]";
  240.     // вывод отладочного сообщения
  241.     cout << "Default Constructor for " << tableName << endl;
  242. }
  243. // Конструктор инициализатор
  244. TTable::TTable(TRow _r1, TRow _r2, TRow _r3) : rows{ _r1, _r2, _r3 }
  245. {
  246.     countRows += 3;
  247.     // увеличиваем количество таблиц
  248.     count++;
  249.     // Наименование таблицы
  250.     tableName = "Table[" + to_string(count) + "]";
  251.     // вывод отладочного сообщения
  252.     cout << "Constructor initiation for " << tableName << endl;
  253. }
  254. // Перегруженный конструктор
  255. 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)
  256. {
  257.     // увеличиваем количество таблиц
  258.     count++;
  259.     // Наименование таблицы
  260.     tableName = "Table[" + to_string(count) + "]";
  261.     rows[countRows].name = n1;
  262.     rows[countRows].surname = s1;
  263.     rows[countRows].year = y1;
  264.     countRows++;
  265.     rows[countRows].name = n2;
  266.     rows[countRows].surname = s2;
  267.     rows[countRows].year = y2;
  268.     countRows++;
  269.     rows[countRows].name = n3;
  270.     rows[countRows].surname = s3;
  271.     rows[countRows].year = y3;
  272.     countRows++;
  273.     rows[countRows].name = n4;
  274.     rows[countRows].surname = s4;
  275.     rows[countRows].year = y4;
  276.     countRows++;
  277.     // вывод отладочного сообщения
  278.     cout << " Overloaded Constructor (by coordinates) for " << tableName << endl;
  279. }
  280. // Конструктор копирования
  281. TTable::TTable(const TTable& table) : tableName(table.tableName + "(copy)") , countRows(table.countRows), rows(table.rows)
  282. {
  283.     cout << "Copy constructor for: " << table.GetName() << endl;
  284. }
  285. // Деструктор
  286. TTable::~TTable()
  287. {
  288.     cout << "Destructor for " << tableName << endl;
  289. }
  290. // Метод текстового показа объекта
  291. void TTable::Show()const
  292. {
  293.     cout << tableName << ":";
  294.     for (int i = 0; i < countRows; i++)
  295.     {
  296.         rows[i].Show();
  297.     }
  298.     cout << endl;
  299. }
  300. void TTable::ShowOneLines(int lines)const
  301. {
  302.     cout << tableName << ":";
  303.     rows[lines].Show();
  304.     cout << endl;
  305. }
  306. //операция сравнения
  307. bool TTable::operator > (const TTable& table)const
  308. {
  309.     if (countRows > table.countRows)
  310.         return true;
  311.     else
  312.         return false;
  313. }
  314. //операция сравнения
  315. bool TTable::operator < (const TTable& table)const
  316. {
  317.     if (countRows < table.countRows)
  318.         return true;
  319.     else
  320.         return false;
  321. }
  322. //операция присваивания
  323. TTable& TTable::operator = (const TTable& table)
  324. {
  325.     countRows = table.countRows;
  326.     tableName = table.tableName;
  327.     for (int i = 0;i<10;i++)
  328.         rows[i]=table.rows[i];
  329.     cout << "Assign operator complete for " << tableName << endl;
  330.     return *this;
  331. }
  332. //операция +=
  333. void TTable::Plus(TRow row)
  334. {
  335.     for(int i=0;i<countRows;i++)
  336.     rows[i] += row;
  337. }
  338. //операция -=
  339. void TTable::Minus(TRow row)
  340. {
  341.     for (int i = 0; i<countRows; i++)
  342.         rows[i] -= row;
  343. }
Advertisement
Add Comment
Please, Sign In to add comment