Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Main.cpp
- #include <iostream>
- #include <iomanip>
- #include "Game.h"
- #include "String.h"
- #include "Array.h"
- #include "DoubleList.h"
- #include "io.h"
- using namespace std;
- const String INVALID_OPEN = "Невозможно открыть файл!";
- const String INVALID_FILE = "Файл пуст!";
- const String INVALID_STRINGS = "В файле нет ни одной корректной строки!";
- void tableLine(ofstream& out);
- int strToInt(char* s);
- void Sort(Array<Game>& ob);
- Game maxObject(Array<Game>& ob);
- Game minObject(Array<Game>& ob);
- int main()
- {
- //подключение русского языка
- setlocale(LC_ALL, "ru");
- //открытие файлов ввода и вывода
- ifstream in("input.txt");
- ofstream out("output.txt");
- ifstream din("inputGenre.txt");
- ofstream dout("DevelopersList.txt");
- try
- {
- //проверка на открытие файлов
- if (!in)
- {
- throw INVALID_OPEN;
- }
- if (!din)
- {
- throw INVALID_OPEN;
- }
- //создаем шаблонный массив объектов игр
- Array<Game>Metacritic;
- //создаем дополнительный шаблонный массив объектов игр, чтобы потом работать с ним со списком
- Array<Game>Opencritic;
- //счетчик номера строки
- int nOfStr = 0;
- //счетчик неправильных строк
- int nOfErrStr = 0;
- //запускаем цикл пока файл не закончится
- while(!in.eof())
- {
- //считываем очередную строку
- ++nOfStr;
- String temp = "";
- in >> temp;
- //проверка на пустой файл
- if (in.eof() && (temp == "") && nOfStr == 1)
- {
- throw INVALID_FILE;
- }
- //разделение большой строки, проверка более мелких, создание объекта и занесение в массив
- try
- {
- //счетчик символов "|" в строке
- unsigned int nOfSymbols = 0;
- for (int i = 0; i < temp.size(); ++i)
- {
- if (temp[i] == '|')
- {
- ++nOfSymbols;
- }
- }
- //проверка на правильное количество делений символом '|'
- if (nOfSymbols != 4)
- {
- ++nOfErrStr;
- throw nOfStr;
- }
- //разделяем большую строку на более мелкие по полям
- int i = 0;
- int firstLetter = i;
- //Название
- String title = "";
- //проверка на правильность ввода названия
- for (; temp[i] != '|'; ++i)
- {
- if (temp[firstLetter] >= 'A' && temp[firstLetter] <= 'Z')
- {
- if (temp[i] >= 'A' && temp[i] <= 'Z' || temp[i] >= 'a' && temp[i] <= 'z' || temp[i] == ' '
- || temp[i] == ':' || temp[i] >= '0' && temp[i] <= '9' || temp[i] == '-')
- title.append(temp[i]);
- else
- {
- ++nOfErrStr;
- throw nOfStr;
- }
- }
- else
- {
- ++nOfErrStr;
- throw nOfStr;
- }
- }
- ++i;
- firstLetter = i;
- //Жанр
- String genre = "";
- //проверка на правильность ввода жанра
- for (; temp[i] != '|'; ++i)
- {
- if (temp[firstLetter] >= 'A' && temp[firstLetter] <= 'Z')
- {
- if (temp[i] >= 'A' && temp[i] <= 'Z' || temp[i] >= 'a' && temp[i] <= 'z' || temp[i] == ' '
- || temp[i] == ':' || temp[i] >= '0' && temp[i] <= '9' || temp[i] == '-' || temp[i] == '/')
- genre.append(temp[i]);
- else
- {
- ++nOfErrStr;
- throw nOfStr;
- }
- }
- else
- {
- ++nOfErrStr;
- throw nOfStr;
- }
- }
- ++i;
- firstLetter = i;
- //Разработчик
- String developer = "";
- //проверка на правильность ввода разработчика
- for (; temp[i] != '|'; ++i)
- {
- if (temp[firstLetter] >= 'A' && temp[firstLetter] <= 'Z')
- {
- if (temp[i] >= 'A' && temp[i] <= 'Z' || temp[i] >= 'a' && temp[i] <= 'z' || temp[i] == ' '
- || temp[i] == ':' || temp[i] >= '0' && temp[i] <= '9' || temp[i] == '-')
- developer.append(temp[i]);
- else
- {
- ++nOfErrStr;
- throw nOfStr;
- }
- }
- else
- {
- ++nOfErrStr;
- throw nOfStr;
- }
- }
- ++i;
- firstLetter = i;
- //Дата
- //разбиение даты на символы
- int dayFirst = firstLetter;
- int daySecond = firstLetter + 1;
- int firstPoint = firstLetter + 2;
- int monthFirst = firstLetter + 3;
- int monthSecond = firstLetter + 4;
- int secondPoint = firstLetter + 5;
- int yearFirst = firstLetter + 6;
- int yearSecond = firstLetter + 7;
- int yearThird = firstLetter + 8;
- int yearForth = firstLetter + 9;
- String date = "";
- //проверка на правильность ввода даты
- for (; temp[i] != '|'; ++i)
- {
- if (((temp[dayFirst] >= '0' && temp[dayFirst] <='2' && temp[daySecond] >= '0' && temp[daySecond] <= '9')
- || (temp[dayFirst] == '3' && temp[daySecond] >='0' && temp[daySecond] <= '1'))
- && temp[firstPoint] == '.'
- && ((temp[monthFirst] == '0' && temp[monthSecond] >= '1' && temp[monthSecond] <= '9')
- || (temp[monthFirst] == '1' && temp[monthSecond] >= '0' && temp[monthSecond] <= '2'))
- && temp[secondPoint] == '.'
- && temp[yearFirst] >= '1' && temp[yearFirst] <= '9' && temp[yearSecond] >= '0' && temp[yearSecond] <= '9'
- && temp[yearThird] >= '0' && temp[yearThird] <= '9' && temp[yearForth] >= '0' && temp[yearForth] <= '9')
- {
- date.append(temp[i]);
- }
- else
- {
- ++nOfErrStr;
- throw nOfStr;
- }
- }
- ++i;
- firstLetter = i;
- //Средняя оценка (СО)
- String strRating = "";
- //определяем количество разрядов в средней оценке
- int counter = 0;
- for(int j = firstLetter; temp[j] != '\0'; ++j)
- {
- ++counter;
- }
- //проверка на правильность ввода средней оценки
- for (; temp[i] != '\0'; ++i)
- {
- if ((counter == 3 && temp[firstLetter] == '1' && temp[firstLetter + 1] == '0' && temp[firstLetter + 2] == '0')
- || (counter == 2 && temp[firstLetter] >= '1' && temp[firstLetter] <= '9' && temp[firstLetter + 1] >= '0' && temp[firstLetter + 1] <= '9')
- || (counter == 1 && temp[firstLetter] >= '0' && temp[firstLetter] <= '9'))
- {
- strRating.append(temp[i]);
- }
- else
- {
- ++nOfErrStr;
- throw nOfStr;
- }
- }
- //перевод средней оценки из строки в char*
- char* cstrRating = new char[strRating.size()];
- int j = 0;
- for (; j < strRating.size(); ++j)
- {
- *(cstrRating + j) = strRating[j];
- }
- *(cstrRating + j) = '\0';
- //перевод средней оценки из char* в int
- int rating = strToInt(cstrRating);
- //создаем объект игры
- Game ob(title, genre, developer, date, rating);
- //добавляем объект игры в массив
- Metacritic.pushBack(ob);
- }
- catch (const int & error)
- {
- cerr << "Ошибка в строке " << error << "!" << endl;
- }
- }
- //вывод таблиц
- try
- {
- //проверка на неправильность всех строк
- if (nOfErrStr == nOfStr)
- {
- throw INVALID_STRINGS;
- }
- //вывод таблицы максимального и минимального объектов
- out << "MAX AND MIN RATING GAMES" << endl;
- tableLine(out);
- out << maxObject(Metacritic) << endl;
- out << minObject(Metacritic) << endl;
- tableLine(out);
- //сортировка массива
- Sort(Metacritic);
- //вывод таблицы отсортированного массива объектов
- out << endl;
- out << "SORTED GAMES ARRAY" << endl;
- tableLine(out);
- out << "| Название | Жанр | Разработчик | Дата | СО |" << endl;
- tableLine(out);
- for (int i = 0; i < Metacritic.length(); ++i)
- {
- out << Metacritic[i];
- out << endl;
- }
- tableLine(out);
- //создаем массив объектов игр и копируем используемый для работы со списком
- for(int i = 0; i < Metacritic.length(); ++i)
- {
- Opencritic.pushBack(Metacritic[i]);
- }
- //тест перегрузок
- Metacritic[0] + Metacritic[1];
- Metacritic[9] + 6;
- Metacritic[8] - 10;
- Metacritic[7].operator++();
- Metacritic[6].operator--();
- //вывод таблицы теста перегрузок
- out << endl;
- out << "OVERLOAD TEST" << endl;
- tableLine(out);
- out << Metacritic[0] << endl;
- out << Metacritic[9] << endl;
- out << Metacritic[8] << endl;
- out << Metacritic[7] << endl;
- out << Metacritic[6] << endl;
- tableLine(out);
- }
- catch (const String & error)
- {
- cerr << error << endl;
- }
- //создаем шаблонный лист строковых полей разработчиков
- DoubleList<String>DevelopersList;
- //читаем из файла нужный жанр
- String developGenre = "";
- din >> developGenre;
- //добавляем в список необходимые поля разработчиков по заданному жанру
- for (int i = 0; i < Opencritic.length(); ++i)
- {
- if (Opencritic[i].getGenre() == developGenre)
- {
- DevelopersList.addTail(Opencritic[i].getDeveloper());
- }
- }
- //выводим список
- dout << developGenre << endl;
- tableLine(dout);
- DevelopersList.showFromBegin(dout);
- tableLine(dout);
- //закрытие файлов
- in.close();
- out.close();
- din.close();
- dout.close();
- return 0;
- }
- catch (const String & error)
- {
- cerr << endl << error << endl;
- return -1;
- }
- }
- //вывод табличной линии
- void tableLine(ofstream& out)
- {
- for (int i = 0; i < 117; ++i)
- {
- out << '-';
- }
- out << endl;
- }
- //сортировка массива простыми вставками
- void Sort(Array<Game>& ob)
- {
- for (int i = 0; i < ob.length(); ++i)
- for (int j = i; j > 0 && ob[j - 1].getRating() < ob[j].getRating(); --j)
- {
- Game temp = ob[j - 1];
- ob[j - 1] = ob[j];
- ob[j] = temp;
- }
- }
- //перевод строки в число
- int strToInt(char* s)
- {
- int temp = 0; // число
- int i = 0;
- int sign = 0; // знак числа 0- положительное, 1 — отрицательное
- if (s[i] == '-')
- {
- sign = 1;
- i++;
- }
- while (s[i] >= 0x30 && s[i] <= 0x39)
- {
- temp = temp + (s[i] & 0x0F);
- temp = temp * 10;
- i++;
- }
- temp = temp / 10;
- if (sign == 1)
- temp = -temp;
- return(temp);
- }
- //поиск максимального объекта в массиве
- Game maxObject(Array<Game>& ob)
- {
- Game maxObj = ob[0];
- for (int i = 0; i < ob.length(); ++i)
- {
- if (ob[i].getRating() > maxObj.getRating())
- maxObj = ob[i];
- }
- return maxObj;
- }
- //поиск минимального объекта в массиве
- Game minObject(Array<Game>& ob)
- {
- Game minObj = ob[0];
- for (int i = 0; i < ob.length(); ++i)
- {
- if (ob[i].getRating() < minObj.getRating())
- minObj = ob[i];
- }
- return minObj;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement