中缀到后缀.cpp
来自「实现编译原理中缀到后缀的c++程序源代码 实现编译原理中缀到后缀的c++程序源」· C++ 代码 · 共 78 行
CPP
78 行
#include<iostream>
#include<string>
#include<stack>
using std::stack;
using std::string;
using std::cout;
using std::cin;
using std::endl;
string zzs;
int index=0;
string sta;
string op="+-*/()";
int priorty(char a)
{
if(a=='+'||a=='-')return 3;
if(a=='*'||a=='/')return 4;
if(a=='(')return 5;
if(a==')')return 1;
return 0;
}
void f(char a)
{
if(a==')')
{
index++;
return ;
}
if(a=='(')
{
while(zzs[index]!=')')
{
index++;
if(op.find(zzs[index])!=string::npos)
f(zzs[index]);
else
sta+=zzs[index];
}
}
else
{
index++;
while(index<zzs.length())
{
if(op.find(zzs[index])!=string::npos)
{
if(priorty(a)>=priorty(zzs[index]))break;
f(zzs[index]);
}
else
{
sta+=zzs[index];
index++;
}
}
sta+=a;
}
}
int main()
{
cout<<"请输入中缀式:"<<endl;
cin>>zzs;
while(index<zzs.length())
{
if(op.find(zzs[index])!=string::npos)
f(zzs[index]);
else
{
sta+=zzs[index];
index++;
}
}
cout<<sta<<endl;
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?