前缀转中缀(利用栈)
来源:互联网 发布:七微南风知我意2微盘 编辑:程序博客网 时间:2024/06/08 10:09
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>#include<stack>using namespace std;struct node{int type;string s;void kuo(){s="("+s+")";}node(int t,string str){type=t;s=str;}};stack<node> sta;char s[100];void push(int t,string s){if(t==1){while(!sta.empty() && sta.top().type==1){node node1=sta.top();sta.pop();node node2=sta.top();sta.pop();s="("+node1.s+node2.s+s+")";}sta.push(node(1,s));}else{sta.push(node(0,s));}}int main(){ freopen("in","r",stdin); //freopen("out.txt","w",stdout); while(gets(s)!=NULL){int len=strlen(s);while(!sta.empty()) sta.pop();for(int i=0;i<len;){if(s[i]>='0' && s[i]<='9' ){string tmp="";while(i<len && s[i]>='0' && s[i]<='9'){tmp+=s[i];i++;}push(1,tmp);}else if(s[i]>='a' && s[i]<='z' || s[i]>='A' && s[i]<='Z'){string tmp="";tmp+=s[i];push(1,tmp);i++;}else if(s[i]=='|' || s[i]=='&' || s[i]=='>' && s[i+1]=='=' || s[i]=='<' && s[i+1]=='=' || s[i]=='=' && s[i+1]=='=' || s[i]=='!' && s[i+1]=='=' ){string tmp="";tmp+=s[i];tmp+=s[i+1];i+=2;push(0,tmp);}else{string tmp="";tmp+=s[i];push(0,tmp);i++;}}cout<<sta.top().s<<endl;} return 0;}input:
+-*+ABD/E+F*ADC
output:
((((A+B)*D)-(E/(F+(A*D))))+C)
0 0
- 前缀转中缀(利用栈)
- 利用栈实现中缀表达式转前缀表达式
- 中缀转前缀
- 中缀转前缀表达式
- 中缀转后缀(利用栈)
- 前缀、中缀、后缀表达式(栈)
- 中缀表达式转前缀表达式
- 中缀转前缀 c语言
- 中缀表达式转前缀表达式
- 中缀表达式转前缀表达式
- 表达式计算(中缀表达式转后缀前缀表达式)
- VS2015,C++实现前缀、中缀、后缀表达式求值、前缀转中缀、前缀转后缀。
- 中缀式转前缀式、中缀式转后缀式
- 中缀表达式求值、后缀表达式求值、中缀转后缀、前缀
- [Python]中缀表达式转前缀表达式
- 中缀表达式转前缀和后缀表达式
- 表达式求值(前缀、中缀、后缀)
- 中缀,前缀,后缀表达式
- sss
- ThinkPHP 跨控制器页面跳转 官方文档 未讲清楚
- netty5笔记-线程模型3-EventLoop
- vm和主机互相ping不通
- 快速识别汇编中等价的C语言语句(if, while, for, switch)
- 前缀转中缀(利用栈)
- Python模块
- maven2中snapshot快照库和release发布库的应用
- Dropout原理介绍
- 标题和图片的兼容性
- flyway初始化脚本存在默认特殊字符的问题处理
- Python——debug
- 小米push
- android 短信添加附件