计算波兰表达式
2016-03-27 17:02
381 查看
//计算波兰表达式 #include<iostream> #include<string> using namespace std; const int MAX_SIZE = 100;//定义栈的最大容量 typedef struct stack { int top; int data[MAX_SIZE]; }stack; stack S; /* 计算波兰表达式 数字入栈,符号计算入栈 */ void Solution() { string s; cin>>s; int s_len=s.length(); S.top=0;//初始化 int i=0;//迭代器 int num;//存储中间值 while(i<s_len) { if(s[i]>='0'&&s[i]<='9') S.data[S.top++]=s[i]-'0'; else { switch(s[i]) { case '+':num=S.data[S.top-1]+S.data[S.top-2];S.data[S.top++]=num;break; case '-':num=S.data[S.top-2]-S.data[S.top-1];S.data[S.top++]=num;break; case '*':num=S.data[S.top-1]*S.data[S.top-2];S.data[S.top++]=num;break; case '/':if(S.data[S.top-1]!=0) {num=S.data[S.top-1]+S.data[S.top-2];S.data[S.top++]=num;break;} else cout<<"除数不为0"<<endl; } } i++; } cout<<S.data[S.top-1]<<endl; } int main() { Solution(); system("pause"); }
相关文章推荐
- 1010.一元多项式求导 (25)
- 迷瘴
- React 教程及其API接口文档
- Ubuntu 12.04 升级到14.04之后,pidgin-sipe 出现的问题: Trouble with the pidgin and self-signed SSL certificate
- 敏捷开发方法综述
- 利用封装类查询
- hihocoder -1283 hiho密码(水题)
- iOS开发--线程通信
- 实现京东底部添加到购物车的效果
- 插入排序(JAVA语言描述)
- FFMPEG采集摄像头数据并切片为iPhone的HTTP Stream流
- 最少拦截系统
- 15电气李丹【交换两个数字】
- 第五周任务安排
- code forces 630 M. Turn
- 活动选择问题
- 删数问题
- 高版本jquery尤其是1.10.2的版本设置input radio设置值的最正确的姿势。
- 【我的IT学习目标】目标是人前进的动力,只有有目标的人才可能成功!关于:C#
- HTML 笔记