operator 就是运算符,包括加减乘除在内。不用operator会出错。会允许你输入不同的字符。当不是运算符时会出错。即使是运算符也会出错。switch里面要用operator啊,这个是要选择的啊。
不一定要用operator,可大写也可小写
main()
{ float x,y;
char operator;
scanf(“%f,%c,%f”,&x,&operator,&y)
switch(operator)
{ case `+`:printf(“\n%.2f+%.2f”,x,y,x+y);break;
case `-`:printf(“\n%.2f-%.2f”,x,y,x-y);break;
case `*`:printf(“\n%.2f*%.2f”,x,y,x*y);break;
/* case `/`:printf(“\n%.2f-%.2f”,x,y,x/y);break; */
case `/`: if(y==0)
{printf(“error!”);break;}
else
printf(“\n%.2f/%.2f”,x,y,x/y);break;
default:printf(“operator is invalid”);}}
switch(operator)
operator 就是运算符
它可以是 +,-,* /
比如
2+3, 中间的加用输入到operator这个变量里面了
case `/`: if(y==0) 如果是除法,除数为0,不合法对吧, 如果除数为0,输出error!
否则输出除法结果
{printf(“error!”);break;}
printf(“\n%.2f-%.2f”,x,y,x/y);break;