-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser3
More file actions
67 lines (64 loc) · 1.47 KB
/
parser3
File metadata and controls
67 lines (64 loc) · 1.47 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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
static bool stop = false;
int prior(char znak) {
if ((znak == '-') || (znak == '+')) return 1;
else if ((znak == '*') || (znak == '/')) return 2;
else {
stop = true;
cout << "Error";
exit(0);
return 0;
}
}
int count(int a, char znak, int b) {
int res = 0;
if (znak == '*') res = a*b;
else if (znak == '-')res = a - b;
else if (znak == '+') res = a + b;
else if (znak == '/') {
if (b == 0) cout << "Error";
else res = a / b;
}
else {
stop = true;
cout << "Error";
exit(0);
return 0;
}
return res;
}
int main()
{
string string;
int begin, middle, last, itog = 0;
char znak, znak2;
getline(cin, string);
istringstream stream(string);
stream >> begin;
stream >> znak;
if (!(stream >> middle)) { cout << begin << endl; exit(0); }
while (stream >> znak2) {
bool lastprior = false;
stream >> last;
int prior1 = prior(znak), prior2 = prior(znak2);
if ((prior1 > prior2) || (prior1 == prior2)) {
begin = count(begin, znak, middle);
itog = begin;
middle = last;
znak = znak2;
}
else if (prior2 > prior1) {
middle = count(middle, znak2, last);
itog = middle;
lastprior = true;
}
else { stop = true; cout << "Error"; break; }
if (!lastprior) itog = count(begin, znak, middle);
else itog = count(begin, znak, middle);
}
if (!(stream >> znak2)) { cout << count(begin, znak, middle) << endl; stop = false; return 0; }
if (!stop) cout << itog << endl;
}