当前位置:网站首页>后缀式的计算
后缀式的计算
2022-08-04 20:42:00 【柯基@】
- 后缀式为何物?
这种表示方式把运算符写在运算对象的后面,例如,把a+b写成ab+,所以也称为后缀式。 - 它有何优点?
这种表示法的优点是根据运算对象和算符的出现次序进行计算,不需要使用括号,也便于用械实现求值。
eg:原表达式:a*(b*(c+d/e)-f)
后缀式:abcde/+f-
【例】编写一个函数,求后缀式的数值,其中后缀式存于一个字符数组exp中,exp中最后一个字符为“\0”,作为结束符,并且假设后缀式中的数字都只有一位。本题中所出现的除法运算,皆为整除运算,如2/3结果为0、3/2结果为1。
int op(int a,char Op,int b){
//运算函数
if(Op=='+') return a+b;
if(Op=='-') return a-b;
if(Op=='*') return a*b;
if(Op=='/'){
if(b==0){
cout<<"ERROR"<<endl;
return 0;
}
else
return a/b;
}
}
int com(char exp[]){
int stack[maxSize]; // maxSize 为已经定义的常量
int top=-1;
int a,b,c;
int i=0;
while(exp[i]!='\0'){
if(exp[i]>='0' && exp[i]<='9')
stack[++top]=exp[i]-'0';
else{
b=stack[top--];
a=stack[top--];
c=op(a,exp[i],b);
stack[++top]=c;
}
i++;
}
return stack[top];
}
边栏推荐
猜你喜欢
随机推荐
CAS :80750-24-9(脱硫生物素 NHS 酯)
linkboy 5.0 正式发布,新增语音识别、图像识别
node 的运行命令
DICOM医学影像协议
run command for node
Debug locally and start the local server in vs code
jMeter Thread group 对应的 constant timer
Big capital has begun to flee the crypto space?
工龄10年的测试员从大厂“裸辞”后...
香港暂停进口俄罗斯部分地区禽肉及禽类产品
KubeSphere简介,功能介绍,优势,架构说明及应用场景
[TypeScript] In-depth study of TypeScript enumeration
After the tester with 10 years of service "naked resignation" from the big factory...
QT(42)-QT线程-线程调用槽函数
项目难管理?先学会用好甘特图(内附操作方法及实用模板)
Apache服务器配置多个站点
Zero-knowledge proof - zkSNARK proof system
composition-api
WIN10系统如何开启终端
[Data Mining] Written Exam Questions for Sohu Data Mining Engineers









