用c语言栈库函数实现表达式求解
时间: 2023-10-11 11:10:54 浏览: 56
我理解你的问题了。实现表达式求解需要用到一个栈来存储运算符和操作数,并按照一定的优先级进行计算。以下是一个示例程序,其中用到了几个重要的栈库函数(如stack_push、stack_pop等):
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct {
int capacity;
int top;
int* data;
} Stack;
Stack* stack_new(int capacity) {
Stack* s = (Stack*)malloc(sizeof(Stack));
s->capacity = capacity;
s->top = -1;
s->data = (int*)malloc(capacity * sizeof(int));
return s;
}
void stack_push(Stack* s, int value) {
if (s->top == s->capacity - 1) {
fprintf(stderr, "Error: stack is full.\n");
exit(1);
}
s->top++;
s->data[s->top] = value;
}
int stack_pop(Stack* s) {
if (s->top == -1) {
fprintf(stderr, "Error: stack is empty.\n");
exit(1);
}
int value = s->data[s->top];
s->top--;
return value;
}
int stack_top(Stack* s) {
if (s->top == -1) {
fprintf(stderr, "Error: stack is empty.\n");
exit(1);
}
return s->data[s->top];
}
int is_operator(char ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}
int operator_precedence(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
fprintf(stderr, "Error: unsupported operator '%c'.\n", op);
exit(1);
}
}
int evaluate_expression(char* expression) {
Stack* operands = stack_new(strlen(expression));
Stack* operators = stack_new(strlen(expression));
int i = 0;
while (expression[i]) {
if (isdigit(expression[i])) {
int value = expression[i] - '0';
i++;
while (isdigit(expression[i])) {
value = value * 10 + (expression[i] - '0');
i++;
}
stack_push(operands, value);
} else if (is_operator(expression[i])) {
while (!stack_top(operators)) {
char op = stack_top(operators);
if (operator_precedence(op) >= operator_precedence(expression[i])) {
int b = stack_pop(operands);
int a = stack_pop(operands);
switch (op) {
case '+':
stack_push(operands, a + b);
break;
case '-':
stack_push(operands, a - b);
break;
case '*':
stack_push(operands, a * b);
break;
case '/':
stack_push(operands, a / b);
break;
default:
fprintf(stderr, "Error: unsupported operator '%c'.\n", op);
exit(1);
}
stack_pop(operators);
} else {
break;
}
}
stack_push(operators, expression[i]);
i++;
} else if (isspace(expression[i])) {
i++;
} else {
fprintf(stderr, "Error: invalid character '%c'.\n", expression[i]);
exit(1);
}
}
while (!stack_top(operators)) {
char op = stack_top(operators);
int b = stack_pop(operands);
int a = stack_pop(operands);
switch (op) {
case '+':
stack_push(operands, a + b);
break;
case '-':
stack_push(operands, a - b);
break;
case '*':
stack_push(operands, a * b);
break;
case '/':
stack_push(operands, a / b);
break;
default:
fprintf(stderr, "Error: unsupported operator '%c'.\n", op);
exit(1);
}
stack_pop(operators);
}
int result = stack_pop(operands);
free(operands->data);
free(operands);
free(operators->data);
free(operators);
return result;
}
int main() {
char* expression = "3 * ( 4 + 5 ) - 6 / 2";
int result = evaluate_expression(expression);
printf("%s = %d\n", expression, result);
return 0;
}
```
这个程序能够处理带括号的表达式,并按照正确的优先级进行计算。你可以尝试将其复制到本地环境中运行。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)