运算优先级按照从左到右,如:1+2*10-10/2=,输出10
代码如下:
#include <stdio.h>
int main(void){
int k,i;
char str[100], x[100];
double result[100],answer;
printf("请输入一个算式:");
scanf("%s",str);
k=i=0;
do{
result[k]=0;
do{
result[k]=result[k]*10+str[i]-'0';
i++;
}while(str[i]!='+'
&& str[i]!='-'
&& str[i]!='*'
&& str[i]!='/'
&& str[i]!='=');
if(str[i]=='=')
break;
k++;
x[k]=str[i];
k++;
i++;
}while(str[i]!='=');//simplify above
answer=result[0];//calculate below
k=1;
do{
if(x[k]=='+')
answer=answer+result[k+1];
if(x[k]=='-')
answer=answer-result[k+1];
if(x[k]=='/')
answer=answer/result[k+1];
if(x[k]=='*')
answer=answer*result[k+1];
k=k+2;
}while(x[k]!='=');
printf("%.0lf\n",answer);
return 0;
}
请问大神,哪里错了?多谢了!!