用c语言编程实现整型数的四则( +, -, *, ⼀ )运算,其中可以含小括号,且内括号处理的优先级高于外括号。

2025-03-12 21:15:45
推荐回答(1个)
回答1:

我有几个版本:
#include
#include
#include
#define maxn 10001
struct NumStack
{
double stack[maxn];
int top;
} numstack;
struct OpStack
{
char stack[maxn];
int top;
} opstack;
char rpn[maxn];
int GetLevel(char op)
{
if(op=='^')
return 3;
if(op=='*'||op=='/')
return 2;
if(op=='+'||op=='-')
return 1;
return 0;
}
double Add(double a,double b)
{
return a+b;
}
double Sub(double a,double b)
{
return a-b;
}
double Mul(double a,double b)
{
return a*b;
}
double Div(double a,double b)
{
return a/b;
}
double Caculate(double a,double b,char op)
{
switch(op)
{
case '+':
return Add(a,b);
case '-':
return Sub(a,b);
case '*':
return Mul(a,b);
case '/':
return Div(a,b);
case '^':
return pow(a,b);
}
}
void GetRpn(char *str)
{
int cou=0;
int isnum=0;
opstack.top=0;
if(str[0]=='-')
{
rpn[cou++]='0';
rpn[cou++]='#';
}
for(int i=0; i {
if(str[i]>='0'&&str[i]<='9'||str[i]=='.')
{
isnum=1;
rpn[cou++]=str[i];
}
else
{
if(isnum)
{
rpn[cou++]='#';
isnum=0;
}
if(str[i]=='=')
break;
else if(str[i]=='(')
{
if(str[i+1]=='-')
{
rpn[cou++]='0';
rpn[cou++]='#';
}
opstack.stack[opstack.top++]=str[i];
}
else if(opstack.top==0)
opstack.stack[opstack.top++]=str[i];
else if(str[i]==')')
{
while(opstack.stack[opstack.top-1]!='(')
rpn[cou++]=opstack.stack[--opstack.top];
opstack.top--;
}
else if(GetLevel(str[i])<=GetLevel(opstack.stack[opstack.top-1]))
{
while(opstack.top>0&&GetLevel(str[i])<=GetLevel(opstack.stack[opstack.top-1]))
rpn[cou++]=opstack.stack[--opstack.top];
opstack.stack[opstack.top++]=str[i];
}
else
opstack.stack[opstack.top++]=str[i];
}
}
while(opstack.top)
rpn[cou++]=opstack.stack[--opstack.top];
rpn[cou]='\0';
}
double GetAns(char *str)
{
numstack.top=0;
for(int i=0; i {
if(str[i]>='0'&&str[i]<='9')
{
double ita=0,now=10;
int flag=0;
for(; str[i]!='#'; i++)
{
if(str[i]=='.')
{
flag=1;
i++;
}
if(flag==0)
{
ita=ita*10+str[i]-'0';
}
else
{
ita+=(str[i]-'0')/now;
now*=10;
}
}
numstack.stack[numstack.top++]=ita;
}
else
{
double a=numstack.stack[--numstack.top];
double b=numstack.stack[--numstack.top];
if(str[i]=='/'&&a==0)return -1000000;
double c=Caculate(b,a,str[i]);
// printf("%lf %lf %lf\n",a,b,c);
numstack.stack[numstack.top++]=c;
}
}
return numstack.stack[--numstack.top];
}
int main()
{
char str[1001];
int t;
while(scanf("%d",&t)!=EOF)
{
int kase=1;
while(t--)
{
scanf("%s",str);
GetRpn(str);
//printf("%s\n",rpn);
double ans=GetAns(rpn);
printf("Case #%d: ",kase++);
if(ans==-1000000)printf("Error!\n");
else printf("%.2lf\n",ans);
}
}
return 0;
}

#include
#include
#include
#include
#include
using namespace std;
int priority(char c)
{
if(c == '=') return 0;
if(c == '+') return 1;
if(c == '-') return 1;
if(c == '*') return 2;
if(c == '/') return 2;
return 0;
}
void compute(stack& Num,stack& Op)
{
double b = Num.top();
Num.pop();
double a = Num.top();
Num.pop();
switch(Op.top())
{
case '+':Num.push(a+b);break;
case '-':Num.push(a-b);break;
case '*':Num.push(a*b);break;
case '/':Num.push(a/b);break;
}
Op.pop();
}
int main()
{
int z;
char str[1005];
stack Num;
stack Op;
scanf("%d",&z);
int kase=1;
while(z--)
{
scanf("%s",str);
int len = strlen(str);
for(int i=0;i {
if(isdigit(str[i]))
{
double n = atof(&str[i]);
while(i i++;
i--;
Num.push(n);
}
else
{
if(str[i] == '(')
Op.push(str[i]);
else if(str[i] == ')')
{
while(Op.top()!='(')
compute(Num,Op);
Op.pop();
}
else if(Op.empty() || priority(str[i])>priority(Op.top()))
Op.push(str[i]);
else
{
while(!Op.empty() && priority(str[i])<=priority(Op.top()))
compute(Num,Op);
Op.push(str[i]);
}
}
}
Op.pop();
printf("Case #%d: %.2f\n",kase++,Num.top());
Num.pop();
}
return 0;
}