编写一个程序,支持加、减、乘、除、余运算。
如:
1.5 + 2.1 = 3.6
3.1 - 4.1 = -1
5.5 * 6 = 33
7 / 8 = 0.875
9 % 6 = 3
注:
1、余运算之前,操作数取整。
2、连续使用,直到输入结束。用(CTRL+Z)结束输入。
这里需要输入多组数据
具体有一下形式:
// 形式 1while (scanf(..) == 3){// ..}// 形式 2while (scanf(..) > 0){// ..}// 形式 3while (scanf(..) != EOF){// ..}
这里使用第一种方法进行演示:
#include<stdio.h>int main(){double a,b,c;char op;while(scanf("%lf %c %lf",&a,&op,&b)==3){switch(op){case '+':c=a+b;break;case '-':c=a-b;break;case '*':c=a*b;break;case '/':c=a/b;break;case '%':a=(long)a;b=(long)b;c=(long)a%(long)b;break;default: ;}printf("%G %c %G = %G\n",a,op,b,c);}return 0;}
基本原理与之前的switch语句构造的简单计算器相同,只是使用到了while语句,可以输入多组数据计算