Java Stack implementation Simple calculator algorithm

Source: Internet
Author: User

Problem Description:

For any string, include +-*/and parentheses to find the value of the expression

First Baidu the problem, there are many answers online, but the actual thinking, found that many of the answers are not considered complete, for example:

-1+ (-2) How do I encounter the minus sign?

Now post the code as follows:

1  PackageCalcultor;2 3 ImportJava.util.Stack;4 5  Public classCalcultor {6 7     Static BooleanIsnumber (Charx) {8         if(x >= ' 0 ' && x <= ' 9 ') {9             return true;Ten         } One         return false; A     } -  -     Static intPriorityCharx) { the         if(x = = ' + ' | | x = = '-')) { -             return0; -}Else if(x = = ' * ' | | x = = '/')) { -             return1; +}Else if(x = = ' (' | | x = = ') ') { -             return-1; +}Else if(x = = ' # ') { A             return-2; at         } -  -         return-3; -     } -  -      Public Static intCalculte (String s) { instack<integer> number =NewStack<integer>(); -Stack<character> operate =NewStack<character>(); to         Chartop; +         intA = 0, b = 0; -         intj = 0; the         BooleanFlag =false; *          for(inti = 0; I < s.length (); ++i) { $             if(S.charat (1) = = '-') {Panax NotoginsengFlag =true; -             } the             if(I >= 2 && i <= s.length ()-2) { +j =i; A                 if(!isnumber (S.charat (j-1)) && S.charat (i) = = '-' && Isnumber (S.charat (j + 1))) { theFlag =true; +                 } -             } $             if(Isnumber (S.charat (i))) { $                 intTemp = 0; -String temp = ""; -Temp + =S.charat (i); theSYSTEM.OUT.PRINTLN ("line temp is" +temp); -                  while(Isnumber (S.charat (+ +i)))WuyiTemp + =S.charat (i); theSYSTEM.OUT.PRINTLN ("line temp" +temp); -                  for(intJJ = 0; JJ < Temp.length (); ++JJ) { WuTemp = temp * + Temp.charat (JJ)-48; -                 } AboutSystem.out.println ("line Temp" +Temp); $                 if(flag) { -Temp *=-1; -Flag =!Flag; -                 } A Number.push (Temp); +temp =NULL; the             } -             if(!Isnumber (S.charat (i))) { $                 if(((S.charat (i) = = '-') &&!flag) | | (S.charat (i)! = '-')) { the                     if(Operate.empty ()) { the Operate.push (S.charat (i)); the}Else { thetop =Operate.peek (); -                         if(S.charat (i) > Priority (top) | | S.charat (i) = = ' (') { in Operate.push (S.charat (i)); the}Else { the                              while(Priority (S.charat (i)) <=Priority (top)) { About                                 if(top = = ' # ' && s.charat (i) = = ' # ') { the                                     intanswer; the Operate.pop (); theAnswer =Number.peek (); + Number.pop (); -System.out.println ("line answer is" +answer); the                                     returnanswer;Bayi}Else if(top = = ' (' && s.charat (i) = = ') ') { the++i; the}Else { -A =Number.peek (); - Number.pop (); theb =Number.peek (); the Number.pop (); theSystem.out.println ("line-A is" + A + ", B-is" +b); the                                 } -                                 if(top = = ' + ')) { theB + =A; the Number.push (b); the}Else if(top = = '-')) {94B-=A; the Number.push (b); the}Else if(top = = ' * ') { theb *=A;98 Number.push (b); About}Else if(top = = '/')) { -b/=A;101 Number.push (b);102                                 }103 Operate.pop ();104top =Operate.peek (); the                             }106System.out.println ("line s[i) is" +S.charat (i));107 Operate.push (S.charat (i));108                         }109                     } the                 }111             } the         }113  the         return0; the     } the 117      Public Static voidMain (string[] args) {118String s =NewString ("# (1+2) + ( -2*2) + ( -2*6) #");119         intAnswer =Calculte (s); -System.out.println ("The answer is" +answer);121     }122}

Java Stack implementation Simple calculator algorithm

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.