Java inverse Polish Algorithm

Source: Internet
Author: User

Package expression;

Import java. Io .*;
Import java. util .*;

Public class expression {
Private arraylist expression = new arraylist (); // stores the ordinal expression

Private arraylist right = new arraylist (); // stores the right-order expression

Private string result; // result

// Create an object based on input information and put the values and operators into arraylist
Private expression (string input ){
Stringtokenizer ST = new stringtokenizer (input, "+-*/()", true );
While (St. hasmoreelements ()){
Expression. Add (St. nexttoken ());
}
}

// Convert the intermediate expression to the right expression
Private void toright (){
Stacks astack = new stacks ();
String operator;
Int position = 0;
While (true ){
If (calculate. isoperator (string) expression. Get (position ))){
If (astack. Top =-1
| (String) expression. Get (position). Equals ("(")){
Astack. Push (expression. Get (position ));
} Else {
If (string) expression. Get (position). Equals (")")){
If (! (String) astack. Top (). Equals ("(")){
Operator = (string) astack. Pop ();
Right. Add (operator );
}
} Else {
If (calculate. Priority (string) Expression
. Get (position) <= calculate
. Priority (string) astack. Top ())
& Astack. Top! =-1 ){
Operator = (string) astack. Pop ();
If (! Operator. Equals ("("))
Right. Add (operator );
}
Astack. Push (expression. Get (position ));
}
}
} Else
Right. Add (expression. Get (position ));
Position ++;
If (position> = expression. Size ())
Break;
}
While (astack. Top! =-1 ){
Operator = (string) astack. Pop ();
Right. Add (operator );
}
}

// Evaluate the right-order expression
Private void getresult (){
This. toright ();
Stacks astack = new stacks ();
String OP1, OP2, is = NULL;
Iterator it = right. iterator ();

While (it. hasnext ()){
Is = (string) it. Next ();
If (calculate. isoperator (is )){
OP1 = (string) astack. Pop ();
OP2 = (string) astack. Pop ();
Astack. Push (calculate. tworesult (is, OP1, OP2 ));
} Else
Astack. Push (is );
}
Result = (string) astack. Pop ();
It = expression. iterator ();
While (it. hasnext ()){
System. Out. Print (string) it. Next ());
}
System. Out. println ("=" + result );
}

Public static void main (string AVG []) {
Try {
System. Out. println ("input a expression :");
Bufferedreader is = new bufferedreader (New inputstreamreader (
System. In ));
For (;;){
String input = new string ();
Input = is. Readline (). Trim ();
If (input. Equals ("Q "))
Break;
Else {
Expression Boya = new expression (input );
Boya. getresult ();
}
System. Out
. Println ("input another expression or input 'q' to quit :");
}
Is. Close ();
} Catch (ioexception e ){
System. Out. println ("wrong input !!! ");
}
}
}

 

Java code
Package expression;

Public class calculate {
// Determine whether it is an operator symbol
Public static Boolean isoperator (string operator ){
If (operator. Equals ("+") | Operator. Equals ("-")
| Operator. Equals ("*") | Operator. Equals ("/")
| Operator. Equals ("(") | Operator. Equals (")"))
Return true;
Else
Return false;
}

// Set the priority of the operation symbol
Public static int priority (string operator ){
If (operator. Equals ("+") | Operator. Equals ("-")
| Operator. Equals ("("))
Return 1;
Else if (operator. Equals ("*") | Operator. Equals ("/"))
Return 2;
Else
Return 0;
}

// Perform calculation between two values
Public static string tworesult (string operator, string a, string B ){
Try {
String op = operator;
String rs = new string ();
Double X = double. parsedouble (B );
Double Y = double. parsedouble ();
Double Z = 0;
If (op. Equals ("+ "))
Z = x + y;
Else if (op. Equals ("-"))
Z = x-y;
Else if (op. Equals ("*"))
Z = x * Y;
Else if (op. Equals ("/"))
Z = x/y;
Else
Z = 0;
Return Rs + z;
} Catch (numberformatexception e ){
System. Out. println ("input has something wrong! ");
Return "error ";
}
}
}

Java code
Package expression;
Import java. util .*;
// Stack
Public class stacks {
Private partition list = new partition list ();
Int Top =-1;
Public void push (object Value ){
Top ++;
List. addfirst (value );
}
Public object POP (){
Object temp = List. getfirst ();
Top --;
List. removefirst ();
Return temp;

}
Public object top (){
Return list. getfirst ();
}
}

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.