Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Book.h"
- #include <iostream>
- #include <fstream>
- #include "List.h"
- #include "Array.h"
- #include "DisciplineCount.h"
- #include "maxAndMin.h"
- //#include "recursion.h"
- using std::cout;
- using std::cin;
- using std::endl;
- using std::cerr;
- using std::ifstream;
- using std::ofstream;
- const String ERROR_FILE_OPEN = "Main: cannot open a file.";
- const String ERROR_ARRAY_SIZE = "Main: invalid array size.";
- char *getline(istream& in);
- Book split(String& str);
- struct maxAndMin maxMin(const Array<Book>& myArray, size_t size);
- template<typename B>
- void insertionSort(Array<B>& arrayPtr, size_t size) // сортировка вставками
- {
- B temp; // временная переменная для хранения значения элемента сортируемого массива
- int item; // индекс предыдущего элемента
- for (size_t counter = 1; counter < size; counter++)
- {
- temp = arrayPtr[counter]; // инициализируем временную переменную текущим значением элемента массива
- item = counter - 1; // запоминаем индекс предыдущего элемента массива
- while (item >= 0 && arrayPtr[item] > temp) // пока индекс не равен 0 и предыдущий элемент массива больше текущего
- {
- arrayPtr[item + 1] = arrayPtr[item]; // перестановка элементов массива
- arrayPtr[item] = temp;
- item--;
- }
- }
- }
- int main()
- {
- try
- {
- ifstream in; //массив
- in.open("in.txt");
- if (!in)
- {
- throw ERROR_FILE_OPEN;
- }
- String arraySizeStr = getline(in);
- int arraySizeInt = arraySizeStr.to_int();
- if (0 >= arraySizeInt || arraySizeInt > 100 )
- {
- throw ERROR_ARRAY_SIZE;
- }
- Array<Book> libArray(arraySizeInt);
- bool isLessThanArray = false;
- for (int i = 0; i < arraySizeInt; i++)
- {
- String readedLine = getline(in);
- if (readedLine == nullptr)
- {
- cout
- << "There are " << arraySizeInt - i
- << " unused fields, do you want to calculate above "
- << i << " objects? [Type 'Y', if yes. Type 'N', if not.]" << endl;
- char answer;
- cin >> answer;
- if (answer == 'N')
- {
- return -1;
- }
- isLessThanArray = true;
- arraySizeInt = i;
- break;
- }
- libArray[i] = split(readedLine);
- }
- String readedLine = getline(in);
- in.close();
- if ((isLessThanArray == false) && (readedLine != nullptr))
- {
- cout
- << "The maximum possible number of objects for processing has been reached, the remaining data may not be taken into count, do you want to calculate above "
- << arraySizeInt << " objects? [Type 'Y', if yes. Type 'N', if not.]" << endl;
- char answer;
- cin >> answer;
- if (answer == 'N')
- {
- return -1;
- }
- }
- cout << "Array has been filled!" << endl;
- struct maxAndMin mM = maxMin(libArray, arraySizeInt);//нахождение максимума и минимума (кол-во экземпляров в биб-ке)
- insertionSort<Book>(libArray, arraySizeInt);//сортировка массива объектов (кол-во экземпляров в биб-ке)
- //вставками по возрастанию
- ofstream outForArray;
- outForArray.open("outForArray.txt");
- if (!outForArray)
- {
- throw ERROR_FILE_OPEN;
- }
- outForArray << "Amount of the most popular book is: " << endl << endl
- << mM.maxBook << endl
- << "Less popular is: " << endl << endl << mM.minBook << endl
- << "====================================================" << endl;
- outForArray << "Sorted library (by amount of books in library): " << endl << endl;
- for (int i = 0; i < arraySizeInt; i++)
- {
- outForArray << libArray[i] << endl;
- }
- cout << "Array is done!" << endl;
- outForArray.close();
- List<DisciplineCount> libListDis;
- for (size_t i = 0; i < arraySizeInt; i++)
- {
- libListDis.insertDisCountSort(libArray[i]);
- }
- ofstream outForList;
- outForList.open("outForList.txt");
- if (!outForList)
- {
- throw ERROR_FILE_OPEN;
- }
- libListDis.showTable(outForList);
- outForList.close();
- cout << "List is done!" << endl;
- //========================================================================
- //List<Book> libList;
- //for (size_t i = 0; i < arraySizeInt; i++)
- //{
- // libList.insertDisSort(libArray[i]);
- //}
- //cout << "List has been filled!" << endl;
- //ofstream outForList;
- //outForList.open("outForList.txt");
- ///*for (int i = 0; i < libList.getSize(); i++)
- //{
- // outForList << libList[i] << endl;
- //}*/
- //libList.showDisCountTable(outForList);
- //outForList.close();
- //cout << "List is done!" << endl;
- //=======================================================
- /*ifstream in;
- in.open("in.txt");
- String readedLine = getline(in);
- Book toRecurs = split(readedLine);
- String auth = toRecurs.getAuthor();
- String name = toRecurs.getName();
- int ye = toRecurs.getYear();
- String dis = toRecurs.getDiscipline();
- int amo = toRecurs.getAmount();
- cout << "auth is: " << isAuth(auth) << endl;
- cout << "name is:" << isName(name) << endl;
- cout << "year is:" << isYear(ye) << endl;
- cout << "dis is:" << isDiscipline(dis) << endl;
- cout << "amo is:" << isAmount(amo) << endl;
- in.close();*/
- system("pause");
- return 0;
- }
- catch (const String err)
- {
- cerr << err << endl;
- system("pause");
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement