【练习2-9】整数4则运算:输入2个正整数,计算并输出它们的和、差、积、商。
我这里做了少少改进,不仅限于整数,并对除数不为0做了判断
- #include <stdio.h>
- #include <stdlib.h>
- #define DOUBLE_EPS 1e-15
-
- int main(int argc, char *argv[]) {
- double x, y;
- printf("Please Input a number:");
- scanf("%lf", &x);
- printf("Please Input another number:");
- scanf("%lf", &y);
- printf("x+y=%.2f\n", x + y);
- printf("x-y=%.2f\n", x - y);
- printf("x×y=%.2f\n", x * y);
- if(fabs(y) <= DOUBLE_EPS) {
- // double 判零
- printf("0 不能为除数\n");
- } else {
- printf("x÷y=%4.2f\n", x / y);
- }
- return 0;
- }
-
复制