Time Limit: 1000MS Memory limit: 65536K
题目描述
输入
输出
示例输入
59*684/-3*+#
示例输出
57
提示
来源
示例程序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<stack>
using namespace std;
int main()
{
int i;
char str[110];
scanf("%s",str);
stack<int>q;
for(i=0;str[i]!='#';i++)
{
if(str[i]>='0'&&str[i]<='9')
q.push(str[i]-48);
else
{
if(str[i]=='+')
{
int a=q.top();
q.pop();
int b=q.top();
q.pop();
int c=a+b;
q.push(c);
}
if(str[i]=='-')
{
int a=q.top();
q.pop();
int b=q.top();
q.pop();
int c=b-a;
q.push(c);
}
if(str[i]=='*')
{
int a=q.top();
q.pop();
int b=q.top();
q.pop();
int c=a*b;
q.push(c);
}
if(str[i]=='/')
{
int a=q.top();
q.pop();
int b=q.top();
q.pop();
int c=b/a;
q.push(c);
}
}
}
printf("%d\n",q.top());
return 0;
}
这是一个关于数据结构实验的博客,主要讲解如何利用栈来求解后缀表达式(逆波兰表达式)的值。程序示例中包含了一个C++实现,通过读取后缀表达式字符串,逐个处理字符,遇到数字则压入栈中,遇到运算符则进行相应的加减乘除操作,并更新栈顶元素。最后输出计算结果。
871

被折叠的 条评论
为什么被折叠?



