Advertisement
Guest User

main.cpp

a guest
Dec 5th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.55 KB | None | 0 0
  1. #include "Book.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include "List.h"
  5. #include "Array.h"
  6. #include "DisciplineCount.h"
  7. #include "maxAndMin.h"
  8. //#include "recursion.h"
  9.  
  10. using std::cout;
  11. using std::cin;
  12. using std::endl;
  13. using std::cerr;
  14. using std::ifstream;
  15. using std::ofstream;
  16.  
  17. const String ERROR_FILE_OPEN = "Main: cannot open a file.";
  18. const String ERROR_ARRAY_SIZE = "Main: invalid array size.";
  19.  
  20. char *getline(istream& in);
  21. Book split(String& str);
  22.  
  23. struct maxAndMin maxMin(const Array<Book>& myArray, size_t size);
  24.  
  25. template<typename B>
  26. void insertionSort(Array<B>& arrayPtr, size_t size) // сортировка вставками
  27. {
  28.     B temp; // временная переменная для хранения значения элемента сортируемого массива
  29.     int item; // индекс предыдущего элемента
  30.  
  31.     for (size_t counter = 1; counter < size; counter++)
  32.     {
  33.         temp = arrayPtr[counter]; // инициализируем временную переменную текущим значением элемента массива
  34.         item = counter - 1; // запоминаем индекс предыдущего элемента массива
  35.         while (item >= 0 && arrayPtr[item] > temp) // пока индекс не равен 0 и предыдущий элемент массива больше текущего
  36.         {
  37.             arrayPtr[item + 1] = arrayPtr[item]; // перестановка элементов массива
  38.             arrayPtr[item] = temp;
  39.             item--;
  40.         }
  41.     }
  42. }
  43.  
  44. int main()
  45. {
  46.     try
  47.     {
  48.  
  49.         ifstream in;                              //массив
  50.         in.open("in.txt");
  51.  
  52.         if (!in)
  53.         {
  54.             throw ERROR_FILE_OPEN;
  55.         }
  56.  
  57.         String arraySizeStr = getline(in);
  58.  
  59.         int arraySizeInt = arraySizeStr.to_int();
  60.         if (0 >= arraySizeInt || arraySizeInt > 100 )
  61.         {
  62.             throw ERROR_ARRAY_SIZE;
  63.         }
  64.         Array<Book> libArray(arraySizeInt);
  65.  
  66.         bool isLessThanArray = false;
  67.  
  68.         for (int i = 0; i < arraySizeInt; i++)
  69.         {
  70.             String readedLine = getline(in);
  71.             if (readedLine == nullptr)
  72.             {
  73.                 cout
  74.                     << "There are " << arraySizeInt - i
  75.                     << " unused fields, do you want to calculate above "
  76.                     << i << " objects? [Type 'Y', if yes. Type 'N', if not.]" << endl;
  77.                 char answer;
  78.                 cin >> answer;
  79.                 if (answer == 'N')
  80.                 {
  81.                     return -1;
  82.                 }
  83.                 isLessThanArray = true;
  84.                 arraySizeInt = i;
  85.                 break;
  86.             }
  87.  
  88.             libArray[i] = split(readedLine);
  89.         }
  90.  
  91.         String readedLine = getline(in);
  92.         in.close();
  93.  
  94.         if ((isLessThanArray == false) && (readedLine != nullptr))
  95.         {
  96.             cout
  97.                 << "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 "
  98.                 << arraySizeInt << " objects? [Type 'Y', if yes. Type 'N', if not.]" << endl;
  99.             char answer;
  100.             cin >> answer;
  101.             if (answer == 'N')
  102.             {
  103.                 return -1;
  104.             }
  105.         }
  106.  
  107.  
  108.         cout << "Array has been filled!" << endl;
  109.  
  110.         struct maxAndMin mM = maxMin(libArray, arraySizeInt);//нахождение максимума и минимума (кол-во экземпляров в биб-ке)
  111.  
  112.         insertionSort<Book>(libArray, arraySizeInt);//сортировка массива объектов (кол-во экземпляров в биб-ке)
  113.                                                    //вставками по возрастанию
  114.  
  115.         ofstream outForArray;
  116.         outForArray.open("outForArray.txt");
  117.         if (!outForArray)
  118.         {
  119.             throw ERROR_FILE_OPEN;
  120.         }
  121.  
  122.         outForArray << "Amount of the most popular book is: " << endl << endl
  123.             << mM.maxBook << endl  
  124.             << "Less popular is: " << endl << endl << mM.minBook << endl  
  125.             << "====================================================" << endl;
  126.  
  127.         outForArray << "Sorted library (by amount of books in library): " << endl << endl;
  128.  
  129.         for (int i = 0; i < arraySizeInt; i++)
  130.         {
  131.             outForArray << libArray[i] << endl;
  132.         }
  133.  
  134.         cout << "Array is done!" << endl;
  135.  
  136.         outForArray.close();
  137.  
  138.         List<DisciplineCount> libListDis;
  139.         for (size_t i = 0; i < arraySizeInt; i++)
  140.         {
  141.             libListDis.insertDisCountSort(libArray[i]);
  142.         }
  143.  
  144.         ofstream outForList;
  145.         outForList.open("outForList.txt");
  146.         if (!outForList)
  147.         {
  148.             throw ERROR_FILE_OPEN;
  149.         }
  150.  
  151.         libListDis.showTable(outForList);
  152.  
  153.         outForList.close();
  154.  
  155.         cout << "List is done!" << endl;
  156.         //========================================================================
  157.         //List<Book> libList;              
  158.  
  159.         //for (size_t i = 0; i < arraySizeInt; i++)
  160.         //{
  161.         //  libList.insertDisSort(libArray[i]);
  162.         //}
  163.         //cout << "List has been filled!" << endl;
  164.  
  165.         //ofstream outForList;
  166.         //outForList.open("outForList.txt");
  167.  
  168.         ///*for (int i = 0; i < libList.getSize(); i++)
  169.         //{
  170.         //  outForList << libList[i] << endl;
  171.         //}*/
  172.         //libList.showDisCountTable(outForList);
  173.         //outForList.close();
  174.         //cout << "List is done!" << endl;
  175.         //=======================================================
  176.         /*ifstream in;
  177.         in.open("in.txt");
  178.  
  179.         String readedLine = getline(in);
  180.  
  181.         Book toRecurs = split(readedLine);
  182.  
  183.         String auth = toRecurs.getAuthor();
  184.         String name = toRecurs.getName();
  185.         int ye = toRecurs.getYear();
  186.         String dis = toRecurs.getDiscipline();
  187.         int amo = toRecurs.getAmount();
  188.  
  189.         cout << "auth is: " << isAuth(auth) << endl;
  190.         cout << "name is:" << isName(name) << endl;
  191.         cout << "year is:" << isYear(ye) << endl;
  192.         cout << "dis is:" << isDiscipline(dis) << endl;
  193.         cout << "amo is:" << isAmount(amo) << endl;
  194.  
  195.         in.close();*/
  196.  
  197.         system("pause");
  198.         return 0;
  199.     }
  200.     catch (const String err)
  201.     {
  202.         cerr << err << endl;
  203.         system("pause");
  204.         return -1;
  205.     }
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement