First, in the last blog post, we discussed the calculation of inverse Polish expressions. Here we discuss how to convert infix expressions into suffix expressions.
Question Example: How to convert 1+(2-3)*4+10/5 to output like suffix expression 1 23-4*+105/+
Problem analysis:
The first step is to first encounter a number 1, which is directly output in suffix expressions, followed by a symbol "+" on the stack.
The second step, the third character is "("), still a symbol. At this time, the symbol is put on the stack, followed by the number 2, the direct output, and then the symbol "-", while the symbol is still put on the stack.
The third step is the number 3, output, followed by "". At this time, we need to match the "() in the stack, and then the top data of the stack will be out of the stack before matching.
The fourth step, followed by the symbol "*", goes directly to the stack:
Fifth step, when the number 4 is encountered, the direct output is followed by the symbol "+", when the top element of the stack is the symbol "*", according to the principle of multiplying, dividing, adding and subtracting, the priority of the multiplication number at the top of the stack is higher than the plus sign that will enter the stack, so the stack goes out. The second element in the stack is the plus sign. According to the principle of first-come-first-come-first-come-first-come-first-come-first-come-first-come-first-come-first-come-first-come-first-come-first-come-first-come-first-come-go, at this
Step 6, followed by the number 10, the direct output, and finally the symbol "/", into the stack:
Step 7, the last number is 5. At this time, output 5 directly, but there is still data in the stack. At this time, the symbols in the stack can be out of the stack in turn.
Second, code analysis:
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #define STACK_INIT_SIZE 20 #define STACKINCREMENT 10 #define MAXBUFFER 10 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 full, there must be more space, so make a judgment before it is full. 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); //Pop up the stack top element and modify the stack top pointer } int StackLen(sqStack s) { return (s.top - s.base); } int main() { sqStack s; ElemType c,e; InitStack( &s ); printf("Please enter the infix expression to#For the end sign: "; 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 re-enter!\n"); return -1; } scanf("%c", &c); } while( StackLen(s)) { Pop( &s, &e); printf("%c", e); } return 0; }
Final results
You can see that the results are directly linked, and it is difficult to distinguish 123 or 123. At this time, we need to make a change in the output, changing printf("%c" to printf("%c"), adding a space, but the result is as follows:
In this way, we can see that 10 is split into 10, which is obviously unreasonable. Then modify the program and change the main function as follows:
int main() { sqStack s; ElemType c,e; InitStack( &s ); printf("Please enter the infix expression to#For the end sign: "; 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 re-enter!\n"); return -1; } scanf("%c", &c); } while( StackLen(s)) { Pop( &s, &e); printf("%c ", e); } return 0; }
Finally, the desired satisfactory results are achieved: