[Pin to top] evaluate Java expressions

Source: Internet
Author: User

JAVA expressions are implemented based on the characteristics of advanced stack and later release, for details, see Code

.

Public float evaluation (string textfield) {stringbuilder STR = new stringbuilder (); stack <float> STK = new stack <float> (); // used to store operations or result stack <character> stk2 = new stack <character> (); // used to store the stk2.push operator ('#'); // first a '#' in the operator stack for the first operator to compare the priority of char C; // read the character for (INT I = 0; I <textfield. length (); I ++) {Boolean isbeg = false; // whether it is a negative number or a positive number operator C = textfield. charat (I); If (C> = '0' & C <= '9') | C = '. '){ // STR can be directly combined with digits or decimal points. append (c );} else if (C = '+' | C = '-' | C = '*' | C = '/' | C = '( '| C = ') ') {If (C =' + '| C ='-') & STK. empty () & Str. length () = 0) {Str. append (c); isbeg = true;} else if (Str. length ()! = 0) {try {float F = float. parsefloat (Str. tostring (); STK. push (f) ;}catch (exception e) {flag = false; Return-1 ;}str. delete (0, str. length ();} // If the read operator priority is smaller than the element priority on the top of the operator stack, the result is calculated and saved, for example, if (Judge (stk2.peek ()> = judge (c )&&! Isbeg) {try {float B = float. parsefloat (STK. pop (). tostring (); float a = float. parsefloat (STK. pop (). tostring (); // retrieves the two numbers of the operand stack char C1 = stk2.pop (); // retrieves the operator if (C! = ') {Stk2.push (c); // low-priority operator in the stack} switch (C1) {Case' + ': STK. push (a + B); break; Case '-': STK. push (a-B); break; Case '*': STK. push (A * B); break; Case '/': STK. push (A/B); break; default: break;} catch (exception e) {flag = false; Return-1 ;}if (C = ')') {While (stk2.peek ()! = '#') {Float B = float. parsefloat (STK. pop (). tostring (); try {float a = float. parsefloat (STK. pop (). tostring (); // retrieves the two numbers of the operand stack char C1 = stk2.pop (); // retrieves the operator switch (C1) {Case '+': STK. push (a + B); break; Case '-': STK. push (a-B); break; Case '*': STK. push (A * B); break; Case '/': // If (B = 0) {// division to zero //} else {STK. push (A/B); //} break; default: break;} catch (exception e) {flag = false; Return-1 ;}// stk2.pop (); // Pop up ')' stk2. Pop (); // pop up '#'stk2. Pop (); // pop up' ('} else if (! Isbeg) {stk2.push (c); If (C = '(') {stk2.push ('#') ;}} else {flag = false; Return-1 ;}} // if it end with an operand, the last operand is added to the stack if (Str. length ()> 0) {try {float F = float. parsefloat (Str. tostring (); STK. push (f) ;}catch (exception e) {flag = false; Return-1 ;}str. delete (0, str. length ();} // The last character is "(" If (stk2.peek () = '#') {flag = false; Return-1 ;} while (stk2.peek ()! = '#') {Try {float B = float. parsefloat (STK. pop (). tostring (); float a = float. parsefloat (STK. pop (). tostring (); // retrieves the two numbers of the operand stack char C1 = stk2.pop (); // retrieves the operator switch (C1) {Case '+': STK. push (a + B); break; Case '-': STK. push (a-B); break; Case '*': STK. push (A * B); break; Case '/': // If (B = 0) {// division to zero //} else {STK. push (A/B); //} break; default: break; }} catch (exception e) {flag = false; Return-1 ;}try {return float. parsefloat (STK. pop (). tostring ();} catch (exception e) {flag = false; Return-1 ;}}

Determining operator priority

 
// Judge the priority public static int judge (char c) {int n = 0; Switch (c) {Case '(': n = 3; break; Case '+ ': case '-': n = 1; break; Case '*': Case '/': N = 2; break; Case ')': n = 0; break; default:} return N ;}

 

Related Article

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.