算术表达式的转换
来源:互联网 发布:unity3d点击图片事件 编辑:程序博客网 时间:2024/04/27 18:36
算术表达式的转换
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
小明在学习了数据结构之后,突然想起了以前没有解决的算术表达式转化成后缀式的问题,今天他想解决一下。
因为有了数据结构的基础小明很快就解出了这个问题,但是他突然想到怎么求出算术表达式的前缀式和中缀式呢?小明很困惑。聪明的你帮他解决吧。
输入
输入一算术表达式,以\’#\’字符作为结束标志。(数据保证无空格,只有一组输入)
输出
输出该表达式转换所得到的前缀式 中缀式 后缀式。分三行输出,顺序是前缀式 中缀式 后缀式。
示例输入
a*b+(c-d/e)*f#
示例输出
+ab-c/def
a*b+c-d/e*f
ab*cde/-f*+
#include <map>#include <set>#include <cmath>#include <stack>#include <queue>#include <time.h>#include <cstdio>#include <cctype>#include <vector>#include <string>#include <climits>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#define RR freopen("input.txt","r",stdin)#define WW freopen("output.txt","w",stdout)#define INF 0x3f3f3f3fusing namespace std;char s[1110];char str[1100];char st[1100];int trans(char c){ if(c=='+'||c=='-') return 1; if(c=='*'||c=='/') return 2; return 3;}void head(){ int len=strlen(s); int top=0,i; int End=0; for(i=len-1;i;i--) { if(s[i]=='#') break; } for( i--; i>=0; i--) { if(s[i]>='a'&&s[i]<='z') { str[top++]=s[i]; } else if(s[i]!='(') { if(s[i]==')') { st[++End]=s[i]; } else { while(End&&st[End]!=')'&&trans(st[End])>trans(s[i]))//这里是大于 { str[top++]=st[End]; End--; } st[++End]=s[i]; } } else { while(st[End]!=')') { str[top++]=st[End]; End--; } End--; } } while(End) { str[top++]=st[End]; End--; } for(int i=top-1; i>=0; i--) { cout<<str[i]; } cout<<endl;}void mid(){ for(int i=0; s[i]!='#'; i++) { if(s[i]!='('&&s[i]!=')') { cout<<s[i]; } } cout<<endl;}void last(){ int top=0; int End=0; for(int i=0; s[i]!='#'; i++) { if(s[i]>='a'&&s[i]<='z') { str[top++]=s[i]; } else if(s[i]!=')') { if(s[i]=='(') { st[++End]=s[i]; } else { while(End&&st[End]!='('&&trans(st[End])>=trans(s[i]))//这里是大于等于 { str[top++]=st[End]; End--; } st[++End]=s[i]; } } else { while(st[End]!='(') { str[top++]=st[End]; End--; } End--; } } while(End) { str[top++]=st[End]; End--; } for(int i=0;i<top;i++) { cout<<str[i]; } cout<<endl;}int main(){ scanf("%s",s); head(); mid(); last(); return 0;}
0 0
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- 算术表达式的转换
- PagerAdapter
- Spring启动过程
- TimePicker在ScrollView中滑动冲突的解决方法(更新适用于全控件的方法)
- 通过实现一个 TableView 来理解 iOS UI 编程
- 《开源框架那点事儿16》:缓存相关代码的演变
- 算术表达式的转换
- break和continue的区别
- Jquery ajax项目实例
- 多列转行命令
- 如何用js判断不同浏览器
- 解决Andorid调试ADB端口占用问题
- 设计模式
- jvm 垃圾回收(个人笔记)
- Oracle 中删除数据