我是大学生,学了一部分C,想自己编个计算程序,但不知道怎么把括号加上去
就是2*3*5+2加上括号就可以变成2*3*(5+2)的
程序写了一部分,请帮忙完善一下啊
#include<stdio.h>
#include<math.h>
double add(double a,double b){
double s;
s=a+b;
return s;
}
double sub(double a,double b){
double s;
s=a-b;
return s;
}
double mul(double a,double b){
double s;
s=a*b;
return s;
}
double div(double a,double b){
double s;
s=a/b;
return s;
}
int mod(double a,double b){
int s;
s=(int)a%(int)b;
return s;
}
double power(double a,double b){
int i;
double s=1.0;
if(b==0)
return s=1;
else
for(i=1;i<=b;i++)
s*=a;
return s;
}
void main(void){
double a,b;
char c;
double sum;
scanf("%lf%c%lf",&a,&c,&b);
printf("%lf%c%lf\n",a,c,b);
switch(c){
case '+':sum=add(a,b);
break;
case '-':sum=sub(a,b);
break;
case '*':sum=mul(a,b);
break;
case '/':sum=div(a,b);
break;
case '%':sum=mod(a,b);
break;
case '^':sum=power(a,b);
break;
default:printf("It's Error!!");
}
if(c=='%')
printf("sum=%d",(int)sum);
else
printf("sum=%lf",sum);
}