Application of Data Structure "Stack" implementation: parsing arithmetic expressions and computing evaluate (C #/Java)

Source: Internet
Author: User

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 );
}
}

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.