You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dgnjsxt/Multi-function calculator.c

286 lines
7.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
//两数相加
int add(int n,int m) {
return n + m;
}
//两数相减
int subtract(int n,int m) {
return n - m;
}
//两数相乘
int multiply(int n,int m) {
return n * m;
}
//两数相除
double divide(int n,int m) {
if (m == 0){
printf("Error!\n");
return 0;
}
else {
return (double)n/m;
}
}
//求模运算
int modulo(int n,int m) {
if (m == 0) {
printf("Error\n");
return 0;
}
else {
return n % m;
}
}
//求n的阶乘
long long factorial(int n){
if(n == 0 || n == 1){
return n;
}
else return n * factorial(n - 1);
}
//判断n是否为素数
int prime(int n){
int i ,j = 1;
if(n == 1 || n == 0){
j = 0;
}
else {
for(i = 2;i < n;i++){
if(n % i == 0){
j=0;
break;
}
}
}
return j;
}
//求半径为n的球的体积
double volume(int n){
if(n < 0){
printf("Error!\n");
return 0;
}
else {
return 4.0 / 3.0 * PI * n * n * n;
}
}
//求斐波那契数列的第n项值
long long int function(int n){
if(n <= 0){
printf("Error!\n");
return 0;
}
else if(n == 1||n == 2){
return 1;
}
else {
return function(n-2)+function(n-1);
}
}
//求x的n次方
long long int power(int n,int m){
if(m == 0){
return 1;
}
else if(m == 1){
return n;
}
else {
return n * power(n,m-1);
}
}
//求两个数的最大公约数
int GCD(int n,int m){
if(m == 0){
return n;
}
else{
return GCD(m,n%m);
}
}
//求两个数的最小公倍数
long long int LCM(int n,int m){
int s=GCD(n,m);
if(n == 0||m == 0){
return 0;
}
else{
return n * m / s;
}
}
//交换两个数
void exchange(int *n,int *m){
int t;
t = *n;
*n = *m;
*m = t;
}
//对输入的n个数排序
//从小到大排序
void minsort(int a[],int n){
int i,j,t,min;
for(i = 0;i < n;i++){
min = i;
for(j = i;j<n;j++){
if(a[j] < a[min]) min = j;
}
if(min != i){
t = a[min];
a[min] = a[i];
a[i] = t;
}
}
}
//从大到小排序
void maxsort(int a[],int n){
int i,j,t,max;
for(i = 0;i < n;i++){
max = i;
for(j = i;j<n;j++){
if(a[j] > a[max]) max=j;
}
if(max != i){
t = a[max];
a[max] = a[i];
a[i] = t;
}
}
}
int main() {
int num1, num2, choice;
double result;
while (1) { // 无限循环直到用户选择退出
printf("\n--------菜单--------\n");
printf("1. 两数相加\n");
printf("2. 两数相减\n");
printf("3. 两数相乘\n");
printf("4. 两数相除\n");
printf("5. 求模运算\n");
printf("6. 求n的阶乘\n");
printf("7. 判断n是否为素数\n");
printf("8. 求半径为n的球的体积\n");
printf("9. 求斐波那契数列的第n项值\n");
printf("10. 求x的n次方\n");
printf("11. 求两个数的最大公约数\n");
printf("12. 求两个数的最小公倍数\n");
printf("13. 交换两个数\n");
printf("14. 对输入的n个数排序\n");
printf("0. 退出程序\n");
printf("----------------------\n");
printf("请输入你的选择:");
scanf("%d", &choice);
switch (choice) {
case 0: // 退出程序
printf("感谢使用本程序,再见!\n");
return 0;
case 1: // 两数相加
printf("请输入两个数:");
scanf("%d %d", &num1, &num2);
result = add(num1, num2);
printf("%lf",result);
break;
case 2: // 两数相减
printf("请输入两个数:");
scanf("%d %d", &num1, &num2);
result = (double)subtract(num1, num2);
printf("%lf",result);
break;
case 3: // 两数相乘
printf("请输入两个数:");
scanf("%d %d", &num1, &num2);
result = multiply(num1, num2);
printf("%lf",result);
break;
case 4: // 两数相除
printf("请输入两个数:");
scanf("%d %d", &num1, &num2);
result = divide(num1, num2);
printf("%lf",result);
break;
case 5: // 求模运算
printf("请输入两个数:");
scanf("%d%d", &num1, &num2);
result = modulo(num1, num2);
printf("%lf",result);
break;
case 6://求n的阶乘
printf("请输入一个数:");
scanf("%d",&num1);
result = factorial(num1);
printf("%lf",result);
break;
case 7://判断n是否为素数
printf("请输入一个数:");
scanf("%d",&num1);
result = prime(num1);
if(result == 1){
printf("%d是素数",num1);
}
else {
printf("%d不是素数",num1);
}
break;
case 8://求半径为n的球的体积
printf("请输入一个数:");
scanf("%d",&num1);
result = volume(num1);
printf("%lf",result);
break;
case 9://求斐波那契数列的第n项值
printf("请输入一个数:");
scanf("%d",&num1);
result = function(num1);
printf("%lf",result);
break;
case 10://求x的n次方
printf("请输入两个数:");
scanf("%d %d", &num1, &num2);
result = power(num1,num2);
printf("%lf",result);
break;
case 11://求两个数的最大公约数
printf("请输入两个数:");
scanf("%d %d", &num1, &num2);
result = GCD(num1,num2);
printf("%lf",result);
break;
case 12://求两个数的最小公倍数
printf("请输入两个数:");
scanf("%d %d", &num1, &num2);
result = LCM(num1,num2);
printf("%lf",result);
break;
case 13://交换两个数
printf("请输入两个数:");
scanf("%d %d", &num1, &num2);
exchange(&num1,&num2);
printf("%d %d", num1, num2);
case 14://对输入的n个数排序
printf("请输入需要排序的数字的个数:");
scanf("%d",&num1);
int a[num1];
printf("请输入%d个数",num1);
for(int i = 0;i < num1;i++){
scanf("%d",&a[i]);
}
minsort(a,num1);
printf("从小到大排序;\n");
for(int i = 0;i < num1;i++){
printf("%d ",a[i]);
}
printf("\n");
maxsort(a,num1);
printf("从大到小排序;\n");
for(int i = 0;i < num1;i++){
printf("%d ",a[i]);
}
}
}
}