-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
181 lines (138 loc) · 3.73 KB
/
Main.cpp
File metadata and controls
181 lines (138 loc) · 3.73 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "NewHeap.h"
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
void extractFromString(string line, NewHeap& mainHeap, int& countE, int& countOper, bool& isInsert, bool& isCreateEmpty);
bool validNumber(string number);
void invalidInput();
void main()
{
NewHeap mainHeap;
string line,s;
int numOfOperations = 0, countOper = 0, countE = 0;
bool isInsert = false,start=true, isCreateEmpty=false;
//size of arr
cin >> numOfOperations;
cout << endl;
s = getchar();//eat white space
if (numOfOperations < 0)
invalidInput();
if (numOfOperations > 0)
{
while (start)
{
getline(cin, line);
if (!line.empty())
{
extractFromString(line, mainHeap, countE, countOper, isInsert, isCreateEmpty);
countOper++;
}
else
start = false;
}
if (countOper != numOfOperations) // if there is less or more inputs than expected
invalidInput();
}
}
void extractFromString(string line,NewHeap& mainHeap,int & countE, int& countOper,bool & isInsert,bool & isCreateEmpty)
{
Pair retPair;
stringstream str;
string data=" ",temp,checkNum;
int num=UNDEFINED;
char oper;
str << line;
str >> oper;//get the operation
str >> checkNum;
switch (oper)
{
case 'a':
if (!isInsert || !isCreateEmpty || !checkNum.empty())//checck if there were members and structure before operations
invalidInput();//check if we need to return value before get any input
else
{
retPair = mainHeap.Max();
cout << retPair.priority << " " << retPair.data << endl;
}
break;
case 'b':
if (!isInsert || !isCreateEmpty || !checkNum.empty())//check if we need to return value before get any input
invalidInput();
else
{
retPair = mainHeap.DeleteMax();
cout << retPair.priority << " " << retPair.data << endl;
}
break;
case 'c':
if (!isInsert || !isCreateEmpty || !checkNum.empty())//check if we need to return value before get any input
invalidInput();
else
{
retPair = mainHeap.Min();
cout << retPair.priority << " " << retPair.data << endl;
}
break;
case 'd':
if (!isInsert || !isCreateEmpty || !checkNum.empty())//check if we need to return value before get any input
invalidInput();
else
{
retPair = mainHeap.DeleteMin();
cout << retPair.priority << " " << retPair.data << endl;
}
break;
case 'e':
if (!checkNum.empty())
invalidInput();
isCreateEmpty = true;
countE++;
if (countE != 1) // if there is more than one 'e' operation
invalidInput();
if (countE == 1 && countOper != 0) // if there is operation 'e', but not in first line
invalidInput();
mainHeap.CreateEmpty();
break;
case 'f':
if (!isCreateEmpty || checkNum.empty() || !validNumber(checkNum))
invalidInput();
else
stringstream(checkNum) >> num;
while (str >> temp) // get from str the string value
{
data += temp + ' ';
}
isInsert = true;
mainHeap.insert(num,data);
break;
case 'g':
if (!isInsert || !isCreateEmpty || !checkNum.empty())//check if we need to return value before get any input
invalidInput();//check if we need to return value before get any input
else
{
retPair = mainHeap.Median();
cout << retPair.priority << " " << retPair.data << endl;
}
break;
default:
invalidInput();//check if the first char isnt an operation
}
}
bool validNumber(string number)
{
if (number[0] != '-' && (!isdigit(number[0])))
return false;
for (int i = 1; i < number.length(); i++)
{
if (!isdigit(number[i]))
return false;
}
return true;
}
void invalidInput()
{
cout << "Wrong input." << endl;
exit(0);
}