求用以下类似程序编一个可以计算括号的计算器
#include
#define smax 10
#define emax 255
int power(int x,int y)
{
int t,s=1;
for (t=0; t=48 && ex[i]=rinput && rtop!=0)
{
//计算
//数字出战
top=top-1;
x=s[top];
top=top-1;
y=s[top];
s[top]=calc(o,x,y);
top=top+1;
//读出下一个栈顶符号
otop=otop-1;
o=op[otop];
if (o=='+')
rtop=0;
else
if (o=='-') rtop=1;
}
//原先的放回
op[otop]=o;
otop=otop+1;
//新的符号入栈
op[otop]=ex[i];
otop=otop+1;
}
}
i=i+1;
}
while (otop!=0)
{
//数字出战
top=top-1;
x=s[top];
top=top-1;
y=s[top];
//符号出栈
otop=otop-1;
o=op[otop];
s[top]=calc(o,x,y);
top=top+1;
}
ans= s[0];
//输出结果
printf("%s=%d\n",ex,ans);
}
人气:457 ℃ 时间:2020-06-02 06:32:30
解答
将()当成op处理即可可以具体编写一下么?谢谢/*上面同原程序*/void main(){ int s[smax]; char op[smax]; int top=0; int otop=0; int i,num,x,y; int rtop,rinput; char o; char ex[emax]; scanf("%s", ex); i=0; num=0; s[0]=0; top=1; op[0]='+'; otop=1;while (1) {if (ex[i] >=48 && ex[i]<58) { while (ex[i] >=48 && ex[i]<58)num=num*10+ex[i++]-48; s[top++]=num; num=0; } else if (ex[i]==0) break; else if (ex[i]=='(') op[otop++]=ex[i++];else if (ex[i]==')'){o=op[--otop];while (o!='(') {x=s[--top];y=s[--top];s[top++]=calc(o,x,y); o=op[--otop]; }i++;} else { rinput=rank(ex[i]);o=op[--otop];rtop=rank(o);while (rtop>=rinput && rtop!=0 && o!='('){x=s[--top]; y=s[--top]; s[top++]=calc(o,x,y); o=op[--otop]; rtop=rank(o); }op[otop++]=o;op[otop++]=ex[i++];} } while (otop!=0) {x=s[--top];y=s[--top];o=op[--otop];s[top++]=calc(o,x,y); } printf("%s=%d\n",ex,s[0]); getch();}
推荐
猜你喜欢
- 落叶阅读题答案
- 岳阳楼记的理解“不以物喜,不以己悲”、“先天下之忧而忧,后天下之乐而乐”和现在人们的观念的不同之处
- 直线y=2x=8与x轴叫于点P,平移直线y=3x,使其过点P,求平移后所得直线的表达式
- 放射与辐射的区别是什么?伦琴/小时又是什么单位,概念是什么?
- 若函数f(x)=f(x+2),x<2 2∧-3,x≥2,则f(-3)的值
- treat everyone or everything equally
- 有一个小物块,置于一个光滑的水平桌面上,一条绳的一端连接此物体,另一端穿过桌面中心的小孔,该物体原以角速度w在距孔为R的圆周上转动,今将绳从小孔缓慢往下拉,则物体
- 氯铂酸盐如何检验铵根,钾离子,钠离子和铯离子,铅离子?