2133-数据结构实验之栈与队列三:后缀式求值
来源:互联网 发布:怎么检查网络被盗用 编辑:程序博客网 时间:2024/05/19 09:14
#include <bits/stdc++.h>using namespace std;typedef int ElemType;class Stack{private: ElemType *up; ElemType *base; ElemType length;public: Stack(); void push(ElemType); void pop(); ElemType top(){return *(up - 1);} ElemType size(){return length;} bool empty(){return 0 == length;}};ElemType cal(ElemType a, ElemType b, char op);int main(){ char st[1123]; while(cin >> st) { Stack Q; for(int i = 0; st[i]; i++) { if(st[i] == '#') { break; } if(isdigit(st[i])) { Q.push(st[i] - '0'); } else{ int a = Q.top(); Q.pop(); int b = Q.top(); Q.pop(); int x = cal( b, a, st[i]); Q.push(x); } } cout << Q.top() << endl; } return 0;}Stack::Stack(){ base = new ElemType; up = base; length = 0;}void Stack::push(ElemType x){ *up++ = x; length++;}void Stack::pop(){ up--; length--;}ElemType cal(ElemType a, ElemType b, char op){ if(op == '+') return a + b; if(op == '-') return a - b; if(op == '*') return a * b; if(op == '/') return a / b; return 0;}
阅读全文
0 0
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 数据结构实验之栈与队列三:后缀式求值
- 2133 数据结构实验之栈与队列三:后缀式求值
- 2133-数据结构实验之栈与队列三:后缀式求值
- 1019作业
- linux命令---vi编辑器快速定位行数、删除当前行、和删除当前行后面的全部内容
- 2132-数据结构实验之栈与队列二:一般算术表达式转换成后缀式
- OpenGL绘制正四面体
- 访问本地Tomcat服务器 localhost/127.0.0.1:8080/web_app/xx- Connection refused
- 2133-数据结构实验之栈与队列三:后缀式求值
- 树形结构地柜查询子节点
- jTessBoxEditor使用
- 2134-数据结构实验之栈与队列四:括号匹配
- angular2如何识别字符串中的html标签,并按照html来显示视图
- c++ 三目运算符
- 3333-数据结构实验之栈与队列五:下一较大值(一)
- hive中的NULL(hive空值处理)
- #每天一点R语言# 简单运算