表达式求值问题
①问题描述 表 达 式 是 数 据 运 算 的 基 本 形 式 。 人 们 的 书 写 习 惯 是 中 缀 式 , 如 : 11+22*(7-4)/3。中缀式的计算按运算符的优先级及括号优先的原则,相同级别从 左到右进行计算。表达式还有后缀式(如:22 7 4 - * 3 / 11 +)和前缀式(如:+ 11 / * 22 – 7 4 3)。后缀表达式和前缀表达式中没有括号,给计算带来方便。如 后缀式计算时按运算符出现的先后进行计算。
本设计的主要任务是进行表达式形 式的转换及不同形式的表达式计算。
②基本要求
从文件或键盘读入中缀表达式。
设计操作数为多位整数,操作符为加、减、乘、除、求模的中缀表达式求值算法。
设计将中缀表达式转换为后缀表达式的算法。
设计将中缀表达式转换为前缀表达式的算法。
设计后缀表达式求值算法。
设计前缀表达式求值算法。
输出各种形式的表达式。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=1e4+10;
char exp[N];
int num[N],ntop,etop;
int check(char c)
{
if(c=='*'||c=='/'||c=='%')
return 1;
if(c=='+'||c=='-')
return 2;
}
bool judge(char c)//数字返回真,字符返回假
{
if(c>='0'&&c<='9')
return 1;
return 0;
}
int cal(char c,int a,int b)
{
if(c=='-')
return a-b;
if(c=='+')
return a+b;
if(c=='*')
return a*b;
if(c=='/')
return a/b;
if(c=='%')
return a%b;
}
void work1(char *s,int l)//前缀表达式求值,从右往左扫
{
cout<<"The answer is ";
char rnum[N];
int rtop=0;
bool f=0;
for(int i=l-1;i>=0;i--)
{
if(s[i]==' ')
{
if(f)
{
int numm=0;
for(int j=rtop-1;j>=0;j--)
numm=numm*10+rnum[j]-'0';
num[ntop++]=numm;
rtop=0;
f=0;
}
}
else
{
if(!judge(s[i]))
{
num[ntop-2]=cal(s[i],num[ntop-1],num[ntop-2]);
--ntop;
}
else
{
rnum[rtop++]=s[i];
f=1;
}
}
}
cout<<num[0]<<endl;
}
void work3(char *s,int l)//后缀表达式求值,从左往右扫
{
cout<<"The answer is ";
int ans;
int numm=0;
for(int i=0;i<l;i++)
{
if(s[i]==' ')
{
if(numm)
{
num[ntop++]=numm;
numm=0;
}
}
else
{
if(judge(s[i]))
numm=numm*10+s[i]-'0';
else
{
num[ntop-2]=cal(s[i],num[ntop-2],num[ntop-1]);
--ntop;
}
}
}
cout<<num[0]<<endl;
}
bool prior2(char a,char b)
{
if(b=='(')
return 0;
if((a=='+'||a=='-')&&(b=='*'||b=='/'||b=='%'))
return 1;
return 0;
}
bool prior(char a,char b)
{
if(b==')')
return 0;
if(a=='+'||a=='-')
return 1;
if((a=='*'||a=='/'||a=='%')&&(b=='*'||b=='/'||b=='%'))
return 1;
return 0;
}
void work2(char *s,int l)
{
cout<<"please select the founction"<<endl;
cout<<"1.change to prefix"<<endl;
cout<<"2.change to suffix"<<endl;
cout<<"3.calculate"<<endl;
char ss[N];
int stop=0;
for(int i=0;i<l;i++)
{
if(judge(s[i]))
ss[stop++]=s[i];
else
{
if(ss[stop-1]!=' ')
ss[stop++]=' ';
if((!etop)||s[i]=='(')
exp[etop++]=s[i];
else
{
if(s[i]==')')
{
while(exp[etop-1]!='(')
{
ss[stop++]=exp[--etop];
ss[stop++]=' ';
}
--etop;
}
else
{
while(etop&&prior2(s[i],exp[etop-1]))
{
ss[stop++]=exp[--etop];
ss[stop++]=' ';
}
exp[etop++]=s[i];
}
}
}
}
if(ss[stop-1]!=' ')
ss[stop++]=' ';
while(etop)
{
ss[stop++]=exp[--etop];
ss[stop++]=' ';
}
int ord;
cin>>ord;
if(ord==2)
cout<<ss<<endl;
else
if(ord==3)
work3(ss,stop);
else
{
stop=etop=0;
for(int i=l-1;i>=0;i--)
{
if(judge(s[i]))
ss[stop++]=s[i];
else
{
if(ss[stop-1]!=' ')
ss[stop++]=' ';
if((!etop)||s[i]==')')
exp[etop++]=s[i];
else
{
if(s[i]=='(')
{
while(exp[etop-1]!=')')
{
ss[stop++]=exp[--etop];
ss[stop++]=' ';
}
--etop;
}
else
{
while(etop&&prior(s[i],exp[etop-1]))
{
ss[stop++]=exp[--etop];
ss[stop++]=' ';
}
exp[etop++]=s[i];
}
}
}
}
if(ss[stop-1]!=' ')
ss[stop++]=' ';
while(etop)
{
ss[stop++]=exp[--etop];
ss[stop++]=' ';
}
for(int i=stop-1;i>=0;i--)
cout<<ss[i];
cout<<endl;
}
}
void init()
{
cout<<"Please select the input method"<<endl;
cout<<"1.prefix"<<endl;
cout<<"2.infix"<<endl;
cout<<"3.suffix"<<endl;
int ord;
char s[N];
cin>>ord;
getchar();//不在这里加getchar下面的gets直接读换行,没有输入
if(ord<1||ord>3)
{
cout<<"Wrong input!"<<endl;
init();
}
else
{
cout<<"Please input the expression"<<endl;
gets(s);
int l=strlen(s);
etop=ntop=0;
if(ord==1)
work1(s,l);
else
if(ord==2)
work2(s,l);
else
work3(s,l);
}
}
int main()
{
init();
return 0;
}