C语言用栈实现四则运算器
时间: 2023-12-27 09:04:22 浏览: 76
用栈实现四则运算
以下是使用C语言和栈实现的四则运算器的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack {
int top;
int capacity;
int *array;
} Stack;
Stack *createStack(int capacity) {
Stack *stack = (Stack*)malloc(sizeof(Stack));
stack->top = -1;
stack->capacity = capacity;
stack->array = (int*)malloc(stack->capacity * sizeof(int));
return stack;
}
int isFull(Stack *stack) {
return stack->top == stack->capacity - 1;
}
int isEmpty(Stack *stack) {
return stack->top == -1;
}
void push(Stack *stack, int data) {
if (isFull(stack)) {
printf("Stack is full\n");
return;
}
stack->array[++stack->top] = data;
}
int pop(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
}
return stack->array[stack->top--];
}
int peek(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
}
return stack->array[stack->top];
}
int precedence(char operator) {
switch (operator) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
int evaluatePostfix(char *expression) {
Stack *stack = createStack(strlen(expression));
if (!stack) return -1;
for (int i = 0; expression[i]; i++) {
if (isdigit(expression[i])) {
push(stack, expression[i] - '0');
} else {
int op2 = pop(stack);
int op1 = pop(stack);
switch (expression[i]) {
case '+':
push(stack, op1 + op2);
break;
case '-':
push(stack, op1 - op2);
break;
case '*':
push(stack, op1 * op2);
break;
case '/':
push(stack, op1 / op2);
break;
case '^':
push(stack, pow(op1, op2));
break;
}
}
}
return pop(stack);
}
int evaluate(char *expression) {
Stack *stack = createStack(strlen(expression));
if (!stack) return -1;
for (int i = 0; expression[i]; i++) {
if (expression[i] == ' ' || expression[i] == ',') continue;
if (isdigit(expression[i])) {
int num = 0;
while (isdigit(expression[i])) {
num = num * 10 + (expression[i] - '0');
i++;
}
i--;
push(stack, num);
} else if (expression[i] == '(') {
push(stack, expression[i]);
} else if (expression[i] == ')') {
while (peek(stack) != '(') {
int op2 = pop(stack);
int op1 = pop(stack);
char operator = pop(stack);
switch (operator) {
case '+':
push(stack, op1 + op2);
break;
case '-':
push(stack, op1 - op2);
break;
case '*':
push(stack, op1 * op2);
break;
case '/':
push(stack, op1 / op2);
break;
case '^':
push(stack, pow(op1, op2));
break;
}
}
pop(stack);
} else {
while (!isEmpty(stack) && precedence(expression[i]) <= precedence(peek(stack))) {
int op2 = pop(stack);
int op1 = pop(stack);
char operator = pop(stack);
switch (operator) {
case '+':
push(stack, op1 + op2);
break;
case '-':
push(stack, op1 - op2);
break;
case '*':
push(stack, op1 * op2);
break;
case '/':
push(stack, op1 / op2);
break;
case '^':
push(stack, pow(op1, op2));
break;
}
}
push(stack, expression[i]);
}
}
while (!isEmpty(stack)) {
int op2 = pop(stack);
int op1 = pop(stack);
char operator = pop(stack);
switch (operator) {
case '+':
push(stack, op1 + op2);
break;
case '-':
push(stack, op1 - op2);
break;
case '*':
push(stack, op1 * op2);
break;
case '/':
push(stack, op1 / op2);
break;
case '^':
push(stack, pow(op1, op2));
break;
}
}
return pop(stack);
}
int main() {
char expression[100];
printf("Enter an infix expression: ");
fgets(expression, 100, stdin);
printf("Result: %d\n", evaluate(expression));
return 0;
}
```
该代码可以处理包括加、减、乘、除和幂的基本四则运算,同时可以处理括号。它首先将中缀表达式转换为后缀表达式,然后使用栈来计算表达式的值。它还定义了一些实用函数来管理栈的操作,例如push,pop和isEmpty。
阅读全文