Data structure: infix expression to suffix expression

Source: Internet
Author: User

#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

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.