int main()
{
int a,b;
char ch;
printf("请输入x=");
scanf("%d",&a);
printf("请输入y=");
scanf("%d",&b);
printf("请输入需要进行的运算:");
scanf("%c",&ch);
switch(ch)
case '+':
printf("x+y=%d\n",a+b);
break;
case '-':
printf("x-y=%d\n",a-b);
break;
case '*':
printf("x*y=%d\n",a*b);
break;
case '/':
if(b==0)
printf("分母不能为0\n");
else
printf("x/y=%d\n",a/b);
break;
default:
printf("输入错误");
break;
return 0;
}
#include"stdio.h"
void main()
{
int x,y;
char crOperator;
printf("请输入x,y,和一个算术运算符,(+-*/中的一个),三者用空格分开,回车结束。 /n");
scanf("%d %d %c",&x,&y,&crOperator);
switch(crOperator)
{
case '+':
printf("%d",x+y);
break;
case '-':
printf("%d",x-y);
break;
case '*':
printf("%d",x*y);
break;
case '/':
printf("%d",x/y);
break;
default:
break;
}
}
if(y!=0)
printf("%d",x/y);
else
printf("除数不能为0.");
后面加个判断,触发被除数不能为0
我擦,楼主,我第一个答的诶,跟你采纳的答案一样,为啥不采纳我?心寒啊,让我们以后怎么有激情在第一时间帮助广大网友啊
#include
void main(){
float x,y;
char c;
printf("输入两个数,空格分隔:");
scanf("%f%f",&x,&y);
fflush(stdin);
printf("输入运算符(+ - * /):");
scanf("%c",&c);
switch(c)
{
case '+':
printf("%d",x+y);
break;
case '-':
printf("%d",x-y);
break;
case '*':
printf("%d",x*y);
break;
case '/':
printf("%d",x/y);
break;
default:
printf("输入错误");
break;
}
}
#include
int calc(int x, int y, char op);
int add(int x, int y);
int sub(int x, int y);
int mul(int x, int y);
int div(int x, int y);
typedef int (*pCalc)(int x, int y);
int main(void)
{
int x, y;
char op;
scanf("%d %d %c", &x, &y, &op);
printf("%d %c %d = %d", x, op, y, calc(x, y, op));
return 0;
}
int calc(int x, int y, char op)
{
int result = 0;
pCalc ptr = (void*)0;
switch(op)
{
case '+':
ptr = add;
break;
case '-':
ptr = sub;
break;
case '*':
ptr = mul;
break;
case '/':
ptr = div;
break;
default:
break;
}
if(ptr != (void*)0)
{
result = ptr(x, y);
}
return result;
}
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;
}
#include
int main()
{
int a,b;
char ch;
scanf("%d%c%d",&a,&ch,&b);//输入格式a+b
if(ch=='+')
printf("a+b=%d\n",a+b);
else if(ch=='-')
printf("a-b=%d\n",a-b);
else if(ch=='*')
printf("a*b=%d\n",a*b);
else
{
if(b!=0)
printf("a/b=%.2lf\n",(double)a/b);
else
printf("除数为0\n");
}
return 0;
}