/*
* Biao. cpp
*
* Created on: 2011-11-25
* Author: allenjin
*/
# Include <iostream>
# Include < String . H>
Using Namespace STD;
Template < Class T>
Class Stack { // Custom stack formula description
Public : Stack () {Top = 0 ;}
T top (){ Return A [Top];}
Void Pop () {top --;}
Bool Isempty (){
If (Top = 0 ) Return True ;
Else Return False ;}
Void Push (t B)
{
Top ++;
A [Top] = B ;}
Private :
T [ 100 ]; Int Top;
};
Void Chpost ( Char Inorder [], Char Post [], Int & M) // Converts an infix expression to a suffix expression. The length of the parameter M-tracking suffix expression
{
Int N = strlen (inorder ); // Get the length of the infix expression
Stack < Char > CZF; // Define a char stack for storing operators;
For ( Int I = 0 ; I <n; I ++)
{
If (Inorder [I]> = ' 0 ' & Inorder [I] <= ' 9 ' ) // If it is an operand, add it to the suffix expression array.
{
Post [m] = inorder [I];
M ++;
}
If (Inorder [I] = ' ( ' ) CZF. Push (inorder [I]); // If it is '(', press the stack directly
If (Inorder [I] = ' + ' ) // If it is set to '+', the stack top operator is added to the array if it is set to a higher priority than the stack top operator.
{
If (! CZF. isempty () & (CZF. Top () = ' * ' | CZF. Top () = ' / ' | CZF. Top () = ' - ' ))
{Post [M ++] = CZF. Top (); CZF. Pop (); CZF. Push (inorder [I]);}
Else {CZF. Push (inorder [I]);}
}
If (Inorder [I] = ' - ' ) // If '-' is the priority of the stack top, the stack top operator is added to the array if it is low, and the '-' is pressed to the stack.
{
If (! CZF. isempty () & (CZF. Top () = ' * ' | CZF. Top () = ' / ' ))
{
Post [M ++] = CZF. Top (); CZF. Pop (); CZF. Push (inorder [I]);
}Else {CZF. Push (inorder [I]);}
}
If (Inorder [I] = ' * ' | Inorder [I] = ' / ' ) CZF. Push (inorder [I]); // If it is '*' or '/', directly press the stack
If (Inorder [I] = ' ) ' ) // If ')' is encountered, the operators in the stack will pop up until '(' ends.
{
While (CZF. Top ()! = ' ( ' )
{Post [M ++] = CZF. Top (); CZF. Pop ();}
CZF. Pop (); // Pop up '('
}
}
While (! CZF. isempty ()) // Pop up the remaining elements in the stack to the suffix expression array in sequence.
{Post [M ++] = CZF. Top (); CZF. Pop ();}
}
Int Yusuan ( Char Post [], Int N) // Evaluate by using a suffix expression
{
Stack < Int > SS; // Defines the number of operations stored in the int stack and the result of each operation.
Int A, B, C, result;
For ( Int I = 0 ; I <n; I ++)
{
If (Post [I]> = ' 0 ' & Post [I] <= ' 9 ' )
{
SS. Push (post [I]- ' 0 ' )); // Convert char type to int type
}
If (Post [I] = ' - ' )
{
B = ss. Top (); SS. Pop (); A = ss. Top (); SS. Pop ();
C = A-B; SS. Push (C );
}
If (Post [I] = ' + ' )
{
B = ss. Top (); SS. Pop (); A = ss. Top (); SS. Pop ();
C = a + B; SS. Push (C );
}
If (Post [I] = ' * ' )
{
B = ss. Top (); SS. Pop (); A = ss. Top (); SS. Pop ();
C = a * B; SS. Push (C );
}
If (Post [I] = ' / ' )
{
B = ss. Top (); SS. Pop (); A = ss. Top (); SS. Pop ();
C = A/B; SS. Push (C );
}
}
Result = ss. Top (); // Obtain the top element of the last stack.
SS. Pop (); // Clear Stack
Return Result;
}
Int Main ( Void )
{
Char In [ 100 ]; Char A; Int I = 0 ;
Cout < " Enter the infix expression (# indicates end ): " ;
While (CIN>) // Obtain input cyclically until '#' is reached
{
If (A = ' # ' ) Break ;
In [I ++] =;
}
Char Po [100 ]; Int N = 0 ;
Chpost ( In , Po, N );
Cout < " Suffix expression: " ;
For ( Int J = 0 ; J <n; j ++) cout <po [J] < " " ;
Cout <Endl;
Cout < " Calculation Result: " <Yusuan (PO, n) <Endl;
Return 0 ;
}