C语言编写一个可以实现+、-、 *、/、%、(、)、√和 乘方的计算器并且输出计算器的仿真界面。
时间: 2023-07-04 09:23:39 浏览: 162
好的,以下是一个简单的 C 语言计算器程序,支持加减乘除、取模、求平方根和乘方运算。同时还有一个基于命令行的简单仿真界面。
```c
#include <stdio.h>
#include <math.h>
double evaluate(char* expr);
int main() {
char expr[100];
printf("Welcome to the calculator simulation!\n");
printf("Enter an expression to evaluate (e.g. 3+4*2): ");
while (fgets(expr, sizeof(expr), stdin)) {
double result = evaluate(expr);
printf("Result: %lf\n", result);
printf("Enter another expression: ");
}
return 0;
}
double evaluate(char* expr) {
double num_stack[100];
char op_stack[100];
int num_top = -1, op_top = -1;
int i = 0;
while (expr[i] != '\n' && expr[i] != '\0') {
if (expr[i] >= '0' && expr[i] <= '9') {
double num = 0;
while (expr[i] >= '0' && expr[i] <= '9') {
num = num * 10 + (expr[i] - '0');
i++;
}
if (expr[i] == '.') {
i++;
double fraction = 0.1;
while (expr[i] >= '0' && expr[i] <= '9') {
num += fraction * (expr[i] - '0');
fraction /= 10;
i++;
}
}
num_top++;
num_stack[num_top] = num;
} else if (expr[i] == '+' || expr[i] == '-' ||
expr[i] == '*' || expr[i] == '/' ||
expr[i] == '%' || expr[i] == '(' ||
expr[i] == ')') {
if (expr[i] == '(') {
op_top++;
op_stack[op_top] = '(';
} else if (expr[i] == ')') {
while (op_top >= 0 && op_stack[op_top] != '(') {
char op = op_stack[op_top];
op_top--;
double b = num_stack[num_top];
num_top--;
double a = num_stack[num_top];
num_top--;
double result = 0;
if (op == '+') {
result = a + b;
} else if (op == '-') {
result = a - b;
} else if (op == '*') {
result = a * b;
} else if (op == '/') {
result = a / b;
} else if (op == '%') {
result = (int)a % (int)b;
}
num_top++;
num_stack[num_top] = result;
}
op_top--;
} else {
while (op_top >= 0 && op_stack[op_top] != '(' &&
((op_stack[op_top] == '*' || op_stack[op_top] == '/') ||
(op_stack[op_top] == '+' || op_stack[op_top] == '-') ||
(op_stack[op_top] == '%' && (expr[i] == '*' || expr[i] == '/')))) {
char op = op_stack[op_top];
op_top--;
double b = num_stack[num_top];
num_top--;
double a = num_stack[num_top];
num_top--;
double result = 0;
if (op == '+') {
result = a + b;
} else if (op == '-') {
result = a - b;
} else if (op == '*') {
result = a * b;
} else if (op == '/') {
result = a / b;
} else if (op == '%') {
result = (int)a % (int)b;
}
num_top++;
num_stack[num_top] = result;
}
op_top++;
op_stack[op_top] = expr[i];
}
i++;
} else if (expr[i] == ' ') {
i++;
} else if (expr[i] == 's' && expr[i+1] == 'q' && expr[i+2] == 'r' && expr[i+3] == 't') {
op_top++;
op_stack[op_top] = 's';
i += 4;
} else if (expr[i] == '^') {
op_top++;
op_stack[op_top] = '^';
i++;
} else {
printf("Unknown character %c\n", expr[i]);
break;
}
}
while (op_top >= 0) {
char op = op_stack[op_top];
op_top--;
double b = num_stack[num_top];
num_top--;
double a = num_stack[num_top];
num_top--;
double result = 0;
if (op == '+') {
result = a + b;
} else if (op == '-') {
result = a - b;
} else if (op == '*') {
result = a * b;
} else if (op == '/') {
result = a / b;
} else if (op == '%') {
result = (int)a % (int)b;
} else if (op == 's') {
result = sqrt(a);
} else if (op == '^') {
result = pow(a, b);
}
num_top++;
num_stack[num_top] = result;
}
return num_stack[0];
}
```
运行程序后,您可以输入一个表达式,例如 `3+4*2`,然后按回车键,程序将输出计算结果。您可以继续输入更多表达式并计算结果,直到您输入 `Ctrl+C` 停止程序。
以下是一个简单的命令行仿真界面:
```
Welcome to the calculator simulation!
Enter an expression to evaluate (e.g. 3+4*2): 3+4*2
Result: 11.000000
Enter another expression: (2+3)*4
Result: 20.000000
Enter another expression: 10/3
Result: 3.333333
Enter another expression: 10%3
Result: 1.000000
Enter another expression: sqrt(16)+2
Result: 6.000000
Enter another expression: 2^3
Result: 8.000000
Enter another expression:
```