-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoj1101.cpp
116 lines (111 loc) · 1.64 KB
/
oj1101.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
#include <stdio.h>
#include <stack>
using namespace std;
int rank[5][5]={
1,0,0,0,0,
1,0,0,0,0,
1,0,0,0,0,
1,1,1,0,0,
1,1,1,0,0,
};
char buf[1000];
bool op;
float num;
int opRank;
stack<int> opStack;
stack<float> numStack;
void getOP(bool &retOp,int &retRank,float &retNum,int &i){
if(buf[i]=='\0')
{
retOp=true;
retRank=0;
retNum=-1;
}
else if(buf[i]<='9'&&buf[i]>='0')
{
retOp=false;
retRank=-1;
retNum=buf[i]-'0';
while(buf[i+1]<='9'&&buf[i+1]>='0')
{
retNum*=10;
retNum+=buf[++i]-'0';
}
}
else
switch(buf[i])
{
case '+':
retOp=true;
retRank=1;
retNum=-1;
break;
case '-':
retOp=true;
retRank=2;
retNum=-1;
break;
case '*':
retOp=true;
retRank=3;
retNum=-1;
break;
case '/':
retOp=true;
retRank=4;
retNum=-1;
break;
}
i++;
return;
}
int main(){
while(scanf("%s",buf)!=EOF){
while(!opStack.empty())
opStack.pop();
while(!numStack.empty())
numStack.pop();
int i=0;
opStack.push(0);
while(true)
{
getOP(op,opRank,num,i);
if(!op)
numStack.push(num);
else
{
while(!rank[opRank][opStack.top()])
{
float a,b;
b=numStack.top();
numStack.pop();
a=numStack.top();
numStack.pop();
switch(opStack.top())
{
case 1:
a+=b;
break;
case 2:
a-=b;
break;
case 3:
a*=b;
break;
case 4:
a/=b;
break;
}
numStack.push(a);
opStack.pop();
}
opStack.push(opRank);
}
if(opStack.size()==2&&!opStack.top())
break;
}
int ans=numStack.top();
printf("%d\n",ans);
}
return 0;
}