A typical example of stack application in data structures is the implementation of suffix expressions and infix expressions. The Code is as follows:
Class ccalculator {<br/> Public: <br/> ccalculator (INT sz); <br/> ~ Ccalculator (); <br/> int run (); <br/> void clean (); <br/> PRIVATE: <br/> void addoperand (INT value ); <br/> bool get2operand (Int & left, Int & right); <br/> void dooperator (char OP); <br/> void Postfix (char * mid, char * post); <br/> int ISP (char ID); <br/> int ICP (char ID); <br/> cstack * m_pstack; <br/> };
Operator priority table:
NT priotable [7] [3] ={< br/> {'#', },< br/> }, <br/> {'*', 5, 4}, <br/> {'/', 5, 4}, <br/> {'+', 3, 2 }, <br/> {'-', 3, 2}, <br/> {')', 8, 1} <br/> };
Query of the Priority table, in-stack priority and out-of-stack priority:
Int ccalculator: ISP (char ID) {<br/> int I = 0; </P> <p> for (I = 0; I <7; I ++) {<br/> If (priotable [I] [0] = ID) {<br/> return priotable [I] [1]; <br/>}< br/> return-1; <br/>}< br/> int ccalculator: ICP (char ID) {<br/> int I = 0; </P> <p> for (I = 0; I <7; I ++) {<br/> If (priotable [I] [0] = ID) {<br/> return priotable [I] [2]; <br/>}< br/> return-1; <br/>}
Other implementations:
Void ccalculator: Postfix (char * mid, char * post) {<br/> cstack * pstack = new cstack (20); <br/> pstack-> makeempty (); <br/> char * PCH = mid; <br/> // char * ptemp; <br/> int itemp; <br/> pstack-> push ('#'); <br/> while (* PCH! = '#') {<Br/> If (isdigit (* PCH) {<br/> * post ++ = * PCH; <br/>} else if (* PCH = ') {<br/> // For (* ptemp = pstack-> POP (); * ptemp! = '('; * Ptemp = pstack-> POP () {<br/> // * post ++ = * ptemp; <br/> //} <br/> for (itemp = pstack-> POP (); itemp! = '('; Itemp = pstack-> POP () {<br/> * post ++ = itemp; <br/>}< br/>} else {<br/> // For (* ptemp = pstack-> POP (); ISP (* ptemp)> ICP (* PCH); * ptemp = pstack-> POP () {<br/> // * post ++ = * ptemp; <br/> //} <br/> for (itemp = pstack-> POP (); ISP (itemp)> ICP (* PCH ); itemp = pstack-> POP () {<br/> * post ++ = itemp; <br/>}< br/> pstack-> push (itemp ); <br/> pstack-> push (* PCH); <br/>}< br/> PCH ++; <br/>}< br/> while (! Pstack-> isempty () {<br/> itemp = pstack-> POP (); <br/> If (itemp! = '#') {<Br/> * post ++ = itemp; <br/>}< br/> * post = '; <br/>}< br/> int ccalculator: Run () {<br/> // char chin [] = "1243-* + 55/-= "; <br/> char chin [] = "1 + 2*(4-3)-5/5 #"; <br/> char chout [20]; <br/> Postfix (Chin, chout); <br/> int newoperand; <br/> char * PCH = chout; <br/> while (* PCH! = ') {<Br/> switch (* PCH) {<br/> case' + ': <br/> case '-': <br/> case '*': <br/> case '/': <br/> dooperator (* PCH); <br/> break; <br/> default: <br/> addoperand (* PCH); <br/> break; <br/>}< br/> PCH ++; <br/>}</P> <p> return m_pstack-> POP (); <br/>}< br/> void ccalculator: Clean () {<br/>}< br/> void ccalculator: addoperand (INT value) {<br/> m_pstack-> push (value ); <br/>}< br/> bool ccalculator: get2operand (Int & left, I NT & right) {<br/> assert (! M_pstack-> isempty (); <br/> right = m_pstack-> POP (); <br/> assert (! M_pstack-> isempty (); <br/> left = m_pstack-> POP (); <br/> return true; <br/>}< br/> void ccalculator:: dooperator (char OP) {<br/> int ileft, iright; <br/> bool result; <br/> result = get2operand (ileft, iright ); <br/> If (result) {<br/> switch (OP) {<br/> case '+ ': <br/> m_pstack-> push (ileft + iright); <br/> break; <br/> case '-': <br/> m_pstack-> push (ileft-iright); <br/> break; <br/> case '*': <br/> m_pstack-> push (ileft * Iright); <br/> break; <br/> case '/': <br/> If (iright! = 0) {<br/> m_pstack-> push (ileft/iright); <br/>}< br/> break; <br/> default: <br/> retailmsg (true, (L "illegal operator"); <br/> break; <br/>}< br/>}
Some problems encountered in this process:
1. Definition of priotable; unable to pass compilation with priotable [] [], must specify priotable [] [3];
2. ISP and ICP; why should there be intra-stack priority and out-of-stack priority? The intra-stack priority and out-of-stack priority are mainly used in the conversion from an infix expression to a suffix expression. Infix expression, human easy to recognize; suffix expression, computer easy to process; for example a + B * (C-D)-E/F is infix expression, while ABCD-* + EF/-is a suffix expression. There are several purposes for the difference between the intra-stack priority and the off-stack priority. One is to process parentheses, and the other is to process the order of right-to-left operations with the same priority.
3. the basic idea of converting an infix expression to a suffix expression is to compare the priority of an operator and a top-stack operator when an operand is output directly or an operator is encountered. If the operator has a higher priority, it is written to the stack; if the operator in the stack has a higher priority, the operator in the stack must be processed first.
4. the pointer must be assigned a value before use. I have defined char * ptemp before, and the problem occurs.
5. bool instead of Boolean
6. l brackets are required