forked from love1024/spoj-solution-with-explanation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathARITH2.cpp
64 lines (48 loc) · 1.22 KB
/
ARITH2.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
#include<bits/stdc++.h>
using namespace std;
int main() {
//Number of test cases
int t;
cin>>t;
//Ignore the carriage return
cin.ignore();
//line to store entire line
string line;
//To store result of calculation
long long int res = -1;
//Store the operand other than res
int b;
//character to store operator
char o;
//Get line from console
getline(cin,line);
getline(cin,line);
//While there is input
while(line != "") {
//put string in string stream to get operand and operator
istringstream strm(line);
strm>>res;
//Traverse all line
while(strm) {
//Take operator and operand
strm>>o>>b;
//Perform calculation according to operator
if(o == '=')
break;
else if(o == '+')
res = res + b;
else if(o == '-')
res = res - b;
else if(o == '*')
res = res*b;
else if(o == '/')
res = res/b;
}
//Print the result
cout<<res<<endl;
//Take next line
getline(cin,line);
getline(cin,line);
}
return 0;
}