请选择一个功能:
1. 加法
2. 减法
3. 乘法
4. 除法
5. 取模
6. 阶乘
7. 判断素数
8. 球体体积
9. 斐波那契数列
10. 幂运算
11. 最大公约数
12. 最小公倍数
13. 交换数字
14. 排序
15. 退出
请选择一个选项:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>#define M_PI 3.1415926// 函数声明
void add(double x, double y);
void subtract(double x, double y);
void multiply(double x, double y);
void divide(double x, double y);
void modulo(double x, double y);
void factorial(int n);
void is_prime(int n);
void sphere_volume(double r);
void fibonacci(int n);
void power(double x, int n);
void gcd(int a, int b);
void lcm(int a, int b);
void swap(int* a, int* b);
void sort(int arr[], int n);// 结构体定义
typedef struct {char name[20];void (*func)(void);
} Function;// 函数指针数组
Function funcs[] = {{"加法", add},{"减法", subtract},{"乘法", multiply},{"除法", divide},{"取模", modulo},{"阶乘", factorial},{"判断素数", is_prime},{"球体体积", sphere_volume},{"斐波那契数列", fibonacci},{"幂运算", power},{"最大公约数", gcd},{"最小公倍数", lcm},{"交换数字", swap},{"排序", sort}
};// 函数实现
void add(double x, double y) {printf("%g + %g = %g\n", x, y, x + y);
}void subtract(double x, double y) {printf("%g - %g = %g\n", x, y, x - y);
}void multiply(double x, double y) {printf("%g * %g = %g\n", x, y, x * y);
}void divide(double x, double y) {if (y == 0) {printf("错误:除以零!\n");}else {printf("%g / %g = %g\n", x, y, x / y);}
}void modulo(double x, double y) {if (y == 0) {printf("错误:除以零!\n");}else {printf("%g %% %g = %g\n", x, y, fmod(x, y));}
}void factorial(int n) {if (n < 0) {printf("错误:无效的输入!\n");}else {unsigned long long fact = 1;for (int i = 1; i <= n; i++) {fact *= i;}printf("%d 的阶乘是 %llu\n", n, fact);}
}void is_prime(int n) {if (n < 2) {printf("错误:无效的输入!\n");}else {int is_prime = 1;for (int i = 2; i <= sqrt(n); i++) {if (n % i == 0) {is_prime = 0;break;}}if (is_prime) {printf("%d 是素数。\n", n);}else {printf("%d 不是素数。\n", n);}}
}void sphere_volume(double r) {if (r < 0) {printf("错误:无效的输入!\n");}else {double volume = 4.0 / 3.0 * M_PI * pow(r, 3);printf("球体的体积为 %.2f\n", volume);}
}void fibonacci(int n) {if (n < 0) {printf("错误:无效的输入!\n");}else {int a = 0, b = 1;for (int i = 1; i <= n; i++) {int tmp = a + b;a = b;b = tmp;}printf("斐波那契数列的第 %d 个数是 %d\n", n, a);}
}void power(double x, int n) {double result = 1;for(int i=0;i<n;i++) result *= x; printf("%g 的 %d 次幂是 %g\n", x, n, result);
}void gcd(int a, int b) {int aa, bb;aa = a, bb = b;if (a == 0 && b == 0) {printf("错误:无效的输入!\n");}else {while (b != 0) {int tmp = b;b = a % b;a = tmp;}printf("%d 和 %d 的最大公约数是 %d\n", aa, bb, a);}
}void lcm(int a, int b) {int aa, bb;aa = a, bb = b;if (a == 0 || b == 0) {printf("错误:无效的输入!\n");}else {int gcd_val = a * b;while (b != 0) {int tmp = b;b = a % b;a = tmp;}printf("%d 和 %d 的最小公倍数是 %d\n", aa, bb, gcd_val / a);}
}void swap(int* a, int* b) {int tmp = *a;*a = *b;*b = tmp;printf("交换后:a = %d,b = %d\n", *a, *b);
}void sort(int arr[], int n) {for (int i = 0; i < n - 1; i++) {for (int j = 0; j < n - i - 1; j++) {if (arr[j] > arr[j + 1]) {int tmp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = tmp;}}}printf("从小到大排序后:");for (int i = 0; i < n; i++) {printf("%d ", arr[i]);}printf("\n");
}int main() {int option;double x, y;int n;int a, b;double r;int* arr = NULL;int len = 0;while (1) {// 显示菜单printf("\n");printf("请选择一个功能:\n");for (int i = 0; i < 14; i++) {printf("%d. %s\n", i + 1, funcs[i].name);}printf("%d. 退出\n", 14 + 1);printf("请选择一个选项:");// 读取用户输入scanf("%d", &option);// 执行相应的函数if (option >= 1 && option <= 14) {printf("请输入参数:");switch (option) {case 1:scanf("%lf%lf", &x, &y);funcs[option - 1].func(x, y);break;case 2:scanf("%lf%lf", &x, &y);funcs[option - 1].func(x, y);break;case 3:scanf("%lf%lf", &x, &y);funcs[option - 1].func(x, y);break;case 4:scanf("%lf%lf", &x, &y);funcs[option - 1].func(x, y);break;case 5:scanf("%lf%lf", &x, &y);funcs[option - 1].func(x, y);break;case 6:scanf("%d", &n);funcs[option - 1].func(n);break;case 7:scanf("%d", &n);funcs[option - 1].func(n);break;case 8:scanf("%lf", &r);funcs[option - 1].func(r);break;case 9:scanf("%d", &n);funcs[option - 1].func(n);break;case 10:scanf("%lf%d", &x, &n);funcs[option - 1].func(x, n);break;case 11:scanf("%d%d", &a, &b);funcs[option - 1].func(a, b);break;case 12:scanf("%d%d", &a, &b);funcs[option - 1].func(a, b);break;case 13:scanf("%d%d", &a, &b);funcs[option - 1].func(&a, &b);break;case 14:printf("\n请输入数组长度:");scanf("%d", &len);if (len <= 0) {printf("错误:无效的输入!\n");}else {arr = (int*)malloc(len * sizeof(int));printf("请输入 %d 个数字:", len);for (int i = 0; i < len; i++) {scanf("%d", arr+i);}funcs[option - 1].func(arr, len);free(arr);}break;} }else if (option == 14 + 1) {break;}else {printf("错误:无效的选项!\n");}}return 0;
}