提交记录 #554
提交时间:2025-11-17 15:22:37
语言:c
状态:Queueing
编译情况:等待中...
固定测试点暂不可用
附加测试点暂不可用
H22【选做▪应用】逆波兰算术表达式
#include<bits/stdc++.h>
using namespace std;
int main(){
char a[1000001],sign[100];
int num[10001];
int top = -1;
while(1){
top++;
a[top] = getchar();
if (a[top] =='\n'){
break;
}
}
top++;
// for (int i = 0;i < top;i++){
// //cout<<a[i];
// }
int top1 = 0,top2 = 0;
int number = 0,sum = 0;
for (int i = 0;i < top;i++){
if (a[i] == ' ' and a[i - 1] >= '0' and a[i - 1] <= '9'){
num[top1] = number;
top1++;
number = 0;
}else if ('0' <= a[i] and a[i] <= '9'){
number = number * 10 + a[i] - '0';
}else if(a[i] == '+' or a[i] == '-' or a[i] == '*' or a[i] == '/'){
top1--;
sum = num[top1];
if (a[i] == '+'){
top1--;
sum += num[top1];
num[top1] = sum;
top1++;
}else if (a[i] == '-'){
top1--;
sum = num[top1] - sum;
num[top1] = sum;
top1++;
}else if (a[i] == '*'){
top1--;
sum *= num[top1];
num[top1] = sum;
top1++;
}else if (a[i] == '/'){
top1--;
sum = num[top1] / sum;
num[top1] = sum;
top1++;
}
}
}
cout<<sum<<endl;
return 0;
}