题目:计算器 基本要求: 1.输入:不含变量的数学表达式的中缀形式,可以接受的操作符包括+、-、 *、/、%、(、)、√(选做)和x y(选做)。 2.输出:如果表达式正确,则输出表达式的结果,如果表达式非法,则输出 错误信息。用C语言写出代码
时间: 2023-11-08 09:17:31 浏览: 217
以下是一个简单的计算器的 C 语言代码,实现了加减乘除和括号的计算,支持整数和浮点数的运算,错误处理包括除数为零和非法表达式:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_SIZE 100
typedef enum { false, true } bool;
typedef struct {
char data[MAX_SIZE];
int top;
} Stack;
void init(Stack *s) {
s->top = -1;
}
bool is_empty(Stack *s) {
return s->top == -1;
}
bool is_full(Stack *s) {
return s->top == MAX_SIZE - 1;
}
void push(Stack *s, char c) {
if (is_full(s)) {
printf("Stack overflow\n");
exit(EXIT_FAILURE);
}
s->data[++s->top] = c;
}
char pop(Stack *s) {
if (is_empty(s)) {
printf("Stack underflow\n");
exit(EXIT_FAILURE);
}
return s->data[s->top--];
}
char peek(Stack *s) {
if (is_empty(s)) {
printf("Stack underflow\n");
exit(EXIT_FAILURE);
}
return s->data[s->top];
}
int priority(char c) {
switch (c) {
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
case '√':
return 4;
default:
return -1;
}
}
bool is_digit(char c) {
return c >= '0' && c <= '9';
}
bool is_operator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '^' || c == '√';
}
double evaluate(double a, char op, double b) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0) {
printf("Divide by zero\n");
exit(EXIT_FAILURE);
}
return a / b;
case '%':
if (b == 0) {
printf("Divide by zero\n");
exit(EXIT_FAILURE);
}
return (int)a % (int)b;
case '^':
return pow(a, b);
case '√':
return sqrt(a);
default:
printf("Invalid operator\n");
exit(EXIT_FAILURE);
}
}
double calculate(char *expr) {
Stack op_stack, num_stack;
init(&op_stack);
init(&num_stack);
double num = 0;
bool has_dot = false;
while (*expr != '\0') {
if (*expr == ' ') {
expr++;
continue;
}
if (is_digit(*expr)) {
num = num * 10 + (*expr - '0');
if (has_dot) {
num /= 10.0;
}
}
else if (*expr == '.') {
has_dot = true;
}
else if (is_operator(*expr)) {
while (!is_empty(&op_stack) && priority(*expr) <= priority(peek(&op_stack))) {
char op = pop(&op_stack);
double b = pop(&num_stack);
double a = pop(&num_stack);
double result = evaluate(a, op, b);
push(&num_stack, result);
}
push(&op_stack, *expr);
num = 0;
has_dot = false;
}
else if (*expr == '(') {
push(&op_stack, *expr);
num = 0;
has_dot = false;
}
else if (*expr == ')') {
while (!is_empty(&op_stack) && peek(&op_stack) != '(') {
char op = pop(&op_stack);
double b = pop(&num_stack);
double a = pop(&num_stack);
double result = evaluate(a, op, b);
push(&num_stack, result);
}
if (!is_empty(&op_stack) && peek(&op_stack) == '(') {
pop(&op_stack);
}
else {
printf("Mismatched parentheses\n");
exit(EXIT_FAILURE);
}
num = 0;
has_dot = false;
}
else {
printf("Invalid character\n");
exit(EXIT_FAILURE);
}
expr++;
}
while (!is_empty(&op_stack)) {
char op = pop(&op_stack);
double b = pop(&num_stack);
double a = pop(&num_stack);
double result = evaluate(a, op, b);
push(&num_stack, result);
}
if (num_stack.top == 0) {
return num_stack.data[0];
}
else {
printf("Invalid expression\n");
exit(EXIT_FAILURE);
}
}
int main() {
char expr[MAX_SIZE];
printf("Enter an expression: ");
fgets(expr, MAX_SIZE, stdin);
double result = calculate(expr);
printf("Result: %g\n", result);
return 0;
}
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)