C语言实现计算器,附源码,超简单!

 1 #include<stdio.h>
 2 #include<math.h>
 3 
 4 void main()
 5 {
 6     calculator();
 7 }
 8 
 9 double calculator()
10 {
11     // 分别存放第一个操作数和第二个操作数以及结果的变量
12     double x1,x2,result;
13 
14     // 存放运算符的变量
15     char m;
16 
17     while(1)
18     {
19         printf("请输入第一个数:
");
20         // 下面这得注意,接收double型的数据得用lf%,接收float用f%
21         scanf("%lf",&x1);
22 
23         printf("请输入运算操作(+ - * /):
");
24         m = getche();
25         printf("
");
26 
27         printf("请输入第二个数:
");
28         scanf("%lf",&x2);
29 
30         switch(m)
31         {
32             case '+':
33                 printf("加法
");
34                 result = x1 + x2;
35                 printf("%lf + %lf = %lf
",x1,x2,result);
36                 break;
37 
38             case '-':
39                 printf("减法
");
40                 result = x1 - x2;
41                 printf("%lf - %lf = %lf
",x1,x2,result);
42                 break;
43 
44             case '*':
45                 printf("乘法
");
46                 result = x1 * x2;
47                 printf("%lf * %lf = %lf
",x1,x2,result);
48                 break;
49 
50             case '/':
51                 printf("除法
");
52                 if(x2 == 0)
53                 {
54                     printf("除数不能为0.
");
55                 }
56                 else
57                 {
58                     result = x1 / x2;
59                     printf("%lf / %lf = %lf
",x1,x2,result);
60                 }
61                 break;
62 
63             default:
64                 break;
65         }
66     }
67 
68     return 0.0;
69 }
原文地址:https://www.cnblogs.com/wsg25/p/8696868.html