Stack:
infix-to-suffix conversion. We only allow operation of +,*, (,).
Infix expression: a+b*c+ (d*e+f) *g, suffix expression: abc*+de*f+g*+
The program is as follows, Stack.h as shown in the previous blog post:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "stack.h" char* infix_to_ Postfix (CHAR*&NBSP;STR) { int i,j=0; int size=strlen ( STR); if (str==null) {printf ("Empty string!!! \ n "); Return null; } stack s=createstack (SIZE); char *tmpstr=malloc (sizeof (char) * (size+1)); if (Tmpstr==null) {printf ("tmpstr is empty!\n"); return null; } for (i=0;i<size;++i) {if (str[i]== ' + ') { while (! IsEmpty (s) && top (s)! = ' (' ) {tmpstr[j++]=topandpop (s); } push (' + ', s); }else if (str[i]== ' * ') {&NBSP;&NBSP;&NBSp; while (! IsEmpty (s) && top (s) = = ' * ') {tmpstr[j++]=topandpop (s); } push (' * ', s);} else if (str[i]== ' (') { push (str[i],s);} else if (str[i]== ') ' { while (Top (s)! = ' (') { Tmpstr[j++]=topandpop (s); } pop (s);} Else{ tmpstr[j++]=str[i];} } while (! IsEmpty (s)) {tmpstr[j++]=topandpop (s); } Return tmpstr;} Int main () { char ss[]= "a+b*c+ (d*e+f) *g"; char* Goal=infix_to_postfix (ss); printf ("the string is :%s\n", goal); return 0;}
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/71/71/wKiom1XQOPjyKlL_AAD0NhTKcTY823.jpg "title=" capture. JPG "alt=" Wkiom1xqopjykll_aad0nhtkcty823.jpg "/>
You can use +,-,*,, (,) in an expression after the if (str[i]== ' + ') statement in the above program is preceded by the following two statements and the corresponding else if statement is modified.
else if (str[i]== '-') {while (! IsEmpty (s) && Top (s)! = ' (') {Tmpstr[j++]=topandpop (s); } Push ('-', s);}
else if (str[i]== '/') {while (! IsEmpty (s) && (Top (s) = = ' * ' | | Top (s) = = '/')) {Tmpstr[j++]=topandpop (s); } Push ('/', s);
else if (str[i]== ' * ') {while (! IsEmpty (s) && (Top (s) = = ' * ' | | Top (s) = = '/')) {Tmpstr[j++]=topandpop (s); } Push (' * ', s);}
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/72/wKiom1XQQBXBQPRwAADsBI3lPts135.jpg "title=" capture. JPG "alt=" Wkiom1xqqbxbqprwaadsbi3lpts135.jpg "/>
"Data structure and algorithm analysis--c language description" after reading Note 4