Infix expression is the ordinary operation, the operator is in infix form in the middle of the operand (example: 3 + 4), the suffix expression is reversed Polish (example: 3 4 +), infix expression suffix expression is also learning the stack in the data structure when a typical example, combined with the last write to the inverse of the Polish style. This principle can be used to enter a common formula (i.e. infix expression), converted to a suffix expression, and then through the suffix expression (inverse polish) calculation, can be obtained results.
1#include <stdio.h>2#include <stdlib.h>3 #defineStack_init_size 204 #defineStack_increment 105typedefCharElemtype;6typedefCharStatus;7 8typedefstruct9 {TenElemtype *Base; OneElemtype *top; A intstacksize; - }sqstack; - theStatus Initstack (Sqstack *S) - { -S->Base= (Elemtype *)malloc(stack_init_size*sizeof(Elemtype)); -S->top = s->Base ; +S->stacksize =stack_init_size; - + return 0; A } at - //Insert Element E, that is, into the stack -Status Push (Sqstack *S, elemtype e) - { - if(s->top-s->Base>= s->stacksize)//current capacity is greater than or equal to maximum capacity - { in //space to append stacks -S->Base= (Elemtype *)realloc(s->Base, S->stacksize + stack_increment*sizeof(Elemtype)); to if( ! S->Base ) + return-1; -S->top = s->Base+ s->stacksize; theS->stacksize = S->stacksize +stack_increment; * $ }Panax Notoginseng //Start assigning values -*s->top =e; thes->top++; + A return 0;//Note that the return value is 0, not e the } + - //pop-up element e, which is the stack $Status Pop (Sqstack *s, Elemtype *e)//here S is a variable of struct pointer, so access struct member with "--" $ { - //first determine if the stack is empty - if(S->top = = s->Base ) the return-1 ; - Wuyi--s->top; the*e = *s->top; - Wu return*e;//Note that the return value is E, not 0 - } About $ intStacklen (Sqstack S)//S is a struct variable, so access struct members with "." - { - return(S.top-s.Base ); - } A + intMain () the { - Sqstack S; $ Charc,e; theInitstack (&s); theprintf"Please enter the formula by infix expression with ' # ' as the Terminator: \ n"); thescanf"%c",&c); the while(c!='#') - { in while(c>='0'&&c<='9') the { theprintf"%c", c); Aboutscanf"%c",&c); the if(c<'0'|| C>'9') the { theprintf" "); + } - } the if(')'==c)Bayi { thePop (&s,&e); the while('('!=e) - { -printf"%c", e); thePop (&s,&e); the } the } the Else if('+'==c| |'-'==c) - { the if(!Stacklen (s)) the { thePush (&s,c);94 } the Else the { the Do98 { AboutPop (&s,&e); - if('('==e)101 {102Push (&s,e);103 }104 Else the {106printf"%c", e);107 }108} while(Stacklen (s) &&'('!=e);109Push (&s,c); the }111 } the 113 Else if('*'==c| |'/'==c| |'('==c) the { thePush (&s,c); the }117 Else if('#'==c)118 {119 Break; - }121 Else122 {123printf"\ nthe error");124 return-1; the }126scanf"%c",&c);127 } - while(Stacklen (s))129 { thePop (&s,&e);131printf"%c", e); the }133 return 0;134}
Several points of note in the above program: first, the expression of IIf (') ' ==c) ' is written on the left side of ' = = ', which helps debug the discovery of errors and prevents some programmers from "= = '"; otherwise even c= ' ) ' The compiler will also not find errors, which are considered to be assignment, and ' =c ', because the variable cannot be assigned to a constant, and the second is that the stack function (push) and the Out-stack function (POP) are necessary subroutines for the subroutine to write about the stack, which can be made into a header file, Can be called directly later. The third is the programmatic format because while (). if (),
else if () there are too many statements, there are also many "{}", the format is not easy to make mistakes and easy debugging to find errors.
There are spaces in the results of the operation, which need to be improved in the program.
infix expression converted to suffix expression