数据结构实验之栈三:后缀式求值
来源:互联网 发布:宁德时代 知乎 编辑:程序博客网 时间:2024/06/15 12:11
Think:
利用栈的性质, 将操作数进行储存即可
Problem Description
对于一个基于二元运算符的后缀表示式(基本操作数都是一位正整数),求其代表的算术表达式的值。
Input
输入一个算术表达式的后缀式字符串,以‘#’作为结束标志。
Output
求该后缀式所对应的算术表达式的值,并输出之。
Example Input
59*684/-3*+#
Example Output
57
Hint
基本操作数都是一位正整数!
#include<bits/stdc++.h>using namespace std;#define INF 0x3f3f3ftypedef int Elemtype;typedef struct{ Elemtype *elem; int top; int base;} Stack;void InitStack(Stack &S);void Push(Stack &S, Elemtype key);int main(){ int i; char str[10086]; while(cin >> str) { Stack S; InitStack(S); for (i = 0;str[i] != '#'; i ++) { if (str[i] == '+') { S.elem[S.top - 2] = S.elem[S.top - 2] + S.elem[S.top - 1]; S.top --; } else if (str[i] == '-') { S.elem[S.top - 2] = S.elem[S.top - 2] - S.elem[S.top - 1]; S.top --; } else if (str[i] == '*') { S.elem[S.top - 2] = S.elem[S.top - 2] * S.elem[S.top - 1]; S.top --; } else if (str[i] == '/') { S.elem[S.top - 2] = S.elem[S.top - 2] / S.elem[S.top - 1]; S.top --; } else { int key = str[i] - '0'; Push(S, key); } } cout << S.elem[S.top - 1] << endl; }}void InitStack(Stack &S){ S.elem = new int [INF]; S.top = S.base = 0;}void Push(Stack &S, Elemtype key){ S.elem[S.top] = key; S.top ++;}
阅读全文
0 0
- SDUT2133数据结构实验之栈三:后缀式求值(栈)
- 数据结构实验之栈三:后缀式求值
- 数据结构实验之栈三:后缀式求值
- 数据结构实验之栈三:后缀式求值
- 数据结构实验之栈三:后缀式求值
- 数据结构实验之栈三:后缀式求值
- SDUT 2133 数据结构实验之栈三:后缀式求值
- 数据结构实验之栈三:后缀式求值
- 数据结构实验之栈三:后缀式求值
- 数据结构实验之栈三:后缀式求值
- 数据结构实验之栈三:后缀式求值
- SDUT 2133 数据结构实验之栈三:后缀式求值
- 数据结构实验之栈三:后缀式求值
- 数据结构实验之栈三:后缀式求值
- 数据结构实验之栈三:后缀式求值
- SDUT 3133 数据结构实验之栈三:后缀式求值
- 数据结构实验之栈三:后缀式求值 SDUT 2133
- 数据结构实验之栈三:后缀式求值
- CMake里面怎么递归地设定头文件的搜索目录
- hbase编程指南
- 算法题:二分查找
- 学习资料
- Sass、LESS 和 Stylus区别总结
- 数据结构实验之栈三:后缀式求值
- Hbase集群监控
- CodeForces
- Creating Web Animations-OReilly 2017 读书笔记
- USACO-Section1.5 Superprime Rib
- JAVA自定义注解实现SQL语句自动生成DEMO
- windows平台多tomcat注册系统服务及jvm优化
- SocketRocket的导入及基本功能实现
- Ubuntu系统下运行Eclipse出现找不到jre的问题的解决方法