1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <stdio.h>
float add(float x, float y); float subtract(float x, float y); float multiply(float x, float y); float divide(float x, float y);
int main() { float num1, num2; char operator;
printf("请输入第一个数字: "); scanf("%f", &num1); printf("请输入第二个数字: "); scanf("%f", &num2); printf("请输入运算符 (+, -, *, /): "); scanf(" %c", &operator);
float result;
switch (operator) { case '+': result = add(num1, num2); break; case '-': result = subtract(num1, num2); break; case '*': result = multiply(num1, num2); break; case '/': if (num2 != 0) { result = divide(num1, num2); } else { printf("错误: 除数不能为0!\n"); return 1; } break; default: printf("错误: 不支持的运算符!\n"); return 1; }
printf("结果: %.2f\n", result); return 0; }
float add(float x, float y) { return x + y; }
float subtract(float x, float y) { return x - y; }
float multiply(float x, float y) { return x * y; }
float divide(float x, float y) { return x / y; }
|