社区讨论
帮忙看看有什么问题?
P1449后缀表达式参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @lo7wi0ew
- 此快照首次捕获于
- 2023/10/27 08:54 2 年前
- 此快照最后确认于
- 2023/10/27 08:54 2 年前
CPP
#include <bits/stdc++.h>
#include <string>
using namespace std;
int top=0,b[10001]={};
string a;
void push(int t)
{
b[++top]=t;
}
void pop()
{
top--;
}
bool empty()
{
return top==0?1:0;
}
int get_top()
{
return b[top];
}
int main()
{
getline(cin,a);
int m,n;
for(int i=0;i<a.size();i++)
{
if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/')
{
m=get_top();
pop();
n=get_top();
pop();
if(a[i]=='+') push(m+n);
else if(a[i]=='-') push(n-m);
else if(a[i]=='*') push(m*n);
else if(a[i]=='/') push(n/m);
}
else if(a[i]=='.')
{
continue;
}
else
{
int sum=0;
while(a[i]>='0'&&a[i]<='9')
{
sum=sum*10+a[i++]-'0';
}
push(sum);
}
}
cout<<get_top();
return 0;
}
因为老师上课讲过,所以直接挪过来...
回复
共 2 条回复,欢迎继续交流。
正在加载回复...