"Data structure and algorithm" in the prefix expression, suffix expression conversion

Source: Internet
Author: User
Tags printf

First, the calculation of inverse Polish expressions is discussed in the previous blog post, and here we discuss how infix expressions can be converted to postfix expressions

Example of a problem: how to convert 1+ (2-3) *4+10/5 to a suffix expression 1 2 3-4 * + 10 5/+ output of this type

Problem Analysis:

The first step, first encountered the first input is the number 1, the number in the suffix expression is the direct output, followed by the symbol "+" into the stack

The second step, the third character is "(", is still a symbol, this time the symbol into the stack, followed by the number 2, the direct output, and then the symbol "-", this time the symbol is still in the stack

The third step, followed by the number 3, the output, followed by the ")", at this time, we need to match the stack of "(", and then match the top data stack before the stack:

The fourth step, followed by the symbol "*", directly into the stack:

The fifth step, encountered the number 4, the direct output, followed by the symbol "+", at this time the top element of the stack is the symbol "*", in accordance with the first multiplication after the principle of adding and subtraction, at this time the top of the stack multiplication sign priority than the plus sign to the stack, so out of the stack. and the second element in the stack is a plus, according to the principle of first-come, this time "+" also to out of the stack.

The sixth step, followed by the number 10, the direct output, and finally the symbol "/", into the stack:

The seventh step, the last number 5, this time directly output 5, but the stack still has data, at this time can be the stack of symbols in turn out of the stack.

Two, Code Analysis:

#include <stdio.h> #include <stdlib.h> #include <ctype.h> #define STACK_INIT_SIZE #define STACKINCR
Ement #define MAXBUFFER-typedef char ELEMTYPE;
    typedef struct {Elemtype *base;
    Elemtype *top;
int stackSize;

}sqstack;
    void Initstack (Sqstack *s) {s->base = (Elemtype *) malloc (stack_init_size * sizeof (elemtype));

    if (!s->base) exit (0);
    S->top = s->base;

S->stacksize = stack_init_size; 
    } void Push (Sqstack *s, elemtype e) {//If the stack is bound to increase space, so before you make a judgment if (S->top-s->base >= s->stacksize)
        {s->base = (Elemtype *) realloc (s->base, (s->stacksize + stackincrement) * sizeof (elemtype));

        if (!s->base) exit (0);
        S->top = S->base + s->stacksize;
    S->stacksize = s->stacksize + stackincrement;
    } * (s->top) = e;

s->top++; } void Pop (Sqstack *s, Elemtype *e) {if (s->top = = s->base) return; *e = *--(s->top);

POPs the top element of the stack and modifies the top pointer} int Stacklen (Sqstack s) {return (s.top-s.base);}
    int main () {sqstack s;


    Elemtype c,e;

    Initstack (&s);
    printf ("Please enter infix expression, with # as the end flag:");

    scanf ("%c", &c);
        while (c! = ' # ') {if (c >= ' 0 ' && C <= ' 9 ') {printf ("%c", c);
            } else if (') ' = = c) {Pop (&s, &e);
                while (' ('! = e) {printf ("%c", e);
            Pop (&s, &e); }} else if (' + ' = = c | | '-' = = C ' {if (!
            Stacklen (s)) {Push (&s, C);
                       } else {do {Pop (&s, &e);
                       if (' (' = = e) {Push (&s, E);
              } else          {printf ("%c", e);
                    }}while (Stacklen (s) && ' ('! = e);
            Push (&s, C); }} else if (' * ' = = c | | '/' = = c | |
        ' (' = = c) {Push (&s, C);
            } else {printf ("Input error, please reenter!\n");
        return-1;
    } scanf ("%c", &c);
        } while (Stacklen (s)) {Pop (&s, &e);
    printf ("%c", e);
} return 0;
 }

The final result

You can see that the results are directly linked together, it is difficult to tell whether it is 123 or 1 2 3, this time to make a change in the output, the printf ("%c" to printf ("%c", added a space, but there is a result:

This will see that 10 is split into 1 0, which is obviously unreasonable. At this point, modify the program, the main function main is modified as follows:

                         

int main () {sqstack s;


    Elemtype c,e;

    Initstack (&s);
    printf ("Please enter infix expression, with # as the end flag:");

    scanf ("%c", &c);
            while (c = ' # ') {while (c >= ' 0 ' && C <= ' 9 ') {printf ("%c", c);
            scanf ("%c", &c);
            if (c< ' 0 ' | | c> ' 9 ') {printf ("");
            }} if (') ' = = c) {Pop (&s, &e);
                while (' ('! = e) {printf ("%c", e);
            Pop (&s, &e); }} else if (' + ' = = c | | '-' = = C ' {if (!
            Stacklen (s)) {Push (&s, C);
                       } else {do {Pop (&s, &e);
                       if (' (' = = e) {Push (&s, E);
                      } else {printf ("%c", e);
                    }}while (Stacklen (s) && ' ('! = e);
            Push (&s, C); }} else if (' * ' = = c | | '/' = = c | |
        ' (' = = c) {Push (&s, C);
        } else if (' # ' = = c) {break;
            } else {printf ("Input error, please reenter!\n");
        return-1;
    } scanf ("%c", &c);
        } while (Stacklen (s)) {Pop (&s, &e);
    printf ("%c", e);
} return 0;
 }

Finally get the desired results:





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.