main()
{
float n,m,q;
char o;
printf("o is:*,+,-,/\n");
printf("please input n:");
scanf("%u",&n);
printf("please input m:");
scanf("%u",&m);
printf("please input o:");
o=getch();
switch(o)
{
case '+':q=n+m;break;
case '-':q=n-m;break;
case '*':q=n*m;break;
case '/':q=n/m;break;
default:printf("date input error!\n");
}
printf("n %c m =%f\n", o,q);
return 0;
}
帮忙加上先乘后加 先算括号里的代码
第1个回答 2007-12-27
//给你个全新的代码
/*输入表达式只能包含整数,+-*/和()*/
#include<stdio.h>
#include<string.h>
int add(int x,int y);
int sub(int x,int y);
int mul(int x,int y);
int div(int x,int y);
int getach(); //用于获取一个合法字符
int getid(); //对合法字符进行判断处理
int cal(); //计算表达式的值
int num,curch;
int (*func[])(int x,int y)={add,sub,mul,div}; //函数指针数组,数组元素分别指向+-*/运算函数
char chtbl[]="+-*/()=";
char corch[]="+-*/()=0123456789";
void main()
{
int value;
printf("请输入要计算的表达式,以=结束:");
getach();
while(curch!='=')
{
value=cal();
printf("结果为:%d\n",value);
printf("请输入要计算的表达式,以=结束:");
getach();
}
}
int add(int x,int y)
{
return x+y;
}
int sub(int x,int y)
{
return x-y;
}
int mul(int x,int y)
{
return x*y;
}
int div(int x,int y)
{
return x/y;
}
int getach()
{
unsigned int i;
while(true)
{
curch=getchar();
if(curch==EOF)
return -1;
for(i=0;corch[i]&&curch!=corch[i];i++)
;
if(i<strlen(corch))
break;
}
return curch;
}
int getid()
{
int i;
if(curch>='0'&&curch<='9')
{
for(num=0;curch>='0'&&curch<='9';getach())
num=num*10+curch-'0';
return -1;
}
else
{
for(i=0;chtbl[i];i++)
{
if(chtbl[i]==curch)
break;
}
if(i<=5)
getach();
return i;
}
}
int cal()
{
int x1,x2,x3,op1,op2,i;
i=getid();
if(i==4)
x1=cal();
else
x1=num;
op1=getid();
if(op1>=5)
return x1;
i=getid();
if(i==4)
x2=cal();
else
x2=num;
op2=getid();
while((op2>=0)&&(op2<5))
{
i=getid();
if(i==4)
x3=cal();
else
x3=num;
if((op1/2==0)&&(op2/2==1))
x2=func[op2](x2,x3);
else
{
x1=func[op1](x1,x2);
x2=x3;
op1=op2;
}
op2=getid();
}
return func[op1](x1,x2);
}
/*输入表达式只能包含整数,+-*/和()*/
#include<stdio.h>
#include<string.h>
int add(int x,int y);
int sub(int x,int y);
int mul(int x,int y);
int div(int x,int y);
int getach(); //用于获取一个合法字符
int getid(); //对合法字符进行判断处理
int cal(); //计算表达式的值
int num,curch;
int (*func[])(int x,int y)={add,sub,mul,div}; //函数指针数组,数组元素分别指向+-*/运算函数
char chtbl[]="+-*/()=";
char corch[]="+-*/()=0123456789";
void main()
{
int value;
printf("请输入要计算的表达式,以=结束:");
getach();
while(curch!='=')
{
value=cal();
printf("结果为:%d\n",value);
printf("请输入要计算的表达式,以=结束:");
getach();
}
}
int add(int x,int y)
{
return x+y;
}
int sub(int x,int y)
{
return x-y;
}
int mul(int x,int y)
{
return x*y;
}
int div(int x,int y)
{
return x/y;
}
int getach()
{
unsigned int i;
while(true)
{
curch=getchar();
if(curch==EOF)
return -1;
for(i=0;corch[i]&&curch!=corch[i];i++)
;
if(i<strlen(corch))
break;
}
return curch;
}
int getid()
{
int i;
if(curch>='0'&&curch<='9')
{
for(num=0;curch>='0'&&curch<='9';getach())
num=num*10+curch-'0';
return -1;
}
else
{
for(i=0;chtbl[i];i++)
{
if(chtbl[i]==curch)
break;
}
if(i<=5)
getach();
return i;
}
}
int cal()
{
int x1,x2,x3,op1,op2,i;
i=getid();
if(i==4)
x1=cal();
else
x1=num;
op1=getid();
if(op1>=5)
return x1;
i=getid();
if(i==4)
x2=cal();
else
x2=num;
op2=getid();
while((op2>=0)&&(op2<5))
{
i=getid();
if(i==4)
x3=cal();
else
x3=num;
if((op1/2==0)&&(op2/2==1))
x2=func[op2](x2,x3);
else
{
x1=func[op1](x1,x2);
x2=x3;
op1=op2;
}
op2=getid();
}
return func[op1](x1,x2);
}