-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
145 lines (117 loc) · 3.14 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <fstream>
#include "List.h"
#include "DisciplineCount.h"
#include "insertionSort.h"
#include "maxAndMin.h"
const String FILE_NOT_OPEN = "Main: file not open.";
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
using std::ifstream;
using std::ofstream;
Book split(String& str);
char *getline(istream& in);
struct maxAndMin maxMin(const Array<Book>& myArray, size_t size);
int main()
{
try
{
ifstream in;
in.open("in.txt");
if (!in)
{
throw FILE_NOT_OPEN;
}
String arraySizeStr = getline(in);
int arraySizeInt = arraySizeStr.to_int();
Array<Book> libArray(arraySizeInt);
bool isLessThanArraySize = 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 anything, if yes. Type 'N', if not.]" << endl;
char answer;
cin >> answer;
if (answer == 'N')
{
return -1;
}
isLessThanArraySize = true;
arraySizeInt = i;
break;
}
libArray[i] = split(readedLine);
}
String readedLine = getline(in);
in.close();
if ((isLessThanArraySize == 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 anything, 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 FILE_NOT_OPEN;
}
if (mM.maxBook == mM.minBook)
{
outForArray << "Equal number of all books in the library is: " << endl << mM.maxBook.getAmount() << endl;
}
else
{
outForArray << "Amount of the most popular book is: " << endl << endl
<< mM.maxBook << endl
<< "Less popular is: " << endl << endl << mM.minBook << endl;
}
outForArray << "Sorted library (by amount of books in library): " << endl << endl;
for (size_t 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 FILE_NOT_OPEN;
}
libListDis.showTable(outForList);
outForList.close();
cout << "List is done!" << endl;
cout << "Everything is DONE. Checkout the output files!" << endl << endl;
system("pause");
return 0;
}
catch (const String err)
{
cerr << err << endl;
system("pause");
return -1;
}
}