Xom9ik

Lab_3/16var (IIl semester)

Sep 24th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.58 KB | None | 0 0
  1. //Lab_3.cpp
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <string>
  5. #include <windows.h>
  6. #include "MyTRow.h"
  7. #include "MyTTable.h"
  8.  
  9. using namespace std;
  10.  
  11. /*16.   Метод поиска таблицы с наибольшим количеством строк;
  12. метод выводящий сведения о строках, по всем таблицам;
  13. метод перемещающий строку из одной таблицы в другую.*/
  14.  
  15. //Класс-обработчик
  16. class TProcessor
  17. {
  18. public:
  19.     TProcessor(TTable* T, int n)
  20.     {
  21.         cout << "Constructor class TProcessor" << endl;
  22.         TPointer = T;
  23.         numOfTables = n;
  24.     }
  25.     TTable* TPointer;
  26.     int numOfTables = 0;
  27.     void maxRow();
  28.     void moveRow(int table1, int row1, int table2, int row2);
  29.     void printRowOfAllTables(int select);
  30.     ~TProcessor()
  31.     {
  32.         cout << "Destructor class TProcessor" << endl;
  33.     }
  34. };
  35. void TProcessor::maxRow()
  36. {
  37.     int count = 0, index = 0;
  38.     for (int i = 0; i < numOfTables; i++)
  39.     {
  40.         if (count <= TPointer[i].getMax())
  41.         {
  42.             count = TPointer[i].getMax();
  43.             index = i;
  44.         }
  45.     }
  46.     cout << "Max row in Table[" << index + 1 << "]. This table:" << endl;
  47.     TPointer[index].print();
  48. }
  49. void TProcessor::moveRow(int table1, int row1, int table2, int row2)
  50. {
  51.     table1--;
  52.     table2--;
  53.     row1--;
  54.     row2--;
  55.     string name1 = "", surname1 = "";
  56.     int year1 = 0;
  57.     string name2 = "", surname2 = "";
  58.     int year2 = 0;
  59.     name1 = TPointer[table1].Rpointer[row1].getName();
  60.     surname1 = TPointer[table1].Rpointer[row1].getSurname();
  61.     year1 = TPointer[table1].Rpointer[row1].getYear();
  62.  
  63.     name2 = TPointer[table2].Rpointer[row2].getName();
  64.     surname2 = TPointer[table2].Rpointer[row2].getSurname();
  65.     year2 = TPointer[table2].Rpointer[row2].getYear();
  66.  
  67.     cout << "Move: '";
  68.     cout << name1 << " " << surname1 << " " << year1 << "'";
  69.     cout << " to Table[" << table2+1 << "] in row[" << row2+1 << "]" << endl;
  70.  
  71.     TPointer[table2].Rpointer[row2].setName(name1);
  72.     TPointer[table2].Rpointer[row2].setSurname(surname1);
  73.     TPointer[table2].Rpointer[row2].setYear(year1);
  74.  
  75.     TPointer[table1].Rpointer[row1].setName(name2);
  76.     TPointer[table1].Rpointer[row1].setSurname(surname2);
  77.     TPointer[table1].Rpointer[row1].setYear(year2);
  78.  
  79.     cout << "Moving successfully" << endl;
  80.     TPointer[table1].print();
  81.     TPointer[table2].print();
  82. }
  83. void TProcessor::printRowOfAllTables(int select)
  84. {
  85.     select--;
  86.     for (int i = 0; i < numOfTables; i++)
  87.     {
  88.         if (TPointer[i].getMax() > select)
  89.         {
  90.             cout << "Table[" << (i + 1) << "]" << endl;
  91.             cout << "Name\t" << "Surname\t" << "Birthday" << endl;
  92.             cout << TPointer[i].Rpointer[select].getName() << "\t" << TPointer[i].Rpointer[select].getSurname() << "\t" << TPointer[i].Rpointer[select].getYear() << endl;
  93.         }
  94.         else
  95.         {
  96.             cout << "Table[" << (i + 1) << "]" << endl;
  97.             cout << "No " << select + 1 << " rows" << endl;
  98.         }
  99.     }
  100. }
  101.  
  102. int main()
  103. {
  104.     int numOfTables = 0;
  105.     cout << "Enter the number of tables : ";
  106.     cin >> numOfTables;
  107.     TTable *table = new TTable[numOfTables];
  108.     for (int i = 0; i < numOfTables; i++)
  109.     {
  110.         int max = 0;
  111.         string name = "", surname = "";
  112.         int year = 0;
  113.         cout << "[" << i+1 << "] Enter the number of rows: ";
  114.         cin >> max;
  115.         TRow *row = new TRow[max];
  116.         table[i].pointer_max(row,max);
  117.         for (int j = 0; j < max; j++)
  118.         {
  119.             table[i].addRow();
  120.         }
  121.         cout << "Print table[" << i+1 << "]" << endl;
  122.         table[i].print();
  123.     }
  124.     TProcessor processor(table, numOfTables);
  125.     while (1)
  126.     {
  127.         int option = 0;
  128.         cout << "1.Finding the table with the largest number of rows." << endl;
  129.         cout << "2.Output of information about lines, for all tables." << endl;
  130.         cout << "3.Moving rows from one table to another." << endl;
  131.         cout << "4.Exit" << endl;
  132.         cin >> option;
  133.         switch (option)
  134.         {
  135.         case 1:
  136.         {
  137.             processor.maxRow();
  138.             break;
  139.         }
  140.         case 2:
  141.         {
  142.             int select = 0;
  143.             cout << "Which row to print: ";
  144.             cin >> select;
  145.             processor.printRowOfAllTables(select);
  146.             break;
  147.         }
  148.         case 3:
  149.         {
  150.             int table1 = 0, table2 = 0, row1 = 0, row2 = 0;
  151.             cout << "From which table (1-" << numOfTables << "):";
  152.             cin >> table1;
  153.             cout << "Which row with [" << table1 << "] tables (1-" << table[table1-1].getMax() << "):";
  154.             cin >> row1;
  155.             cout << "Which table (1-" << numOfTables << "):";
  156.             cin >> table2;
  157.             cout << "In which row [" << table2 << "] of the table (1-" << table[table2 - 1].getMax() << "):";
  158.             cin >> row2;
  159.             processor.moveRow(table1, row1, table2, row2);
  160.             break;
  161.         }
  162.         case 4:
  163.         {
  164.             exit(0);
  165.             break;
  166.         }
  167.         default:
  168.             cout << "Enter a number from 1 to 4" << endl;
  169.         }
  170.     }
  171.     return 0;
  172. }
  173. //MyTRow.h
  174. #pragma once
  175. #include <string>
  176. using namespace std;
  177.  
  178. //Класс-строка
  179. class TRow
  180. {
  181. private:
  182.     string Name;
  183.     string Surname;
  184.     int Year;
  185. public:
  186.     TRow();
  187.     int getYear();
  188.     string getName();
  189.     string getSurname();
  190.     void setYear(int year);
  191.     void setName(string name);
  192.     void setSurname(string surname);
  193.     ~TRow();
  194. };
  195. //MyTRow.cpp
  196. #include "stdafx.h"
  197. #include "MyTRow.h"
  198. #include <iostream>
  199. TRow::TRow()
  200. {
  201.     cout << "Constructor class TRow" << endl;
  202. }
  203. int TRow::getYear()
  204. {
  205.     return Year;
  206. }
  207. string TRow::getName()
  208. {
  209.     return Name;
  210. }
  211. string TRow::getSurname()
  212. {
  213.     return Surname;
  214. }
  215. void TRow::setYear(int year)
  216. {
  217.     Year = year;
  218. }
  219. void TRow::setName(string name)
  220. {
  221.     Name = name;
  222. }
  223. void TRow::setSurname(string surname)
  224. {
  225.     Surname = surname;
  226. }
  227. TRow::~TRow()
  228. {
  229.     cout << "Destructor class TRow" << endl;
  230. }
  231. //MyTTable.h
  232. #pragma once
  233. #include "MyTRow.h"
  234. //Класс-таблица
  235. class TTable
  236. {
  237. public:
  238.     TTable();
  239.     int Max;
  240.     int CurrentPos = 0;
  241.     TRow *Rpointer;
  242.     int getMax();
  243.     void pointer_max(TRow *R, int max);
  244.     void addRow();
  245.     void print();
  246.     ~TTable();
  247. };
  248. //MyTTable.cpp
  249. #include "stdafx.h"
  250. #include "MyTTable.h"
  251. #include <iostream>
  252.  
  253. TTable::TTable()
  254. {
  255.     cout << "Constructor class TTable" << endl;
  256. }
  257. int TTable::getMax()
  258. {
  259.     return Max;
  260. }
  261. void TTable::pointer_max(TRow *R, int max)
  262. {
  263.     Max = max;
  264.     Rpointer = R;
  265. }
  266. void TTable::addRow()
  267. {
  268.     string name, surname;
  269.     int year;
  270.     cout << "Enter name: ";
  271.     cin >> name;
  272.     Rpointer[CurrentPos].setName(name);
  273.     cout << "Enter surname: ";
  274.     cin >> surname;
  275.     Rpointer[CurrentPos].setSurname(surname);
  276.     cout << "Enter birthday: ";
  277.     cin >> year;
  278.     Rpointer[CurrentPos].setYear(year);
  279.     CurrentPos++;
  280. }
  281. void TTable::print()
  282. {
  283.     cout << "Name\t" << "Surname\t" << "Birthday" << endl;
  284.     for (int i = 0; i < CurrentPos; i++)
  285.     {
  286.         cout << Rpointer[i].getName() << "\t" << Rpointer[i].getSurname() << "\t" << Rpointer[i].getYear() << endl;
  287.     }
  288.     int i = Rpointer[0].getYear();
  289. }
  290. TTable::~TTable()
  291. {
  292.     cout << "Destructor class TTable" << endl;
  293. }
Add Comment
Please, Sign In to add comment