中缀表达式转后缀表达式
来源:互联网 发布:手机logo免费设计软件 编辑:程序博客网 时间:2024/05/29 07:46
很久前写的代码,复习的时候翻了出来,发现以前的比较优先级函数写错了,改了改。网上的比较优先级的函数似乎大多数都有问题。
#include<iostream>
#include<sstream>
#include<cctype>
#include<stack>
using namespace std;
#include<sstream>
#include<cctype>
#include<stack>
using namespace std;
stack<char> s;
bool cmp(char c1, char c2)
{
if((c1=='+'||c1=='-')&&(c2=='+'||c2=='-'))
return true;
if(c1=='*'||c1=='/')
return true;
return false;
}
{
if((c1=='+'||c1=='-')&&(c2=='+'||c2=='-'))
return true;
if(c1=='*'||c1=='/')
return true;
return false;
}
int main()
{
string line;
char ch;
while(1)
{
getline(cin,line);
stringstream ss(line);
while(ss>>ch)
{
if(isdigit(ch)) cout<<ch;
else
{
if(ch=='(') s.push(ch);
else if(ch==')')
{
while(s.top()!='(')
{
cout<<s.top();
s.pop();
}
s.pop();
}
else
{
while(!s.empty()&&cmp(s.top(),ch))
{
cout<<s.top();
s.pop();
}
s.push(ch);
}
}
}
while(!s.empty()) {cout<<s.top();s.pop();}
cout<<endl;
}
return 0;
}
阅读全文
0 0
- 中缀转后缀表达式
- 表达式中缀转后缀
- 中缀转后缀表达式
- 中缀表达式转后缀表达式
- 中缀表达式转后缀表达式
- 中缀表达式转后缀表达式
- 中缀表达式转后缀表达式
- 中缀表达式转后缀表达式
- 中缀表达式转后缀表达式
- 中缀表达式转后缀表达式
- 中缀表达式转后缀表达式
- 后缀表达式转中缀表达式
- 中缀表达式转后缀表达式
- 中缀表达式转后缀表达式
- 中缀表达式转后缀表达式
- 中缀表达式转后缀表达式
- 中缀表达式转后缀表达式
- 中缀表达式转后缀表达式
- React使用Next.js作服务器端渲染
- 华为架构师8年经验谈:从单体架构到微服务的服务化演进之路
- Jmeter参数化-用户参数
- 1064. 朋友数(20)
- Android自定义UI文章汇总
- 中缀表达式转后缀表达式
- 1065. 单身狗(25)
- 一次性解决Java程序中的乱码问题
- BZOJ 1015: [JSOI2008]星球大战starwar kevin_xcw
- Ijkplayer播放视频的简单使用,可以暂停继续(二)
- 深入解析Linux 常用命令--arping
- 跳过异常继续执行
- 1067. 试密码(20)
- linux下快速启动tomcat脚本