求用以下类似程序编一个可以计算括号的计算器
#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);
}
人气:354 ℃ 时间: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();}
推荐
猜你喜欢
- 中国科学家在下列哪一项生物可以研究成果中有重大的贡献?
- 叶公子高好龙,钩以写龙,凿以写龙,屋室雕文以写龙 这句话采用什么修辞手法,有没有夸张?
- 0到底是不是偶数啊啊,→_→
- 额定功率为1800W的热水器产生20160J的热量需要多长时间?
- An orchestra is a large group of musicians.The musicians play classical music.合并为一句
- 初中物理知识动能和势能,运动的汽车具有动能,这句话正确,那么,汽车具有能量,这句话对吗?是不是任何物体都有能量!
- My grangdpa usually takes______(walk) after supper.
- 今天是星期六,过2013的2013次方天是星期几?