Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab_9_Task2
- #include "stdafx.h"
- #include <string>
- #include <iostream>
- #include <locale>
- #include <fstream>
- using namespace std;
- //16. Классы таблица и строка.
- //Класс - обработчик позволяет выводить список записей таблицы, отсортированных по заданному полю.
- //Класс-строка
- class TRow
- {
- private:
- string Name;
- string Surname;
- int Year;
- public:
- TRow();
- int getYear()
- {
- return Year;
- }
- string getName()
- {
- return Name;
- }
- string getSurname()
- {
- return Surname;
- }
- void setYear(int year)
- {
- Year = year;
- }
- void setName(string name)
- {
- Name = name;
- }
- void setSurname(string surname)
- {
- Surname = surname;
- }
- ~TRow();
- };
- TRow::TRow()
- {
- cout << "Constructor class TRow" << endl;
- }
- TRow::~TRow()
- {
- cout << "Destructor class TRow" << endl;
- }
- //Класс-таблица
- class TTable
- {
- public:
- TTable();
- TTable(TRow *R, int max);
- int Max;
- int CurrentPos = 0;
- TRow *Rpointer;
- void setMax(int newMax)
- {
- Max = newMax;
- }
- int getMax()
- {
- return Max;
- }
- void addRow();
- ~TTable();
- };
- TTable::TTable()
- {
- cout << "Constructor class TTable" << endl;
- }
- TTable::TTable(TRow *R, int max)
- {
- setMax(max);
- Rpointer = R;
- }
- TTable::~TTable()
- {
- cout << "Destructor class TTable" << endl;
- }
- void TTable::addRow()
- {
- string name, surname;
- int year;
- cout << "Enter name: ";
- cin >> name;
- Rpointer[CurrentPos].setName(name);
- cout << "Enter surname: ";
- cin >> surname;
- Rpointer[CurrentPos].setSurname(surname);
- cout << "Enter birthday: ";
- cin >> year;
- Rpointer[CurrentPos].setYear(year);
- CurrentPos++;
- }
- class TabularOutput
- {//табличный вывод информации в файл и на экран
- public:
- virtual void Print(TTable *T) = 0;
- };
- class RibbonOutput
- {//вывод информации в ленточной форме в файл и на экран
- public:
- virtual void Print(TTable *T) = 0;
- };
- class TabularOutput_helper : public TabularOutput {
- public:
- virtual void TabularOutput_Print(TTable *T) = 0;
- void Print(TTable *T)
- {
- TabularOutput_Print(T);
- }
- };
- class RibbonOutput_helper : public RibbonOutput {
- public:
- virtual void RibbonOutput_Print(TTable *T) = 0;
- void Print(TTable *T)
- {
- RibbonOutput_Print(T);
- }
- };
- class TProcessor : public TabularOutput_helper, public RibbonOutput_helper
- {
- public:
- TProcessor();
- TProcessor(TTable *T);
- TTable *Tpointer;
- void sorting(int sortRow);
- void TabularOutput_Print(TTable *T)
- {
- string out = "";
- cout << "Tabular Output" << endl;
- out += "Tabular Output\n";
- cout << "Name\t" << "Surname\t" << "Birthday" << endl;
- out += "Name\tSurname\tBirthday\n";
- for (int i = 0; i < T[0].CurrentPos; i++)
- {
- out += T[0].Rpointer[i].getName() + "\t" + T[0].Rpointer[i].getSurname() + "\t";
- out += to_string(T[0].Rpointer[i].getYear()) + "\n";
- cout << T[0].Rpointer[i].getName() << "\t" << T[0].Rpointer[i].getSurname() << "\t" << T[0].Rpointer[i].getYear() << endl;
- }
- ofstream fout;
- fout.open("Tabular.txt");
- fout << out;
- fout.close();
- }
- void RibbonOutput_Print(TTable *T)
- {
- string out = "";
- out += "Ribbon Output\n";
- cout << "Ribbon Output" << endl;
- cout << "Name: ";
- out += "Name: ";
- for (int i = 0; i < T[0].CurrentPos; i++)
- {
- out += T[0].Rpointer[i].getName() + ", ";
- cout << T[0].Rpointer[i].getName() << ", ";
- }
- cout << endl;
- cout << "Surname: ";
- out += "\nSurname: ";
- for (int i = 0; i < T[0].CurrentPos; i++)
- {
- out += T[0].Rpointer[i].getSurname() + ", ";
- cout << T[0].Rpointer[i].getSurname() << ", ";
- }
- cout << endl;
- cout << "Birthday: ";
- out += "\nBirthday: ";
- for (int i = 0; i < T[0].CurrentPos; i++)
- {
- out += to_string(T[0].Rpointer[i].getYear()) + ", ";
- cout << T[0].Rpointer[i].getYear() << ", ";
- }
- cout << endl;
- out += "\n";
- ofstream fout;
- fout.open("Ribbon.txt");
- fout << out;
- fout.close();
- }
- ~TProcessor();
- };
- void TProcessor::sorting(int sortRow)
- {
- for (int i = 0; i < Tpointer[0].getMax(); i++)
- {
- for (int j = i + 1; j < Tpointer[0].getMax(); j++)
- {
- string istr = "";
- string jstr = "";
- if (sortRow == 1)
- {
- istr = Tpointer[0].Rpointer[i].getName();
- jstr = Tpointer[0].Rpointer[j].getName();
- }
- else if (sortRow == 2)
- {
- istr = Tpointer[0].Rpointer[i].getSurname();
- jstr = Tpointer[0].Rpointer[j].getSurname();
- }
- else if (sortRow == 3)
- {
- istr = Tpointer[0].Rpointer[i].getYear();
- jstr = Tpointer[0].Rpointer[j].getYear();
- }
- if (strcmp((istr.c_str()), (jstr.c_str())) > 0)
- {
- string stemp = Tpointer[0].Rpointer[i].getName(); //temp=i
- Tpointer[0].Rpointer[i].setName(Tpointer[0].Rpointer[j].getName()); //i=j
- Tpointer[0].Rpointer[j].setName(stemp); //j=temp
- stemp = Tpointer[0].Rpointer[i].getSurname(); //temp=i
- Tpointer[0].Rpointer[i].setSurname(Tpointer[0].Rpointer[j].getSurname()); //i=j
- Tpointer[0].Rpointer[j].setSurname(stemp); //j=temp
- int itemp = Tpointer[0].Rpointer[i].getYear(); //temp=i
- Tpointer[0].Rpointer[i].setYear(Tpointer[0].Rpointer[j].getYear()); //i=j
- Tpointer[0].Rpointer[j].setYear(itemp); //j=temp
- }
- }
- }
- }
- TProcessor::TProcessor()
- {
- cout << "Constructor class TProcessor" << endl;
- };
- TProcessor::TProcessor(TTable *T)
- {
- Tpointer = T;
- }
- TProcessor::~TProcessor()
- {
- cout << "Destructor class TProcessor" << endl;
- };
- int main()
- {
- int max = 0, sortRow = 0;
- string name = "", surname = "";
- int year = 0;
- cout << "Enter the number of rows: ";
- cin >> max;
- TRow *row;
- row = new TRow[max];
- TTable table(row, max);
- for (int i = 0; i < max; i++)
- {
- table.addRow();
- }
- TProcessor processor(&table);
- //(TabularOutput)processor.Print(&table);
- TabularOutput* TO = &processor;
- RibbonOutput* RO = &processor;
- RO->Print(&table); // || processor.RibbonOutput_Print(&table);
- while (1)
- {
- cout << "Sort column by column(1,2,3): ";
- cin >> sortRow;
- if (sortRow > 0 && sortRow < 4)
- {
- processor.sorting(sortRow);
- cout << "Sort by " << sortRow << " column" << endl;
- TO->Print(&table); // || processor.TabularOutput_Print(&table);
- }
- else
- cout << "Enter a number between 1 and 3" << endl;
- }
- }
Add Comment
Please, Sign In to add comment