To convert an infix expression to a suffix expression, you must convert the expression from the infix expression form to the suffix expression form.
Equivalent expression
C # Code:
// Using System;
Class Class1
{
Public static void Main ()
{
System. Console. WriteLine ("Hello World! ");
// Infix => suffix expression
String s = "(1.9 + (20 + 41)/(25*11)-3) * 2"; // infix
String S = ""; // suffix
Char [] Operators = new char [s. Length];
Int Top =-1;
For (int I = 0; I <s. Length; I ++)
{
Char C = s [I];
Switch (C)
{
Case '': // ignore Spaces
Break;
Case '+': // Operator
Case '-':
While (Top> = 0) // when the stack is not empty
{
Char c = Operators [Top --]; // pop Operator
If (c = '(')
{
Operators [++ Top] = c; // push Operator
Break;
}
Else
{
S = S + c;
}
}
Operators [++ Top] = C; // push Operator
S + = "";
Break;
Case '*': // ignore Spaces
Case '/':
While (Top> = 0) // when the stack is not empty
{
Char c = Operators [Top --]; // pop Operator
If (c = '(')
{
Operators [++ Top] = c; // push Operator
Break;
}
Else
{
If (c = '+' | c = '-')
{
Operators [++ Top] = c; // push Operator
Break;
}
Else
{
S = S + c;
}
}
}
Operators [++ Top] = C; // push Operator
S + = "";
Break;
Case '(':
Operators [++ Top] = C;
S + = "";
Break;
Case ')':
While (Top> = 0) // when the stack is not empty
{
Char c = Operators [Top --]; // pop Operator
If (c = '(')
{
Break;
}
Else
{
S = S + c;
}
}
S + = "";
Break;
Default:
S = S + C;
Break;
}
}
While (Top> = 0)
{
S = S + Operators [Top --]; // pop Operator
}
System. Console. WriteLine (S); // suffix
// Suffix expression Calculation
Double [] Operands = new double [S. Length];
Double x, y, v;
Top =-1;
String Operand = "";
For (int I = 0; I <S. Length; I ++)
{
Char c = S [I];
If (c> = '0' & c <= '9') | c = '.')
{
Operand + = c;
}
If (c = ''| I = S. Length-1) & Operand! = "") // Update
{
Operands [++ Top] = System. Convert. ToDouble (Operand); // push Operands
Operand = "";
}
If (c = '+' | c = '-' | c = '*' | c = '/')
{
If (Operand! = ""))
{
Operands [++ Top] = System. Convert. ToDouble (Operand); // push Operands
Operand = "";
}
Y = Operands [Top --]; // The second operand of the pop binary operator (after, first, first, and foremost). Pay attention to the effect of the operand order on Division.
X = Operands [Top --]; // The first operand of the pop binary operator
Switch (c)
{
Case '+ ':
V = x + y;
Break;
Case '-':
V = x-y;
Break;
Case '*':
V = x * y;
Break;
Case '/':
V = x/y; // The first operand/The second operand. Note the effect of the operand order on Division.
Break;
Default:
V = 0;
Break;
}
Operands [++ Top] = v; // push the intermediate result into the stack again
}
}
V = Operands [Top --]; // pop final result
System. Console. WriteLine (v );
System. Console. ReadLine ();
}
}
Java Code:
Class Class1
{
Public static void main (String [] args)
{
System. out. println ("Hello World! ");
// Infix => suffix expression
String s = "(1.9 + (20 + 41)/(25*11)-3) * 2"; // infix
String S = ""; // suffix
Char [] Operators = new char [s. length ()];
Int Top =-1;
For (int I = 0; I <s. length (); I ++)
{
Char C = s. charAt (I );
Switch (C)
{
Case '':
Break;
Case '+': // Operator
Case '-':
While (Top> = 0) // when the stack is not empty
{
Char c = Operators [Top --]; // pop Operator
If (c = '(')
{
Operators [++ Top] = c; // push Operator
Break;
}
Else
{
S = S + c;
}
}
Operators [++ Top] = C; // push Operator
S + = "";
Break;
Case '*': // Operator
Case '/':
While (Top> = 0) // when the stack is not empty
{
Char c = Operators [Top --]; // pop Operator
If (c = '(')
{
Operators [++ Top] = c; // push Operator
Break;
}
Else
{
If (c = '+' | c = '-')
{
Operators [++ Top] = c; // push Operator
Break;
}
Else
{
S = S + c;
}
}
}
Operators [++ Top] = C; // push Operator
S + = "";
Break;
Case '(': // Operator
Operators [++ Top] = C;
S + = "";
Break;
Case ')': // Operator
While (Top> = 0) // when the stack is not empty
{
Char c = Operators [Top --]; // pop Operator
If (c = '(')
{
Break;
}
Else
{
S = S + c;
}
}
S + = "";
Break;
Default: // operand
S = S + C;
Break;
}
}
While (Top> = 0)
{
S = S + Operators [Top --]; // pop Operator
}
System. out. println (S); // suffix
// Suffix expression Calculation
Double [] Operands = new double [S. length ()];
Double x, y, v;
Top =-1;
String Operand = "";
For (int I = 0; I <S. length (); I ++)
{
Char c = S. charAt (I );
If (c> = '0' & c <= '9') | c = '.')
{
Operand + = c;
}
If (c = ''| I = S. length ()-1) & Operand! = "") // Update
{
Operands [++ Top] = java. lang. Double. parseDouble (Operand); // push Operands
Operand = "";
}
If (c = '+' | c = '-' | c = '*' | c = '/')
{
If (Operand! = ""))
{
Operands [++ Top] = java. lang. Double. parseDouble (Operand); // push Operands
Operand = "";
}
Y = Operands [Top --]; // The second operand of the pop binary operator (after, first, first, and foremost). Pay attention to the effect of the operand order on Division.
X = Operands [Top --]; // The first operand of the pop binary operator
Switch (c)
{
Case '+ ':
V = x + y;
Break;
Case '-':
V = x-y;
Break;
Case '*':
V = x * y;
Break;
Case '/':
V = x/y; // The first operand/The second operand. Note the effect of the operand order on Division.
Break;
Default:
V = 0;
Break;
}
Operands [++ Top] = v; // push the intermediate result into the stack again
}
}
V = Operands [Top --]; // pop final result
System. out. println (v );
}
}