例3-5 四则运算
四则运算
程序核心——判断语句
程序
#include<stdio.h>
int main()
{
double value1,value2;
char op;
printf("Type in an expression:");
scanf("%lf%c%lf",&value1,&op,&value2);
if(op=='+')
printf("=%.2f\n",value1+value2);
else if (op=='-')
printf("=%.2f\n",value1-value2);
else if (op=='*')
printf("=%.2f\n",value1*value2);
else if (op=='/')
printf("=%.2f\n",value1/value2);
else
printf("Unknown operator\n");
return 0;
}
结果
Type in an expression:3*10
=30.00
--------------------------------
Process exited after 7.727 seconds with return value 0
请按任意键继续. . .
.
分析
重点:判断语句,注意判断条件,在判断字符时要加’’才可以