当前位置:网站首页>后缀式的计算
后缀式的计算
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];
}
边栏推荐
猜你喜欢
Five Minutes Introductory Text Processing Three Musketeers grep awk sed
AWS SES 的监控和告警
阿里的arthas使用,入门报错:Unable to attach to 32-bit process running under WOW64
After the tester with 10 years of service "naked resignation" from the big factory...
Zero-knowledge proof notes - private transaction, pederson, interval proof, proof of ownership
linkboy 5.0 正式发布,新增语音识别、图像识别
宝塔实测-搭建中小型民宿酒店管理源码
面试官:JVM运行时数据区包含哪几部分?作用是啥?
How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?
JS new一个构造器发生了什么?从零手写一个new方法
随机推荐
win10 uwp 使用 ScaleTransform 放大某个元素
STP基本配置及802.1D生成树协议的改进
web漏洞扫描器-awvs
Uniapp微信雪糕刺客单页小程序源码
EasyUi常用代码
【Web漏洞探索】跨站脚本漏洞
Comic | Two weeks after the boss laid me off, he hired me back and doubled my salary!
简述@RequestParam与@RequestBody参数注解
如何找到某个 ABAP structure 某字段的源头来自哪个数据库表
暴雨中的人
刷题-洛谷-P1200 你的飞碟在这儿Your Ride Is Here
Apache服务器配置多个站点
数据安全解决方案的发展
vs Code 运行一个本地WEB服务器
文章复现:超分辨率网络-VDSR
jMeter Thread group 对应的 constant timer
新式茶饮,卷完水果还能卷什么?
Tear down the underlying mechanism of the five JOINs of SparkSQL
实现菜单拖拽排序
vscode离线安装插件方法