"Data structure and algorithm analysis--c language description" after reading Note 4

Source: Internet
Author: User

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

Related Article

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.