Data structure Practice-suffix expression (stack)

Source: Internet
Author: User

This article is for the data structure Basic Series Network course (3): Stack and queue implementation project.

"Project-suffix expression"

The algorithm of converting an infix expression to the corresponding suffix expression is realized by using the basic operation of the stack in Sqstack.h. For example, input (56-20)/(4+2), output suffix expression:: 56#20#-4#2#+/requires the number to be appended with #.

Description of the reference solution:

Principle see the application of video stack 1-expression evaluation, you can also refer to the expression evaluation of the source code
Based on the stack structure, the algorithm steps to convert infix expressions to Postfix expressions are:

Initializes the operator stack op;' = 'into the stack;ExpRead character ch; while(ch!=' + '){if(Ch is not an operator) all subsequent digits are stored in the Postexp, followed by a character' # 'Flag value string End;Else        Switch(Precede (OP stack top operator, CH)) { Case ' < '://Stack top operator priority LowThe CH into the stack; FromExpRead the next character ch; Break; Case ' = '://Only the top of the stack operator is ' (', ch ') ' Casefallback stack; FromExpRead the next character ch; Break; Case ' > '://stack top operator should be executed first, so the stack and put into the PostexpStack up operator and store it in Postexp; Break; }}
The implementation code is as follows:

header file sqstack.h see [Sequential Stack Algorithm library], using a chain stack is also possible.

#include <stdio.h>#include <stdlib.h>#include "sqstack.h"#define MAXOP 7struct  //Set operator precedence{CharCh//Operator    intpri//Priority}lpri[]= {{' = ',0},{' (',1},{' * ',5},{'/',5},{' + ',3},{'-',3},{' ) ',6}},rpri[]= {{' = ',0},{' (',6},{' * ',4},{'/',4},{' + ',2},{'-',2},{' ) ',1}};intLeftpri (CharOp//To prioritize left operator op{intI for(i=0; i<maxop; i++)if(LPRI[I].CH==OP)returnLpri[i].pri;}intRightpri (CharOp//The priority of the right operator op{intI for(i=0; i<maxop; i++)if(RPRI[I].CH==OP)returnRpri[i].pri;}BOOLInop (CharCh//Determine if CH is an operator{if(ch==' ('|| ch==' ) '|| ch==' + '|| ch=='-'|| ch==' * '|| ch=='/')return true;Else        return false;}intPrecede (CharOP1,CharOP2)comparison results for//OP1 and OP2 operator Precedence{if(Leftpri (OP1) ==rightpri (OP2))return 0;Else if(Leftpri (OP1) <rightpri (OP2))return-1;Else        return 1;}voidTransChar*Exp,CharPostexp[])//Convert an arithmetic expression exp to a suffix expression postexp{Sqstack *opstack;//define operator Stacks    intI=0;//i as the subscript of PostexpElemtype ch; Initstack (Opstack);//Use initialization stack operation to allocate space for stack, make sure to doPush (Opstack,' = '); while(*Exp!=' + ')//exp loop When expression is not finished scanning{if(! INOP (*Exp))//Case of numeric characters{ while(*Exp>=' 0 '&& *Exp<=' 9 ')//judged as digital{postexp[i++]=*Exp;Exp++; } postexp[i++]=' # ';//Use # to mark the end of a numeric string}Else    //For the case of the operator{GetTop (opstack, ch);//Get top of stack operator            Switch(Precede (CH, *Exp))            { Case-1://stack top operator with low priority: in-StackPush (Opstack, *Exp);Exp++;//Continue to scan other characters                 Break; Case 0://Only brackets to meet this situationPop (Opstack, ch);//Will (back up the stack                Exp++;//Continue to scan other characters                 Break; Case 1://rewind stack and output to PostexpPostexp[i++]=ch; Pop (Opstack, ch); Break; }        }    }//while (*exp!= ')Pop (Opstack, ch); while(ch!=' = ')//At this time exp scan complete, back to ' = ' so far{postexp[i++]=ch;    Pop (Opstack, ch); } postexp[i]=' + ';//Add end-of-identity to postexp expressionDestroystack (opstack);}intMain () {Char Exp[]="(56-20)/(4+2)";//exp can be changed to keyboard input    Charpostexp[ $]; TransExp, postexp);printf("infix expression:%s\n",Exp);printf("suffix expression:%s\n", postexp);return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure Practice-suffix expression (stack)

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.