堆栈实现表达式运算C语言
时间: 2024-01-02 09:14:23 浏览: 117
堆栈在表达式运算中的应用主要是为了处理运算符的优先级和括号的匹配。下面是一个使用堆栈实现表达式运算的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义堆栈结构
typedef struct {
int top; // 栈顶指针
int *data; // 数据数组
} Stack;
// 初始化堆栈
void initStack(Stack *stack, int size) {
stack->top = -1;
stack->data = (int *)malloc(size * sizeof(int));
}
// 判断堆栈是否为空
int isEmpty(Stack *stack) {
return stack->top == -1;
}
// 入栈操作
void push(Stack *stack, int value) {
stack->data[++stack->top] = value;
}
// 出栈操作
int pop(Stack *stack) {
return stack->data[stack->top--];
}
// 获取栈顶元素
int top(Stack *stack) {
return stack->data[stack->top];
}
// 判断是否为运算符
int isOperator(char op) {
return op == '+' || op == '-' || op == '*' || op == '/';
}
// 获取运算符优先级
int getPriority(char op) {
if (op == '*' || op == '/')
return 2;
else if (op == '+' || op == '-')
return 1;
else
return 0;
}
// 执行表达式运算
int evaluateExpression(char *expression) {
Stack operatorStack; // 运算符堆栈
Stack operandStack; // 操作数堆栈
initStack(&operatorStack, 100);
initStack(&operandStack, 100);
int i = 0;
while (expression[i] != '\0') {
if (expression[i] == ' ') {
i++;
continue;
}
if (isdigit(expression[i])) {
int operand = 0;
while (isdigit(expression[i])) {
operand = operand * 10 + (expression[i] - '0');
i++;
}
push(&operandStack, operand);
} else if (isOperator(expression[i])) {
while (!isEmpty(&operatorStack) && getPriority(top(&operatorStack)) >= getPriority(expression[i])) {
int op2 = pop(&operandStack);
int op1 = pop(&operandStack);
char op = pop(&operatorStack);
switch (op) {
case '+':
push(&operandStack, op1 + op2);
break;
case '-':
push(&operandStack, op1 - op2);
break;
case '*':
push(&operandStack, op1 * op2);
break;
case '/':
push(&operandStack, op1 / op2);
break;
}
}
push(&operatorStack, expression[i]);
i++;
} else if (expression[i] == '(') {
push(&operatorStack, expression[i]);
i++;
} else if (expression[i] == ')') {
while (!isEmpty(&operatorStack) && top(&operatorStack) != '(') {
int op2 = pop(&operandStack);
int op1 = pop(&operandStack);
char op = pop(&operatorStack);
switch (op) {
case '+':
push(&operandStack, op1 + op2);
break;
case '-':
push(&operandStack, op1 - op2);
break;
case '*':
push(&operandStack, op1 * op2);
break;
case '/':
push(&operandStack, op1 / op2);
break;
}
}
pop(&operatorStack); // 弹出左括号
i++;
}
}
while (!isEmpty(&operatorStack)) {
int op2 = pop(&operandStack);
int op1 = pop(&operandStack);
char op = pop(&operatorStack);
switch (op) {
case '+':
push(&operandStack, op1 + op2);
break;
case '-':
push(&operandStack, op1 - op2);
break;
case '*':
push(&operandStack, op1 * op2);
break;
case '/':
push(&operandStack, op1 / op2);
break;
}
}
int result = pop(&operandStack);
free(operatorStack.data);
free(operandStack.data);
return result;
}
int main() {
char expression[100];
printf("请输入表达式:");
gets(expression);
int result = evaluateExpression(expression);
printf("计算结果:%d\n", result);
return 0;
}
```
以上代码实现了对输入的表达式进行运算,其中使用两个堆栈分别存储运算符和操作数。通过比较运算符的优先级来决定是否进行运算,并将结果压入操作数堆栈中。最终得到的操作数堆栈中的唯一元素即为表达式的计算结果。请注意,在输入表达式时需要注意空格的处理,以方便程序的解析。
阅读全文