1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void addition() 5 { 6 int a, b; 7 printf("请输入2个数字:\n"); 8 scanf("%d%d", &a, &b); 9 printf("%d + %d = %d\n", a, b, a + b); 10 } 11 12 void substraction() 13 { 14 int a, b; 15 printf("请输入2个数字:\n"); 16 scanf("%d%d", &a, &b); 17 printf("%d - %d = %d\n", a, b, a - b); 18 } 19 20 void multiplication() 21 { 22 int a, b; 23 printf("请输入2个数字:\n"); 24 scanf("%d%d", &a, &b); 25 printf("%d * %d = %d\n", a, b, a * b); 26 } 27 28 void division() 29 { 30 int a, b; 31 printf("请输入2个数字:\n"); 32 scanf("%d%d", &a, &b); 33 while (b == 0) 34 { 35 printf("请重新输入:\n"); 36 scanf("%d%d", &a, &b); 37 } 38 printf("%d / %d = %d …… %d", a, b, a / b, a % b); 39 } 40 41 void operation() 42 { 43 printf("输入数字选择对应的运算操作符\n"); 44 printf("1:加法\t2:减法\n3:乘法\t4:除法\n0:退出\n"); 45 int opera = 0; 46 scanf("%d", &opera); 47 try 48 { 49 if (opera == 1) 50 { 51 addition(); 52 } 53 else if (opera == 2) 54 { 55 substraction(); 56 } 57 else if (opera == 3) 58 { 59 multiplication(); 60 } 61 else if (opera == 4) 62 { 63 division(); 64 } 65 else if (opera == 0) 66 { 67 exit(0); 68 } 69 else 70 { 71 throw 1; 72 } 73 } 74 catch (int i) 75 { 76 printf("输入错误\n"); 77 } 78 operation(); 79 } 80 81 int main() 82 { 83 printf("欢迎使用计算机器!\n"); 84 operation(); 85 return 0; 86 }
时间: 2024-10-29 10:46:00