Calculation of infix simple arithmetic expression for Java stack implementation

Source: Internet
Author: User

 Public Abstract classStack<t> {     Public Abstract BooleanIsEmpty ();  Public Abstract BooleanIsfull ();  Public AbstractT Top ();  Public Abstract Booleanpush (T x);  Public AbstractT pop ();  Public Abstract voidclear ();}
 Public classSeqstack<t>extendsStack<t> {    Privateobject[] Elementdata; Private intMaxtop; Private inttop;  PublicSeqstack (intsize) {         This. Maxtop = size-1; Elementdata=NewObject[size]; Top=-1; } @Override Public BooleanIsEmpty () {returntop = =-1; } @Override Public BooleanIsfull () {returntop = = MaxTop-1; } @SuppressWarnings ("Unchecked") @Override PublicT Top () {if(top = =-1) {System.out.println ("Empty"); return NULL; }        return(T) elementdata[top]; } @Override Public Booleanpush (T x) {if(top = =maxtop) {System.err.println ("Full"); return false; } elementdata[++top] =x; return true; } @SuppressWarnings ("Unchecked") @Override PublicT Pop () {if(top = =-1) {System.err.println ("Empty"); return NULL; } T result=(T) elementdata[top]; Elementdata[top]=NULL;//GCtop--; returnresult; } @Override Public voidClear () {//Let GC do it work         for(inti = 0; i < top+1; i++) {Elementdata[i]=NULL; } Top=-1; }}
 Public classStackcalc {PrivateSeqstack<integer>Stack;  PublicStackcalc (intmaxSize) {Stack=NewSeqstack<integer>(maxSize); }    Private voidPushoperand (Integer number) {Stack.push (number); }    PrivateNumber Dooperate (CharOper) {Integer Right=Stack.pop (); Integer Left=Stack.pop (); Integer result=NULL; if(Left! =NULL&& Right! =NULL) {            Switch(oper) { Case+: Result= Left.intvalue () +Right.intvalue ();  Break;  Case‘-‘: Result= Left.intvalue ()-Right.intvalue ();  Break;  Case‘*‘: Result= Left.intvalue () *Right.intvalue ();  Break;  Case‘/‘:                if(Right.intvalue () = = 0) {System.err.println ("Divide by 0"); } result= Left.intvalue ()/Right.intvalue ();  Break; default:                 Break;        }} stack.push (Result); returnresult; }    Private intIcpCharc) {Switch(c) { Case‘#‘:            return0;  Case‘(‘:            return7;  Case‘*‘:            return4;  Case‘/‘:            return4;  Case+:            return2;  Case‘-‘:            return2;  Case‘)‘:            return1; default:            return-1; }    }    Private intIspintc) {Switch(c) { Case‘#‘:            return0;  Case‘(‘:            return1;  Case‘*‘:            return5;  Case‘/‘:            return5;  Case+:            return3;  Case‘-‘:            return3;  Case‘)‘:            return7; default:            return-1; }    }     Publicstring Transfer (string expression) {StringBuilder sb=NewStringBuilder (); Seqstack<Character> stack =NewSeqstack<character>(Expression.length ()); Stack.push (‘#‘);  for(inti = 0; I < expression.length (); i++) {Character C=Expression.charat (i); if(' 0 ' <= C && c <= ' 9 ' | |                    ' A ' <= C && c <= ' Z ' | | ' A ' <= c && c <= ' Z ') {//Digit characterSb.append (c); } Else{//operator                if(ICP (c) > ISP (Stack.top ())) {//into the stackStack.push (c); } Else{//out of the stack                    if(c = = ') ') {                        CharCH =Stack.pop ();  while(ch! = ' (') {sb.append (CH); CH=Stack.pop (); }                    } Else {                        CharCH =Stack.pop ();  while(ICP (c) <=ISP (CH))                            {sb.append (CH); CH=Stack.pop ();                        } stack.push (CH);                    Stack.push (c); }                }            }        } //End of for        CharCH =Stack.pop ();  while(ch! = ' # ') {sb.append (CH); CH=Stack.pop ();                } stack.clear (); returnsb.tostring (); }     PublicInteger Calc (String expression) {expression=transfer (expression);  for(inti = 0; I < expression.length (); i++) {            Charc =Expression.charat (i); Switch(c) { Case+:             Case‘-‘:             Case‘*‘:             Case‘/‘: Dooperate (c);  Break; default: Pushoperand (NewInteger (c + ""));  Break; }        }        returnStack.pop (); }    /**     * @paramargs*/     Public Static voidMain (string[] args) {Stackcalc calc=NewStackcalc (10); System.out.println (Calc.calc ("6/(4-2) +3*2")); }}

Calculation of infix simple arithmetic expression for Java stack implementation

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.