使用C语言1.表达式求值问题,应用“栈”编写一个计算器,要求: a)支持整数(长整型范围内)四则混合运算; b)输入应是中缀表达式,对不合法的输入应有提示; c)能够输出正确计算结果; d)示例如输入5(10-3)/7,输出结果5
时间: 2024-02-05 10:14:04 浏览: 52
C语言编写的表达式求值 用栈实现
4星 · 用户满意度95%
以下是使用栈实现表达式求值的C代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_EXPR_LEN 100
typedef struct {
long long data[MAX_EXPR_LEN];
int top;
} Stack;
void init(Stack *s) {
s->top = -1;
}
int is_empty(Stack *s) {
return s->top == -1;
}
int is_full(Stack *s) {
return s->top == MAX_EXPR_LEN - 1;
}
void push(Stack *s, long long x) {
if (is_full(s)) {
printf("Error: Stack overflow\n");
exit(EXIT_FAILURE);
}
s->data[++(s->top)] = x;
}
long long pop(Stack *s) {
if (is_empty(s)) {
printf("Error: Stack underflow\n");
exit(EXIT_FAILURE);
}
return s->data[(s->top)--];
}
long long peek(Stack *s) {
if (is_empty(s)) {
printf("Error: Stack underflow\n");
exit(EXIT_FAILURE);
}
return s->data[s->top];
}
int precedence(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
long long evaluate(char *expr) {
Stack value_stack, op_stack;
init(&value_stack);
init(&op_stack);
int i = 0;
while (expr[i] != '\0') {
if (isdigit(expr[i])) {
long long num = 0;
while (isdigit(expr[i])) {
num = num * 10 + (expr[i] - '0');
i++;
}
push(&value_stack, num);
} else if (expr[i] == '+' || expr[i] == '-' || expr[i] == '*' || expr[i] == '/') {
while (!is_empty(&op_stack) && precedence(peek(&op_stack)) >= precedence(expr[i])) {
long long rhs = pop(&value_stack);
long long lhs = pop(&value_stack);
char op = pop(&op_stack);
switch (op) {
case '+':
push(&value_stack, lhs + rhs);
break;
case '-':
push(&value_stack, lhs - rhs);
break;
case '*':
push(&value_stack, lhs * rhs);
break;
case '/':
if (rhs == 0) {
printf("Error: Division by zero\n");
exit(EXIT_FAILURE);
}
push(&value_stack, lhs / rhs);
break;
}
}
push(&op_stack, expr[i]);
i++;
} else if (expr[i] == '(') {
push(&op_stack, expr[i]);
i++;
} else if (expr[i] == ')') {
while (peek(&op_stack) != '(') {
long long rhs = pop(&value_stack);
long long lhs = pop(&value_stack);
char op = pop(&op_stack);
switch (op) {
case '+':
push(&value_stack, lhs + rhs);
break;
case '-':
push(&value_stack, lhs - rhs);
break;
case '*':
push(&value_stack, lhs * rhs);
break;
case '/':
if (rhs == 0) {
printf("Error: Division by zero\n");
exit(EXIT_FAILURE);
}
push(&value_stack, lhs / rhs);
break;
}
}
if (is_empty(&op_stack) || pop(&op_stack) != '(') {
printf("Error: Mismatched parentheses\n");
exit(EXIT_FAILURE);
}
i++;
} else if (isspace(expr[i])) {
i++;
} else {
printf("Error: Invalid character\n");
exit(EXIT_FAILURE);
}
}
while (!is_empty(&op_stack)) {
long long rhs = pop(&value_stack);
long long lhs = pop(&value_stack);
char op = pop(&op_stack);
switch (op) {
case '+':
push(&value_stack, lhs + rhs);
break;
case '-':
push(&value_stack, lhs - rhs);
break;
case '*':
push(&value_stack, lhs * rhs);
break;
case '/':
if (rhs == 0) {
printf("Error: Division by zero\n");
exit(EXIT_FAILURE);
}
push(&value_stack, lhs / rhs);
break;
}
}
if (is_empty(&value_stack)) {
printf("Error: Empty expression\n");
exit(EXIT_FAILURE);
}
long long result = pop(&value_stack);
if (!is_empty(&value_stack)) {
printf("Error: Malformed expression\n");
exit(EXIT_FAILURE);
}
return result;
}
int main() {
char expr[MAX_EXPR_LEN];
printf("Enter an expression: ");
if (fgets(expr, MAX_EXPR_LEN, stdin) == NULL) {
printf("Error: Failed to read input\n");
return EXIT_FAILURE;
}
printf("Result: %lld\n", evaluate(expr));
return EXIT_SUCCESS;
}
```
使用方法:
1. 编译运行上述代码,输入一个中缀表达式,按回车键确认;
2. 如果输入的表达式合法,程序会输出表达式的计算结果;
3. 如果输入的表达式不合法,程序会输出错误提示并退出。
例如,输入`5*(10-3)/7`,程序会输出`Result: 2`。
阅读全文