数据结构栈的算术表达式求值C语言
时间: 2024-03-13 07:39:10 浏览: 97
c语言算术表达式,加减乘除、乘方、括号。依次输出在求值过程中运算数栈内的栈顶数据变化过程,并最终输出表达式的值
5星 · 资源好评率100%
栈是一种后进先出(LIFO)的数据结构,它可以用来解决算术表达式求值问题。下面是一个使用栈求解算术表达式的C语言程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_STACK_SIZE 100
typedef struct {
int top;
int data[MAX_STACK_SIZE];
} 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_STACK_SIZE - 1;
}
void push(Stack *s, int x) {
if (is_full(s)) {
printf("Stack overflow\n");
exit(1);
}
s->data[++s->top] = x;
}
int pop(Stack *s) {
if (is_empty(s)) {
printf("Stack underflow\n");
exit(1);
}
return s->data[s->top--];
}
int peek(Stack *s) {
if (is_empty(s)) {
printf("Stack underflow\n");
exit(1);
}
return s->data[s->top];
}
int evaluate(char *expr) {
Stack operand_stack;
Stack operator_stack;
init(&operand_stack);
init(&operator_stack);
while (*expr != '\0') {
if (isdigit(*expr)) {
int value = 0;
while (isdigit(*expr)) {
value = value * 10 + (*expr - '0');
expr++;
}
push(&operand_stack, value);
} else if (*expr == '(') {
push(&operator_stack, *expr);
expr++;
} else if (*expr == ')') {
while (peek(&operator_stack) != '(') {
int op2 = pop(&operand_stack);
int op1 = pop(&operand_stack);
char op = pop(&operator_stack);
int result;
switch (op) {
case '+': result = op1 + op2; break;
case '-': result = op1 - op2; break;
case '*': result = op1 * op2; break;
case '/': result = op1 / op2; break;
}
push(&operand_stack, result);
}
pop(&operator_stack);
expr++;
} else if (*expr == '+' || *expr == '-' || *expr == '*' || *expr == '/') {
while (!is_empty(&operator_stack) && peek(&operator_stack) != '(' && ((*expr == '+' || *expr == '-') && (peek(&operator_stack) == '*' || peek(&operator_stack) == '/'))) {
int op2 = pop(&operand_stack);
int op1 = pop(&operand_stack);
char op = pop(&operator_stack);
int result;
switch (op) {
case '+': result = op1 + op2; break;
case '-': result = op1 - op2; break;
case '*': result = op1 * op2; break;
case '/': result = op1 / op2; break;
}
push(&operand_stack, result);
}
push(&operator_stack, *expr);
expr++;
} else {
printf("Invalid character: %c\n", *expr);
exit(1);
}
}
while (!is_empty(&operator_stack)) {
int op2 = pop(&operand_stack);
int op1 = pop(&operand_stack);
char op = pop(&operator_stack);
int result;
switch (op) {
case '+': result = op1 + op2; break;
case '-': result = op1 - op2; break;
case '*': result = op1 * op2; break;
case '/': result = op1 / op2; break;
}
push(&operand_stack, result);
}
return pop(&operand_stack);
}
int main() {
char expr[100];
printf("Enter an arithmetic expression: ");
scanf("%s", expr);
int result = evaluate(expr);
printf("Result: %d\n", result);
return 0;
}
```
该程序使用两个栈,一个用于存储操作数,另一个用于存储运算符。它遍历算术表达式中的每个字符,并根据字符的类型执行相应的操作。如果字符是数字,则将其转换为整数并将其推入操作数栈中;如果字符是左括号,则将其推入运算符栈中;如果字符是右括号,则从运算符栈中弹出运算符和操作数,执行相应的运算,并将结果推入操作数栈中,直到遇到左括号;如果字符是加、减、乘或除号,则从运算符栈中弹出运算符和操作数,执行相应的运算,并将结果推入操作数栈中,直到运算符栈为空或栈顶运算符的优先级低于当前运算符的优先级。最后,操作数栈中剩下的唯一元素就是算术表达式的值。
阅读全文