使用C语言实现中缀表达式的计算

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 typedef struct {
  5     int* data;
  6     int top;
  7     int capacity;
  8 } Stack;
  9 
 10 void initialize(Stack* stack, int capacity) {
 11     stack->data = (int*)malloc(capacity * sizeof(int));
 12     stack->top = -1;
 13     stack->capacity = capacity;
 14 }
 15 
 16 void push(Stack* stack, int element) {
 17     if (stack->top == stack->capacity - 1) {
 18         printf("栈已满,无法入栈\n");
 19     } else {
 20         stack->data[++stack->top] = element;
 21     }
 22 }
 23 
 24 int pop(Stack* stack) {
 25     if (stack->top == -1) {
 26         printf("栈为空,无法出栈\n");
 27         return -1;
 28     } else {
 29         return stack->data[stack->top--];
 30     }
 31 }
 32 
 33 int peek(Stack* stack) {
 34     if (stack->top != -1) {
 35         return stack->data[stack->top];
 36     }
 37     return -1;
 38 }
 39 
 40 int isEmpty(Stack* stack) {
 41     return (stack->top == -1);
 42 }
 43 
 44 int isOperator(char ch) {
 45     if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
 46         return 1;
 47     else
 48         return 0;
 49 }
 50 
 51 int calculate(int operand1, int operand2, char operator) {
 52     switch (operator) {
 53         case '+':
 54             return operand1 + operand2;
 55         case '-':
 56             return operand1 - operand2;
 57         case '*':
 58             return operand1 * operand2;
 59         case '/':
 60             return operand1 / operand2;
 61         default:
 62             return 0;
 63     }
 64 }
 65 
 66 int evaluate(char* expression) {
 67     Stack operandStack;
 68     Stack operatorStack;
 69     int i = 0;
 70 
 71     initialize(&operandStack, 100);
 72     initialize(&operatorStack, 100);
 73 
 74     while (expression[i] != '\0') {
 75         if (expression[i] >= '0' && expression[i] <= '9') {
 76             int operand = 0;
 77             while (expression[i] >= '0' && expression[i] <= '9') {
 78                 operand = operand * 10 + (expression[i] - '0');
 79                 i++;
 80             }
 81             push(&operandStack, operand);
 82         } else if (isOperator(expression[i])) {
 83             while (!isEmpty(&operatorStack) && peek(&operatorStack) != '(' &&
 84                    peek(&operatorStack) != ')' && peek(&operatorStack) != '^' &&
 85                    peek(&operatorStack) != '*' && peek(&operatorStack) != '/') {
 86                 int operand2 = pop(&operandStack);
 87                 int operand1 = pop(&operandStack);
 88                 char operator = pop(&operatorStack);
 89                 int result = calculate(operand1, operand2, operator);
 90                 push(&operandStack, result);
 91             }
 92             push(&operatorStack, expression[i]);
 93             i++;
 94         } else if (expression[i] == '(') {
 95             push(&operatorStack, expression[i]);
 96             i++;
 97         } else if (expression[i] == ')') {
 98             while (!isEmpty(&operatorStack) && peek(&operatorStack) != '(') {
 99                 int operand2 = pop(&operandStack);
100                 int operand1 = pop(&operandStack);
101                 char operator = pop(&operatorStack);
102                 int result = calculate(operand1, operand2, operator);
103                 push(&operandStack, result);
104             }
105             pop(&operatorStack); // 弹出左括号
106             i++;
107         } else {
108             i++;
109         }
110     }
111 
112     while (!isEmpty(&operatorStack)) {
113         int operand2 = pop(&operandStack);
114         int operand1 = pop(&operandStack);
115         char operator = pop(&operatorStack);
116         int result = calculate(operand1, operand2, operator);
117         push(&operandStack, result);
118     }
119 
120     int finalResult = pop(&operandStack);
121 
122     return finalResult;
123 }
124 
125 int main() {
126     char expression[100];
127 
128     printf("请输入中缀表达式:");
129     fgets(expression, sizeof(expression), stdin);
130 
131     int result = evaluate(expression);
132 
133     printf("计算结果为:%d\n", result);
134 
135     return 0;
136 }

 

posted @ 2023-07-03 23:55  nullIsland01  阅读(50)  评论(0)    收藏  举报