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