Infix expression-to-suffix expression

Source: Internet
Author: User

Infix expression-to-suffix expression

2015-04-05 Lover Snow Child

1 //infix expression converted to suffix expression (inverse Polish expression)2#include <stdio.h>3#include <stdlib.h>4#include <math.h>5#include <ctype.h>6 7 #defineStack_init_size 20//Initial Stack size8 #defineStack_increment 10//increased memory each time the stack is expanded9 #defineMaxbuffer 10//buffersTen  OnetypedefCharElemtype;//data types in the stack Atypedefstruct { -Elemtype *Base; -Elemtype *top; the     intstackSize; - }sqstack; -  - //Initialize Stack + voidInit_stack (Sqstack *1) { -S->Base= (Elemtype *)malloc(Stack_init_size *sizeof(Elemtype)); +     if(!s->Base) AExit (-1); atS->top = s->Base; -S->stacksize =stack_init_size; - } - //into the stack - voidPush (Sqstack *S,elemtype e) { -     if(s->top-s->Base>= s->stacksize) {//detects if the stack is full in         //if full, the stack space is automatically expanded -S->Base= (Elemtype *)realloc(s->Base, (s->stacksize + stack_increment) *sizeof(Elemtype)); to         if(!s->Base) +Exit (-1); -     } the* (s->top) =e; *s->top++; $ }Panax Notoginseng //out of the stack - voidPop (Sqstack *s, Elemtype *e) { the     if(S->top = = s->Base){ +         return ; A     } the*e = * (--(s->top)); + } - //the number of stack data, because we do not modify it, so there is no need to pass pointers $ intStack_len (Sqstack s) { $     return(S.top-s.Base);//number of returned data - } -  the intMainvoid){ - Sqstack S;Wuyi     CharC; the     Chard,e; -     inti =0; Wu      -Init_stack (&s); About  $printf"Please press the input infix expression to # as the end flag: \ n"); -scanf"%c",&c); -      while(c! ='#'){ -          while(c >='0'&& C <='9'){ Aprintf"%c", c); +scanf"%c",&c); the             if(c<'0'|| C>'9'){ -printf" "); $             } the         } the         if(')'==c) { thePop (&s,&e); the              while('('!=e) { -printf"%c", e); inPop (&s,&e); the             } the}Else if('+'= = C| |'-'==c) { About             if(!Stack_len (s)) { thePush (&s,c); the}Else{ the                  Do{ +Pop (&s,&e); -                     if('('==e) { thePush (&s,e);Bayi}Else{ theprintf"%c", e); the                     } -} while(Stack_len (s) &&'('!=e); -Push (&s, c); the             } the}Else if('*'= = C | |'/'==c | |'('==c) { thePush (&s,c); the}Else if('#'==c) { -              Break; the}Else{ theprintf"Please enter the correct infix expression!!! \ n"); the             return-1;94         } thescanf"%c",&c); the     } the      while(Stack_len (s)) {98Pop (&s,&d);//The final result of the calculation is popped Aboutprintf"%c", d); -     }101printf"\ n");102     return 0;103}

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.