I'm trying to convert the median to the posterior, but it's not working

Asked 2 years ago, Updated 2 years ago, 89 views

#include <stdio.h>
char stack[14] = "((9/2)*(4-2))";

int top = -1;

void h_push(int in)
{
        top++;
        stack[top] = in;

}
int h_pop()
{

        int out;

        out = stack[top];
        top--;

        return out;

}


int main() {

    char input = 0;
    char outpop;

    int i;

    for (int i = 0; i<14; i++) {


        switch (stack[i])
        {
        case '(':
            break;
        case ')':
            outpop = h_pop();
            printf("%c", input);
            break;
        case '*':
            input = '*';
            h_push(input);
            break;
        case '-':
            input = '-';
            h_push(input);
            break;
        case '/':
            input = '/';
            h_push(input);
            break;
        default:
            printf("%c", stack[top]);





        }

    }
}

c++ stack

2022-09-20 16:19

1 Answers

There are countless empty spaces in the entertainment industry ``;

I don't think you're using the space with the input value (9/2)*(4-2) as a stack.

It's not... You made a stack space... Just like the direction of the existing code, when it is ), spit out the stack and when it is +-/*, you can just increase the size of the stack by stacking it on the stack h

I think it would have been solved if I thought about it a little more, but I think it could be difficult if I am not familiar with coding/computer base.

#include <stdio.h>
char stack[14];
char text[14] = "((9/2)*(4-2))";

int top = -1;
void h_push(char in)
{
        top++;
        stack[top] = in;
}
int h_pop()
{
        int out;
        out = stack[top];
        top--;
        return out;
}

int main() {
    int stacksize = 0;
    char out;
    for (int i = 0; i<14; i++) {
        switch (text[i])
        {
        case '(':
            break;
        case ')':
            for (int j=stacksize; j>0; j--) {
                out = h_pop();
                printf("%c", out);
            }
            stacksize = 0;
            break;
        case '*':
            h_push(text[i]);
            stacksize += 1;
            break;
        case '-':
            h_push(text[i]);
            stacksize += 1;
            break;
        case '/':
            h_push(text[i]);
            stacksize += 1;
            break;
        default:
            printf("%c", text[i]);
        }
    }
}


2022-09-20 16:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.