例3-9 四则运算
程序核心——switch循环语句
程序
#include<stdio.h>
int main()
{
double value1,value2;
char op;
printf("Type in an expression:");
scanf("%lf%c%lf",&value1,&op,&value2);
switch(op)
{
case'+':printf("=%.2f",value1+value2);
break;
case'-':printf("=%.2f",value1-value2);
break;
case'*':printf("=%.2f",value1*value2);
break;
case'/':printf("=%.2f",value1/value2);
break;
default:printf("Unknown operator\n");
break;
}
return 0;
}
结果
Type in an expression:9.9+0.01
=9.91
--------------------------------
Process exited after 14.53 seconds with return value 0
请按任意键继续. . .
分析
重点:switch语句判断的是一个常量