Xom9ik

Lab_9_Task2/16 var (IIl semester)

Nov 12th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.31 KB | None | 0 0
  1. //Lab_9_Task2
  2. #include "stdafx.h"
  3. #include <string>
  4. #include <iostream>
  5. #include <locale>
  6. #include <fstream>
  7. using namespace std;
  8. //16.   Классы таблица и строка.
  9. //Класс - обработчик позволяет выводить список записей таблицы, отсортированных по заданному полю.
  10.  
  11. //Класс-строка
  12. class TRow
  13. {
  14. private:
  15.     string Name;
  16.     string Surname;
  17.     int Year;
  18. public:
  19.     TRow();
  20.     int getYear()
  21.     {
  22.         return Year;
  23.     }
  24.     string getName()
  25.     {
  26.         return Name;
  27.     }
  28.     string getSurname()
  29.     {
  30.         return Surname;
  31.     }
  32.     void setYear(int year)
  33.     {
  34.         Year = year;
  35.     }
  36.     void setName(string name)
  37.     {
  38.         Name = name;
  39.     }
  40.     void setSurname(string surname)
  41.     {
  42.         Surname = surname;
  43.     }
  44.     ~TRow();
  45. };
  46. TRow::TRow()
  47. {
  48.     cout << "Constructor class TRow" << endl;
  49. }
  50. TRow::~TRow()
  51. {
  52.     cout << "Destructor class TRow" << endl;
  53. }
  54. //Класс-таблица
  55. class TTable
  56. {
  57. public:
  58.     TTable();
  59.     TTable(TRow *R, int max);
  60.     int Max;
  61.     int CurrentPos = 0;
  62.     TRow *Rpointer;
  63.  
  64.     void setMax(int newMax)
  65.     {
  66.         Max = newMax;
  67.     }
  68.     int getMax()
  69.     {
  70.         return Max;
  71.     }
  72.     void addRow();
  73.     ~TTable();
  74. };
  75. TTable::TTable()
  76. {
  77.     cout << "Constructor class TTable" << endl;
  78. }
  79. TTable::TTable(TRow *R, int max)
  80. {
  81.     setMax(max);
  82.     Rpointer = R;
  83. }
  84. TTable::~TTable()
  85. {
  86.     cout << "Destructor class TTable" << endl;
  87. }
  88. void TTable::addRow()
  89. {
  90.     string name, surname;
  91.     int year;
  92.     cout << "Enter name: ";
  93.     cin >> name;
  94.     Rpointer[CurrentPos].setName(name);
  95.     cout << "Enter surname: ";
  96.     cin >> surname;
  97.     Rpointer[CurrentPos].setSurname(surname);
  98.     cout << "Enter birthday: ";
  99.     cin >> year;
  100.     Rpointer[CurrentPos].setYear(year);
  101.     CurrentPos++;
  102. }
  103. class TabularOutput
  104. {//табличный вывод информации в файл и на экран
  105. public:
  106.     virtual void Print(TTable *T) = 0;
  107.    
  108. };
  109. class RibbonOutput
  110. {//вывод информации в ленточной форме в файл и на экран
  111. public:
  112.     virtual void Print(TTable *T) = 0;
  113.    
  114. };
  115. class TabularOutput_helper : public TabularOutput {
  116. public:
  117.     virtual void TabularOutput_Print(TTable *T) = 0;
  118.     void Print(TTable *T)
  119.     {
  120.         TabularOutput_Print(T);
  121.     }
  122. };
  123. class RibbonOutput_helper : public RibbonOutput {
  124. public:
  125.     virtual void RibbonOutput_Print(TTable *T) = 0;
  126.     void Print(TTable *T)
  127.     {
  128.         RibbonOutput_Print(T);
  129.     }
  130. };
  131. class TProcessor : public TabularOutput_helper, public RibbonOutput_helper
  132. {
  133. public:
  134.     TProcessor();
  135.     TProcessor(TTable *T);
  136.     TTable *Tpointer;
  137.     void sorting(int sortRow);
  138.     void TabularOutput_Print(TTable *T)
  139.     {
  140.         string out = "";
  141.         cout << "Tabular Output" << endl;
  142.         out += "Tabular Output\n";
  143.         cout << "Name\t" << "Surname\t" << "Birthday" << endl;
  144.         out += "Name\tSurname\tBirthday\n";
  145.         for (int i = 0; i < T[0].CurrentPos; i++)
  146.         {
  147.             out += T[0].Rpointer[i].getName() + "\t" + T[0].Rpointer[i].getSurname() + "\t";
  148.             out += to_string(T[0].Rpointer[i].getYear()) + "\n";
  149.             cout << T[0].Rpointer[i].getName() << "\t" << T[0].Rpointer[i].getSurname() << "\t" << T[0].Rpointer[i].getYear() << endl;
  150.         }
  151.         ofstream fout;
  152.         fout.open("Tabular.txt");
  153.         fout << out;
  154.         fout.close();
  155.     }
  156.     void RibbonOutput_Print(TTable *T)
  157.     {
  158.         string out = "";
  159.         out += "Ribbon Output\n";
  160.         cout << "Ribbon Output" << endl;
  161.         cout << "Name: ";
  162.         out += "Name: ";
  163.         for (int i = 0; i < T[0].CurrentPos; i++)
  164.         {
  165.             out += T[0].Rpointer[i].getName() + ", ";
  166.             cout << T[0].Rpointer[i].getName() << ", ";
  167.         }
  168.         cout << endl;
  169.         cout << "Surname: ";
  170.         out += "\nSurname: ";
  171.         for (int i = 0; i < T[0].CurrentPos; i++)
  172.         {
  173.             out += T[0].Rpointer[i].getSurname() + ", ";
  174.             cout << T[0].Rpointer[i].getSurname() << ", ";
  175.         }
  176.         cout << endl;
  177.         cout << "Birthday: ";
  178.         out += "\nBirthday: ";
  179.         for (int i = 0; i < T[0].CurrentPos; i++)
  180.         {
  181.             out += to_string(T[0].Rpointer[i].getYear()) + ", ";
  182.             cout << T[0].Rpointer[i].getYear() << ", ";
  183.         }
  184.         cout << endl;
  185.         out += "\n";
  186.         ofstream fout;
  187.         fout.open("Ribbon.txt");
  188.         fout << out;
  189.         fout.close();
  190.     }
  191.     ~TProcessor();
  192. };
  193. void TProcessor::sorting(int sortRow)
  194. {
  195.     for (int i = 0; i < Tpointer[0].getMax(); i++)
  196.     {
  197.         for (int j = i + 1; j < Tpointer[0].getMax(); j++)
  198.         {
  199.             string istr = "";
  200.             string jstr = "";
  201.             if (sortRow == 1)
  202.             {
  203.                 istr = Tpointer[0].Rpointer[i].getName();
  204.                 jstr = Tpointer[0].Rpointer[j].getName();
  205.             }
  206.             else if (sortRow == 2)
  207.             {
  208.                 istr = Tpointer[0].Rpointer[i].getSurname();
  209.                 jstr = Tpointer[0].Rpointer[j].getSurname();
  210.             }
  211.             else if (sortRow == 3)
  212.             {
  213.                 istr = Tpointer[0].Rpointer[i].getYear();
  214.                 jstr = Tpointer[0].Rpointer[j].getYear();
  215.             }
  216.             if (strcmp((istr.c_str()), (jstr.c_str())) > 0)
  217.             {
  218.                 string stemp = Tpointer[0].Rpointer[i].getName(); //temp=i
  219.                 Tpointer[0].Rpointer[i].setName(Tpointer[0].Rpointer[j].getName()); //i=j
  220.                 Tpointer[0].Rpointer[j].setName(stemp); //j=temp
  221.  
  222.                 stemp = Tpointer[0].Rpointer[i].getSurname(); //temp=i
  223.                 Tpointer[0].Rpointer[i].setSurname(Tpointer[0].Rpointer[j].getSurname()); //i=j
  224.                 Tpointer[0].Rpointer[j].setSurname(stemp); //j=temp
  225.  
  226.                 int itemp = Tpointer[0].Rpointer[i].getYear(); //temp=i
  227.                 Tpointer[0].Rpointer[i].setYear(Tpointer[0].Rpointer[j].getYear()); //i=j
  228.                 Tpointer[0].Rpointer[j].setYear(itemp); //j=temp
  229.             }
  230.         }
  231.     }
  232. }
  233. TProcessor::TProcessor()
  234. {
  235.     cout << "Constructor class TProcessor" << endl;
  236. };
  237. TProcessor::TProcessor(TTable *T)
  238. {
  239.     Tpointer = T;
  240. }
  241. TProcessor::~TProcessor()
  242. {
  243.     cout << "Destructor class TProcessor" << endl;
  244. };
  245.  
  246. int main()
  247. {
  248.     int max = 0, sortRow = 0;
  249.     string name = "", surname = "";
  250.     int year = 0;
  251.     cout << "Enter the number of rows: ";
  252.     cin >> max;
  253.     TRow *row;
  254.     row = new TRow[max];
  255.     TTable table(row, max);
  256.     for (int i = 0; i < max; i++)
  257.     {
  258.         table.addRow();
  259.     }
  260.     TProcessor processor(&table);
  261.     //(TabularOutput)processor.Print(&table);
  262.     TabularOutput* TO = &processor;
  263.     RibbonOutput* RO = &processor;
  264.     RO->Print(&table); // || processor.RibbonOutput_Print(&table);
  265.     while (1)
  266.     {
  267.         cout << "Sort column by column(1,2,3): ";
  268.         cin >> sortRow;
  269.         if (sortRow > 0 && sortRow < 4)
  270.         {
  271.             processor.sorting(sortRow);
  272.             cout << "Sort by " << sortRow << " column" << endl;
  273.             TO->Print(&table); // || processor.TabularOutput_Print(&table);
  274.         }
  275.         else
  276.             cout << "Enter a number between 1 and 3" << endl;
  277.     }
  278. }
Add Comment
Please, Sign In to add comment