java-stack implementation in the evaluation of an ordinal expression

Source: Internet
Author: User

The middle order expression is very intuitive to us (this is what we usually touch), but the computer is cumbersome to handle (parentheses, precedence, and so on), there are no parentheses in the pre-order and post-sequence expressions, and only one-way scanning is required in the calculation, and operator precedence is not considered. such as 2*3/(2-1) +3* (4-1)

A pre-order expression is a prefix expression, an arithmetic expression without parentheses, and it is the expression that writes the operator in front, and the operand is written in the following expressions, for example: +/*23-21*3-4, also known as "Polish."

The post -order expression is the opposite of the pre-ordered expression scan, for example: 23*21-/341-*+.

Evaluates the middle-order expression with two stacks, only int is supported in the expression (but the computed result may be float)

infixexpr expr = new infixexpr ("2+3*4+5");

Assert.assertequals (Expr.evaluate (), 0.001f);

The following shows the specific operating procedures in the stack

Here is the specific implementation code:

 Public classinfixexpr {String expr=NULL;  Publicinfixexpr (String expr) { This. Expr =expr; }     Public floatevaluate () {Char[] ch =Expr.tochararray (); Mystack Stackoftocken=NewMystack (); Mystack Stackofnumber=NewMystack ();  for(inti = 0; i < ch.length; i++) {        if(Character.isdigit (Ch[i])) {intTMP = Integer.parseint ("" +Ch[i]);  while(I < ch.length-1 && Character.isdigit (ch[++i])) {tmp= tmp * + integer.parseint ("" +Ch[i]);        } System.out.println (TMP);        Stackofnumber.push (TMP); }        if(Ch[i] = = ' + ' | | ch[i] = = '-' | | ch[i] = = ' * ' | | ch[i] = = '/') {Stackoftocken.push (ch[i]); }        if(! (Stackoftocken.isempty ()) && (Char) Stackoftocken.peek () = = ' * ') {        intTMP = Integer.parseint ("" + ch[++i]);  while(I < ch.length-1 && Character.isdigit (ch[++i])) {tmp= tmp * + integer.parseint ("" +Ch[i]); }        if(I! = ch.length-1) {i--;        } stackofnumber.push (TMP); intTMP1 = Integer.parseint ("" +Stackofnumber.pop ()); intTMP2 = Integer.parseint ("" +Stackofnumber.pop ()); Stackofnumber.push (TMP1*TMP2);        Stackoftocken.pop (); }        if(! (Stackoftocken.isempty ()) && (Char) Stackoftocken.peek () = = '/') {        intTMP = Integer.parseint ("" + ch[++i]);  while(I < ch.length-1 && Character.isdigit (ch[++i])) {tmp= tmp * + integer.parseint ("" +Ch[i]); }        if(I! = ch.length-1) {i--;        } stackofnumber.push (TMP); intTMP1 = Integer.parseint ("" +Stackofnumber.pop ()); intTMP2 = Integer.parseint ("" +Stackofnumber.pop ()); Stackofnumber.push (TMP2/TMP1);        Stackoftocken.pop (); }    }    //reverse the numbers and operations in the stack for easy calculationreverse (Stackofnumber);    Reverse (Stackoftocken);  while(!(Stackoftocken.isempty ())) {        if((Char) Stackoftocken.peek () = = ' + ') {        intTMP1 = Integer.parseint ("" +Stackofnumber.pop ()); intTMP2 = Integer.parseint ("" +Stackofnumber.pop ()); Stackofnumber.push (TMP1+TMP2); }                if((Char) Stackoftocken.peek () = = '-') {        intTMP1 = Integer.parseint ("" +Stackofnumber.pop ()); intTMP2 = Integer.parseint ("" +Stackofnumber.pop ()); Stackofnumber.push (TMP1-TMP2);    } stackoftocken.pop (); }    returnFloat.parsefloat ("" +Stackofnumber.pop ()); }    Private voidreverse (mystack s) {if(S.isempty ()) {return; }    //if s has only one element, it returns. The concrete implementation is the first pop out of a, judging the rest is not empty stack. Object TMP1 =S.pop ();    Reverse (s); if(S.isempty ()) {s.push (TMP1); return; } Object Temp2=S.pop ();    Reverse (s);    S.push (TMP1);    Reverse (s);    S.push (TEMP2); }}

and test Cases:

 Public classinfixexprtest {@Before Public voidSetUp ()throwsException {} @After Public voidTearDown ()throwsException {} @Test Public voidtestevaluate () {//infixexpr expr = new infixexpr ("300*20+12*5-20/4");{infixexpr expr=Newinfixexpr ("2+3*4+5"); Assert.assertequals (19.0, Expr.evaluate (), 0.001f); } {infixexpr expr=Newinfixexpr ("3*20+12*5-40/2"); Assert.assertequals (100.0, Expr.evaluate (), 0.001f); } {infixexpr expr=Newinfixexpr ("3*20/2"); Assert.assertequals (Expr.evaluate (), 0.001f); } {infixexpr expr=Newinfixexpr ("20/2*3"); Assert.assertequals (Expr.evaluate (), 0.001f); } {infixexpr expr=Newinfixexpr ("10-30+50"); Assert.assertequals (Expr.evaluate (), 0.001f); }    }}

java-stack implementation in the evaluation of an ordinal expression

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.