Xom9ik

Lab_3/16var (IIl semester) #2

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