This paper is aimed at the data structure Basic Series Network course (3): The Application of the 5th lesson stack in stacks and queues 1-expression evaluation.
Example: The user enters a valid mathematical expression containing "+", "-", "*", "/", positive integers, and parentheses to calculate the result of the expression.
Answer:
#include <stdio.h>#include <stdlib.h>#define MAXOP#define MaxSizestruct //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{struct{CharData[maxsize];//store operator intTop//Stack pointer} op;//define operator Stacks intI=0;//i as the subscript of Postexpop.top=-1; op.top++;//Will ' = ' into the stackop.data[op.top]=' = '; 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 Switch(Precede (op.data[op.top],*Exp)) { Case-1://stack top operator with low priority: in-Stackop.top++; op.data[op.top]=*Exp;Exp++;//Continue to scan other characters Break; Case 0://Only brackets to meet this situationop.top--;//Will (back up the stack Exp++;//Continue to scan other characters Break; Case 1://rewind stack and output to PostexpPostexp[i++]=op.data[op.top]; op.top--; Break; } }//while (*exp!= ') while(op.data[op.top]!=' = ')//At this time exp scan complete, back to ' = ' so far{Postexp[i++]=op.data[op.top]; op.top--; } postexp[i]=' + ';//Add end-of-identity to postexp expression}floatCompvalue (Char Exp[])//Calculate the value of the suffix expression{struct{floatData[maxsize];//Store value intTop//Stack pointer} St;//define a value stack floatDCharChintt=0;//t as exp subscriptst.top=-1; Ch=Exp[T]; t++; while(ch!=' + ')//exp Loop When string is not finished scanning{Switch(CH) { Case' + ': st.data[st.top-1]=st.data[st.top-1]+st.data[st.top]; st.top--; Break; Case '-': st.data[st.top-1]=st.data[st.top-1]-st.data[st.top]; st.top--; Break; Case ' * ': st.data[st.top-1]=st.data[st.top-1]*st.data[st.top]; st.top--; Break; Case '/':if(st.data[st.top]!=0) st.data[st.top-1]=st.data[st.top-1]/st.data[st.top];Else{printf("\n\t except 0 error!\n");Exit(0);//Abnormal exit} st.top--; Break;default: d=0;//Convert numeric characters into numeric values to be stored in D while(ch>=' 0 '&& ch<=' 9 ')//numeric characters{d=Ten*d+ch-' 0 '; Ch=Exp[T]; t++; } st.top++; St.data[st.top]=d; } ch=Exp[T]; t++; }returnSt.data[st.top];}intMain () {Char Exp[]="(56-20)/(4+2)";//exp can be changed to keyboard input CharPostexp[maxsize]; TransExp, postexp);printf("infix expression:%s\n",Exp);printf("suffix expression:%s\n", postexp);printf("value of expression:%g\n", Compvalue (POSTEXP));return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure routines-expression evaluation (with stack structure)