Intermediate/postal operational expression conversion, post-operational expression calculation

Asked 2 years ago, Updated 2 years ago, 70 views

Below is the source code I wrote, and the infixToPostfix function converts the median expression in the main function into a posterior expression, and the evalPostfix function calculates the converted posterior expression. The algorithm that I convert the median expression to the posterior expression is

As I know, when I print out the results of the infixToPostfix I made, I ask because I can't find the answer in my head even if I compile it several times in my head, "Only the numbers that are operands are output and the operators are not output." The result after converting to the posterior equation I thought is 25-34*1-+93/-, and you don't have to worry about the evalPostfix function.

#include<stdio.h>

void infixToPostfix(const char*);
int evalPostfix(const char*);

char stack[50];
char stack_result[50];
int top = -1;

char pop(char* a) {
    return a[top--];
}

void push(char* a, char data) {
    a[++top] = data;
}


void infixToPostfix(const char* c) {
    for (int i = 0; c[i] != NULL; i++) {
        if (c[i] == '(') { // left bracket
            continue;
        }
        else if (c[i] == '') { // right parenthesis
            char x = pop(stack);
            printf("%c", x);
            push(stack_result, x);
        }
        else if (c[i] >= 48 &&c[i] <=57) { // operand
            printf("%c", c[i]); 
            push(stack_result, c[i]);

        }
        else { // operator
            push(stack, c[i]);
        }
    }

    while (top > -1) {
        pop(stack);

    }


}

int evalPostfix(const char* ch) {
    for (int i = 0; ch[i] != NULL; i++) {
        if (ch[i] >= 48 &&ch[i] <=57) { // operand
            push(stack, ch[i]);
        }
        else { // operator 
            int opr2 = pop(stack); 
            int opr1 = pop(stack);

            switch (ch[i]) {
            case '+':
                push(stack, opr1 + opr2);
                break;
            case '-':
                push(stack, opr1 - opr2);
                break;
            case '*':
                push(stack, opr1 * opr2);
                break;
            case '/':
                push(stack, opr1 / opr2);
                break;
            }
        }
    }

    return pop(stack);
}



void main() {
    int result;
    const char* express = "((2-5)+((3*4)-1)-(9/3))";
    printf ("median notation: %s", express);
    printf ("backward notation: ");
    infixToPostfix(express);
    result = evalPostfix(express);
    printf("\n\nOperation Results => %d", result);
    getchar();

}

Below is the output.

Medium notation: (2-5)+(3*4)-1)-(9/3)) Rear notation: 2534 1993

Calculation Results => 0

c stack expression

2022-09-21 10:42

1 Answers

When I print out the results of the infixToPostfix I made, "Only the numbers that are operands are output, and the operators are not output."

push() and pop() for stack_result stack in the right bracket and operand processing area.

push() and pop() use top variables, so stack_result and stack will share top variables. As a result, changing one stack also causes problems because the other stack also changes.

Comment on push() and pop() for stack_result in infixToPostfix().

To resolve the problem, you must separate the top variables for each stack.


2022-09-21 10:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.