Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.47 KB | None | 0 0
  1. //Main.cpp
  2. #include <iostream>
  3. #include <iomanip>
  4. #include "Game.h"
  5. #include "String.h"
  6. #include "Array.h"
  7. #include "DoubleList.h"
  8. #include "io.h"
  9.  
  10. using namespace std;
  11.  
  12. const String INVALID_OPEN = "Невозможно открыть файл!";
  13. const String INVALID_FILE = "Файл пуст!";
  14. const String INVALID_STRINGS = "В файле нет ни одной корректной строки!";
  15.  
  16. void tableLine(ofstream& out);
  17. int strToInt(char* s);
  18. void Sort(Array<Game>& ob);
  19. Game maxObject(Array<Game>& ob);
  20. Game minObject(Array<Game>& ob);
  21.  
  22. int main()
  23. {
  24.     //подключение русского языка
  25.     setlocale(LC_ALL, "ru");
  26.  
  27.     //открытие файлов ввода и вывода
  28.     ifstream in("input.txt");
  29.     ofstream out("output.txt");
  30.     ifstream din("inputGenre.txt");
  31.     ofstream dout("DevelopersList.txt");
  32.  
  33.     try
  34.     {
  35.         //проверка на открытие файлов
  36.         if (!in)
  37.         {
  38.             throw INVALID_OPEN;
  39.         }
  40.  
  41.         if (!din)
  42.         {
  43.             throw INVALID_OPEN;
  44.         }
  45.  
  46.         //создаем шаблонный массив объектов игр
  47.         Array<Game>Metacritic;
  48.         //создаем дополнительный шаблонный массив объектов игр, чтобы потом работать с ним со списком
  49.         Array<Game>Opencritic;
  50.         //счетчик номера строки
  51.         int nOfStr = 0;
  52.         //счетчик неправильных строк
  53.         int nOfErrStr = 0;
  54.        
  55.         //запускаем цикл пока файл не закончится
  56.         while(!in.eof())
  57.         {
  58.             //считываем очередную строку
  59.             ++nOfStr;
  60.             String temp = "";
  61.             in >> temp;
  62.  
  63.             //проверка на пустой файл
  64.             if (in.eof() && (temp == "") && nOfStr == 1)
  65.             {
  66.                 throw INVALID_FILE;
  67.             }
  68.            
  69.             //разделение большой строки, проверка более мелких, создание объекта и занесение в массив
  70.             try
  71.             {
  72.                 //счетчик символов "|" в строке
  73.                 unsigned int nOfSymbols = 0;
  74.                 for (int i = 0; i < temp.size(); ++i)
  75.                 {
  76.                     if (temp[i] == '|')
  77.                     {
  78.                         ++nOfSymbols;
  79.                     }
  80.                 }
  81.  
  82.                 //проверка на правильное количество делений символом '|'
  83.                 if (nOfSymbols != 4)
  84.                 {
  85.                     ++nOfErrStr;
  86.                     throw nOfStr;
  87.                 }
  88.  
  89.                 //разделяем большую строку на более мелкие по полям
  90.                 int i = 0;
  91.                 int firstLetter = i;
  92.  
  93.                 //Название
  94.                 String title = "";
  95.                 //проверка на правильность ввода названия
  96.                 for (; temp[i] != '|'; ++i)
  97.                 {
  98.                     if (temp[firstLetter] >= 'A' && temp[firstLetter] <= 'Z')
  99.                     {
  100.                         if (temp[i] >= 'A' && temp[i] <= 'Z' || temp[i] >= 'a' && temp[i] <= 'z' || temp[i] == ' '
  101.                             || temp[i] == ':' || temp[i] >= '0' && temp[i] <= '9' || temp[i] == '-')
  102.                             title.append(temp[i]);
  103.                         else
  104.                         {
  105.                             ++nOfErrStr;
  106.                             throw nOfStr;
  107.                         }
  108.                     }
  109.                     else
  110.                     {
  111.                         ++nOfErrStr;
  112.                         throw nOfStr;
  113.                     }
  114.                 }
  115.                 ++i;
  116.                 firstLetter = i;
  117.  
  118.                 //Жанр
  119.                 String genre = "";
  120.                 //проверка на правильность ввода жанра
  121.                 for (; temp[i] != '|'; ++i)
  122.                 {
  123.                     if (temp[firstLetter] >= 'A' && temp[firstLetter] <= 'Z')
  124.                     {
  125.                         if (temp[i] >= 'A' && temp[i] <= 'Z' || temp[i] >= 'a' && temp[i] <= 'z' || temp[i] == ' '
  126.                             || temp[i] == ':' || temp[i] >= '0' && temp[i] <= '9' || temp[i] == '-' || temp[i] == '/')
  127.                             genre.append(temp[i]);
  128.                         else
  129.                         {
  130.                             ++nOfErrStr;
  131.                             throw nOfStr;
  132.                         }
  133.                     }
  134.                     else
  135.                     {
  136.                         ++nOfErrStr;
  137.                         throw nOfStr;
  138.                     }
  139.                 }
  140.                 ++i;
  141.                 firstLetter = i;
  142.  
  143.                 //Разработчик
  144.                 String developer = "";
  145.                 //проверка на правильность ввода разработчика
  146.                 for (; temp[i] != '|'; ++i)
  147.                 {
  148.                     if (temp[firstLetter] >= 'A' && temp[firstLetter] <= 'Z')
  149.                     {
  150.                         if (temp[i] >= 'A' && temp[i] <= 'Z' || temp[i] >= 'a' && temp[i] <= 'z' || temp[i] == ' '
  151.                             || temp[i] == ':' || temp[i] >= '0' && temp[i] <= '9' || temp[i] == '-')
  152.                             developer.append(temp[i]);
  153.                         else
  154.                         {
  155.                             ++nOfErrStr;
  156.                             throw nOfStr;
  157.                         }
  158.                     }
  159.                     else
  160.                     {
  161.                         ++nOfErrStr;
  162.                         throw nOfStr;
  163.                     }
  164.                 }
  165.                 ++i;
  166.                 firstLetter = i;
  167.  
  168.                 //Дата
  169.                 //разбиение даты на символы
  170.                 int dayFirst = firstLetter;
  171.                 int daySecond = firstLetter + 1;
  172.                 int firstPoint = firstLetter + 2;
  173.                 int monthFirst = firstLetter + 3;
  174.                 int monthSecond = firstLetter + 4;
  175.                 int secondPoint = firstLetter + 5;
  176.                 int yearFirst = firstLetter + 6;
  177.                 int yearSecond = firstLetter + 7;
  178.                 int yearThird = firstLetter + 8;
  179.                 int yearForth = firstLetter + 9;
  180.                 String date = "";
  181.                 //проверка на правильность ввода даты
  182.                 for (; temp[i] != '|'; ++i)
  183.                 {
  184.                     if (((temp[dayFirst] >= '0' && temp[dayFirst] <='2' && temp[daySecond] >= '0' && temp[daySecond] <= '9')
  185.                     || (temp[dayFirst] == '3' && temp[daySecond] >='0' && temp[daySecond] <= '1'))
  186.                     && temp[firstPoint] == '.'
  187.                     && ((temp[monthFirst] == '0' && temp[monthSecond] >= '1' && temp[monthSecond] <= '9')
  188.                     || (temp[monthFirst] == '1' && temp[monthSecond] >= '0' && temp[monthSecond] <= '2'))
  189.                     && temp[secondPoint] == '.'
  190.                     && temp[yearFirst] >= '1' && temp[yearFirst] <= '9' && temp[yearSecond] >= '0' && temp[yearSecond] <= '9'
  191.                     && temp[yearThird] >= '0' && temp[yearThird] <= '9' && temp[yearForth] >= '0' && temp[yearForth] <= '9')
  192.                     {
  193.                         date.append(temp[i]);
  194.                     }
  195.                     else
  196.                     {
  197.                         ++nOfErrStr;
  198.                         throw nOfStr;
  199.                     }
  200.                 }
  201.                 ++i;
  202.                 firstLetter = i;
  203.  
  204.                 //Средняя оценка (СО)
  205.                 String strRating = "";
  206.                 //определяем количество разрядов в средней оценке
  207.                 int counter = 0;
  208.                 for(int j = firstLetter; temp[j] != '\0'; ++j)
  209.                 {
  210.                     ++counter;
  211.                 }
  212.                
  213.                 //проверка на правильность ввода средней оценки
  214.                 for (; temp[i] != '\0'; ++i)
  215.                 {
  216.                     if ((counter == 3 && temp[firstLetter] == '1' && temp[firstLetter + 1] == '0' && temp[firstLetter + 2] == '0')
  217.                     || (counter == 2 && temp[firstLetter] >= '1' && temp[firstLetter] <= '9' && temp[firstLetter + 1] >= '0' && temp[firstLetter + 1] <= '9')
  218.                     || (counter == 1 && temp[firstLetter] >= '0' && temp[firstLetter] <= '9'))                 
  219.                     {
  220.                         strRating.append(temp[i]);
  221.                     }
  222.                     else
  223.                     {
  224.                         ++nOfErrStr;
  225.                         throw nOfStr;
  226.                     }
  227.                 }
  228.                
  229.                 //перевод средней оценки из строки в char*
  230.                 char* cstrRating = new char[strRating.size()];
  231.                 int j = 0;
  232.                 for (; j < strRating.size(); ++j)
  233.                 {
  234.                     *(cstrRating + j) = strRating[j];
  235.                 }
  236.                 *(cstrRating + j) = '\0';
  237.  
  238.                 //перевод средней оценки из char* в int
  239.                 int rating = strToInt(cstrRating);
  240.  
  241.                 //создаем объект игры
  242.                 Game ob(title, genre, developer, date, rating);
  243.  
  244.                 //добавляем объект игры в массив
  245.                 Metacritic.pushBack(ob);
  246.             }
  247.             catch (const int & error)
  248.             {
  249.                 cerr << "Ошибка в строке " << error << "!" << endl;
  250.             }
  251.         }
  252.  
  253.         //вывод таблиц
  254.         try
  255.         {
  256.             //проверка на неправильность всех строк
  257.             if (nOfErrStr == nOfStr)
  258.             {
  259.                 throw INVALID_STRINGS;
  260.             }
  261.  
  262.             //вывод таблицы максимального и минимального объектов
  263.             out << "MAX AND MIN RATING GAMES" << endl;
  264.             tableLine(out);
  265.             out << maxObject(Metacritic) << endl;
  266.             out << minObject(Metacritic) << endl;
  267.             tableLine(out);
  268.  
  269.             //сортировка массива
  270.             Sort(Metacritic);
  271.  
  272.             //вывод таблицы отсортированного массива объектов
  273.             out << endl;
  274.             out << "SORTED GAMES ARRAY" << endl;
  275.             tableLine(out);
  276.             out << "|                Название                |           Жанр           |         Разработчик         |    Дата    | СО |" << endl;
  277.             tableLine(out);
  278.             for (int i = 0; i < Metacritic.length(); ++i)
  279.             {
  280.                 out << Metacritic[i];
  281.                 out << endl;
  282.             }
  283.             tableLine(out);
  284.  
  285.             //создаем массив объектов игр и копируем используемый для работы со списком
  286.             for(int i = 0; i < Metacritic.length(); ++i)
  287.             {
  288.                 Opencritic.pushBack(Metacritic[i]);
  289.             }
  290.  
  291.             //тест перегрузок
  292.             Metacritic[0] + Metacritic[1];
  293.             Metacritic[9] + 6;
  294.             Metacritic[8] - 10;
  295.             Metacritic[7].operator++();
  296.             Metacritic[6].operator--();
  297.  
  298.             //вывод таблицы теста перегрузок
  299.             out << endl;
  300.             out << "OVERLOAD TEST" << endl;
  301.             tableLine(out);
  302.             out << Metacritic[0] << endl;
  303.             out << Metacritic[9] << endl;
  304.             out << Metacritic[8] << endl;
  305.             out << Metacritic[7] << endl;
  306.             out << Metacritic[6] << endl;
  307.             tableLine(out);
  308.         }
  309.         catch (const String & error)
  310.         {
  311.             cerr << error << endl;
  312.         }
  313.  
  314.         //создаем шаблонный лист строковых полей разработчиков
  315.         DoubleList<String>DevelopersList;
  316.  
  317.         //читаем из файла нужный жанр
  318.         String developGenre = "";
  319.         din >> developGenre;
  320.  
  321.         //добавляем в список необходимые поля разработчиков по заданному жанру
  322.         for (int i = 0; i < Opencritic.length(); ++i)
  323.         {
  324.             if (Opencritic[i].getGenre() == developGenre)
  325.             {
  326.                 DevelopersList.addTail(Opencritic[i].getDeveloper());
  327.             }
  328.         }
  329.  
  330.         //выводим список
  331.         dout << developGenre << endl;
  332.         tableLine(dout);
  333.         DevelopersList.showFromBegin(dout);
  334.         tableLine(dout);
  335.  
  336.         //закрытие файлов
  337.         in.close();
  338.         out.close();
  339.         din.close();
  340.         dout.close();
  341.  
  342.         return 0;
  343.     }
  344.     catch (const String & error)
  345.     {
  346.         cerr << endl << error << endl;
  347.         return -1;
  348.     }
  349. }
  350.  
  351. //вывод табличной линии
  352. void tableLine(ofstream& out)
  353. {
  354.     for (int i = 0; i < 117; ++i)
  355.     {
  356.         out << '-';
  357.     }
  358.     out << endl;
  359. }
  360. //сортировка массива простыми вставками
  361. void Sort(Array<Game>& ob)
  362. {
  363.     for (int i = 0; i < ob.length(); ++i)
  364.         for (int j = i; j > 0 && ob[j - 1].getRating() < ob[j].getRating(); --j)
  365.         {
  366.             Game temp = ob[j - 1];
  367.             ob[j - 1] = ob[j];
  368.             ob[j] = temp;
  369.         }
  370. }
  371. //перевод строки в число
  372. int strToInt(char* s)
  373. {
  374.     int temp = 0; // число
  375.     int i = 0;
  376.     int sign = 0; // знак числа 0- положительное, 1 — отрицательное
  377.     if (s[i] == '-')
  378.     {
  379.         sign = 1;
  380.         i++;
  381.     }
  382.     while (s[i] >= 0x30 && s[i] <= 0x39)
  383.     {
  384.         temp = temp + (s[i] & 0x0F);
  385.         temp = temp * 10;
  386.         i++;
  387.     }
  388.     temp = temp / 10;
  389.     if (sign == 1)
  390.         temp = -temp;
  391.     return(temp);
  392. }
  393. //поиск максимального объекта в массиве
  394. Game maxObject(Array<Game>& ob)
  395. {
  396.     Game maxObj = ob[0];
  397.     for (int i = 0; i < ob.length(); ++i)
  398.     {
  399.         if (ob[i].getRating() > maxObj.getRating())
  400.             maxObj = ob[i];
  401.     }
  402.     return maxObj;
  403. }
  404. //поиск минимального объекта в массиве
  405. Game minObject(Array<Game>& ob)
  406. {
  407.     Game minObj = ob[0];
  408.     for (int i = 0; i < ob.length(); ++i)
  409.     {
  410.         if (ob[i].getRating() < minObj.getRating())
  411.             minObj = ob[i];
  412.     }
  413.     return minObj;
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement