请用适用codeblocks软件的c语言写一段程序做一个计算器,计算器可以循环进行多次计算,收到特定指令才结束程序。计算器具有不同的计算功能选择,包括标准计算、科学计算、最大数判断。标准计算包括加减乘除和求余数。科学计算包括平方、log、开方、绝对值。log计算时,底数和真数都需由使用者键入。最大数判断功能要求使用循环依次输入数据并保存到一维数组,最大数判断功能为独立的子函数,主函数通过调用子函数求最大数。标注注释
时间: 2024-02-25 09:59:30 浏览: 114
由于您的要求比较复杂,我将为您提供大致的框架,您可以根据自己的需要进行修改和完善。以下是一个简单的计算器程序的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double add(double a, double b) {
return a + b;
}
double sub(double a, double b) {
return a - b;
}
double mul(double a, double b) {
return a * b;
}
double div(double a, double b) {
if (b == 0) {
printf("Error: Division by zero!\n");
return 0;
}
return a / b;
}
double mod(double a, double b) {
if (b == 0) {
printf("Error: Division by zero!\n");
return 0;
}
return fmod(a, b);
}
double square(double a) {
return a * a;
}
double log_calc(double a, double b) {
if (a <= 0 || b <= 0) {
printf("Error: Invalid input for logarithm!\n");
return 0;
}
return log(a) / log(b);
}
double sqrt_calc(double a) {
if (a < 0) {
printf("Error: Invalid input for square root!\n");
return 0;
}
return sqrt(a);
}
double abs_calc(double a) {
return fabs(a);
}
void max_num() {
int n, i;
double max, num;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter number 1: ");
scanf("%lf", &max);
for (i = 2; i <= n; i++) {
printf("Enter number %d: ", i);
scanf("%lf", &num);
if (num > max) {
max = num;
}
}
printf("The maximum number is: %.2lf\n", max);
}
int main() {
int choice = 0;
double a, b, result;
while (choice != 4) {
printf("1. Standard Calculator\n");
printf("2. Scientific Calculator\n");
printf("3. Max Number Calculator\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the first number: ");
scanf("%lf", &a);
printf("Enter the second number: ");
scanf("%lf", &b);
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Modulus\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
result = add(a, b);
printf("%.2lf + %.2lf = %.2lf\n", a, b, result);
break;
case 2:
result = sub(a, b);
printf("%.2lf - %.2lf = %.2lf\n", a, b, result);
break;
case 3:
result = mul(a, b);
printf("%.2lf * %.2lf = %.2lf\n", a, b, result);
break;
case 4:
result = div(a, b);
printf("%.2lf / %.2lf = %.2lf\n", a, b, result);
break;
case 5:
result = mod(a, b);
printf("%.2lf %% %.2lf = %.2lf\n", a, b, result);
break;
default:
printf("Error: Invalid choice!\n");
break;
}
break;
case 2:
printf("1. Square\n");
printf("2. Logarithm\n");
printf("3. Square Root\n");
printf("4. Absolute Value\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the number: ");
scanf("%lf", &a);
result = square(a);
printf("%.2lf^2 = %.2lf\n", a, result);
break;
case 2:
printf("Enter the base: ");
scanf("%lf", &a);
printf("Enter the number: ");
scanf("%lf", &b);
result = log_calc(b, a);
printf("log_%.2lf(%.2lf) = %.2lf\n", a, b, result);
break;
case 3:
printf("Enter the number: ");
scanf("%lf", &a);
result = sqrt_calc(a);
printf("sqrt(%.2lf) = %.2lf\n", a, result);
break;
case 4:
printf("Enter the number: ");
scanf("%lf", &a);
result = abs_calc(a);
printf("|%.2lf| = %.2lf\n", a, result);
break;
default:
printf("Error: Invalid choice!\n");
break;
}
break;
case 3:
max_num();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Error: Invalid choice!\n");
break;
}
}
return 0;
}
```
在这段程序中,我们定义了一些函数来执行不同的计算操作,例如加法、减法、乘法、除法、求余数、平方、对数、平方根和绝对值。我们还定义了一个子函数来计算输入的数字中的最大值。主函数使用一个 while 循环,让用户可以进行多次计算,直到选择退出程序。在循环中,我们提供了三种不同类型的计算器:标准计算器、科学计算器和最大数计算器。用户可以根据自己的需要选择相应的计算器。如果用户输入了无效的选项,程序会显示错误消息并要求重新输入。
阅读全文