c语言利用栈求表达式的值,能够完成加减乘除及带括号的四则混合整数运算
时间: 2024-02-01 16:15:22 浏览: 85
c语言算术表达式,加减乘除、乘方、括号。依次输出在求值过程中运算数栈内的栈顶数据变化过程,并最终输出表达式的值
5星 · 资源好评率100%
好的,下面是C语言利用栈求表达式的值的代码,可以完成加减乘除及带括号的四则混合整数运算:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define STACK_INIT_SIZE 20
#define STACK_INCREMENT 10
typedef struct {
int *base;
int *top;
int stacksize;
} StackInt;
void InitStackInt(StackInt *S) {
S->base = (int *) malloc(STACK_INIT_SIZE * sizeof(int));
if (!S->base) {
printf("InitStackInt: Memory allocation failed.\n");
exit(1);
}
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
}
void DestroyStackInt(StackInt *S) {
free(S->base);
S->base = NULL;
S->top = NULL;
S->stacksize = 0;
}
void PushInt(StackInt *S, int e) {
if (S->top - S->base >= S->stacksize) {
S->base = (int *) realloc(S->base, (S->stacksize + STACK_INCREMENT) * sizeof(int));
if (!S->base) {
printf("PushInt: Memory allocation failed.\n");
exit(1);
}
S->top = S->base + S->stacksize;
S->stacksize += STACK_INCREMENT;
}
*(S->top++) = e;
}
void PopInt(StackInt *S, int *e) {
if (S->top == S->base) {
printf("PopInt: Stack is empty.\n");
exit(1);
}
*e = *(--S->top);
}
int GetTopInt(StackInt *S) {
if (S->top == S->base) {
printf("GetTopInt: Stack is empty.\n");
exit(1);
}
return *(S->top - 1);
}
int IsEmptyInt(StackInt *S) {
if (S->top == S->base)
return 1;
else
return 0;
}
int IsOperator(char c) {
if (c == '+' || c == '-' || c == '*' || c == '/')
return 1;
else
return 0;
}
int OperatorPriority(char c) {
if (c == '+' || c == '-')
return 1;
else if (c == '*' || c == '/')
return 2;
else
return 0;
}
int Calculate(int a, int b, char op) {
int result;
switch (op) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
}
return result;
}
int EvaluateExpression(char *s) {
StackInt numStack, opStack;
InitStackInt(&numStack);
InitStackInt(&opStack);
char *p = s;
while (*p != '\0') {
if (isdigit(*p)) {
int num = 0;
while (isdigit(*p)) {
num = num * 10 + (*p - '0');
p++;
}
PushInt(&numStack, num);
} else if (IsOperator(*p)) {
while (!IsEmptyInt(&opStack) && OperatorPriority(*p) <= OperatorPriority(GetTopInt(&opStack))) {
int b, a, result;
PopInt(&numStack, &b);
PopInt(&numStack, &a);
PopInt(&opStack, (int *) &result);
PushInt(&numStack, Calculate(a, b, result));
}
PushInt(&opStack, *p);
p++;
} else if (*p == '(') {
PushInt(&opStack, *p);
p++;
} else if (*p == ')') {
while (GetTopInt(&opStack) != '(') {
int b, a, result;
PopInt(&numStack, &b);
PopInt(&numStack, &a);
PopInt(&opStack, (int *) &result);
PushInt(&numStack, Calculate(a, b, result));
}
int temp;
PopInt(&opStack, &temp);
p++;
} else {
p++;
}
}
while (!IsEmptyInt(&opStack)) {
int b, a, result;
PopInt(&numStack, &b);
PopInt(&numStack, &a);
PopInt(&opStack, (int *) &result);
PushInt(&numStack, Calculate(a, b, result));
}
int result;
PopInt(&numStack, &result);
DestroyStackInt(&numStack);
DestroyStackInt(&opStack);
return result;
}
int main() {
char s[100];
printf("Enter an expression: ");
scanf("%s", s);
int result = EvaluateExpression(s);
printf("Result: %d\n", result);
return 0;
}
```
该代码使用两个栈,一个存储数字,一个存储运算符,通过扫描表达式字符串,将数字和运算符入栈,并在遇到右括号时,将栈中的数字和运算符出栈,计算结果并将结果入栈,直到遇到左括号为止。最后,将栈中的数字出栈得到表达式的值。
阅读全文