作业帮 > 综合 > 作业

求用以下类似程序编一个可以计算括号的计算器

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/12 01:33:50
求用以下类似程序编一个可以计算括号的计算器
#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);
}
将()当成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]=48 && ex[i]=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(); }