#include <stdio.h>#include"SqStack2.h"#defineStacktype Sqstack#defineBOOL int#defineTRUE 1#defineFALSE 0BOOL IsNumeric (Charc) { return(c >='0'&& C <='9') ?True:false;}intLvCharc) { intWt[] = {3,0,2,1,-1,1,-1,2}; returnWt[c-'('];}voidexpr () {CharEx[] ="(3+4) * (12-6)"; intI,n; BOOL Isnumber= FALSE;//whether the previous character is a number, two consecutive digits to mergeStacktype St; Init (&St); for(i =0; Ex[i]! =' /'; i++) { if(IsNumeric (Ex[i])) {if(Isnumber = = FALSE) printf (" "); printf ("%d", ex[i]-'0'); Isnumber=TRUE; } Else{isnumber=FALSE; if(Ex[i] = =')'){//If closing parenthesis, match the first opening parenthesis in the stackn =' '; Do { if(n! =' ') printf ("%c", N); } while(Pop (&st, &n) ==ok && n! ='('); } Else if(Ex[i] = ='(') {push (&St, Ex[i]); } Else{//other operators, all operators with higher precedence in the output stack while(GetTop (st, &n) = = OK && n! ='('&& LV (n) >=LV (ex[i])) Pop (&st, &N); Push (&St, Ex[i]); } } } while(Pop (&st, &n) = =OK) printf ("%c", N); printf ("\ n"); Destroy (&St);} Main () {expr ();}
Data structure: infix expression to suffix expression