Basic Calculator II
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers,,, +
-
*
, /
operators and empty spaces
. The integer division should truncate toward zero.
Assume that the given expression was always valid.
Some Examples:
"3+2*2" = 7 "3/2" = 1 "3+5/2" = 5
Note: don't use the eval
built-in library function.
This topic needs to implement + 、-、 *, \ Four operations, in fact the idea is consistent, whether you can first more operators, such as LOG,COS,SIN,SQRT and so on.
Here's a question to note:
When encountering A + or-, it is obvious that any operator at the top of the stack can be calculated, because at least the priority is the same, and can be sure that all operations in the account can be taken out of the run, then this time we will all take out all the processing it? or just one?
Case+: Case‘-‘: if(!Op.isempty ()) { CharOpthis =Op.pop (); intData1 =Data.pop (); intData2 =Data.pop (); intres = 0; if(opthis== ' + ') res = data1+data2; Else if(Opthis = = '-') res = data2-data1; Else if(Opthis = = ' * ') res = data1 *data2; Elseres = data2/data1; Data.push (RES); } op.push (OPC);
If only one problem is dealt with, the key is that the calculation of the minus sign needs to be ordered, such as
2-3+4 = 3 If this is a finite calculation that discards the minus sign, it becomes
(3+4) =-5, obviously wrong.
There's nothing else to say in the rest of the place.
1 ImportJava.util.*;2 3 Public classSolution {4 PrivateVector<string>getseg (String s) {5 intindex = 0;6vector<string> res =NewVector<string>();7 while(index<s.length ()) {8 Charc =S.charat (index);9 if(Character.isdigit (c)) {Ten intEndindex = index+1; One while(Endindex<s.length () &&character.isdigit (S.charat (endindex))) endindex++; AString seg =s.substring (index,endindex); - Res.add (SEG); -index =Endindex; the}Else{ - if(c== "){} - Else{ -String seg = s.substring (index,index+1); + Res.add (SEG); - } +index++; A } at } - returnRes; - } - Public intCalculate (String s) { -vector<string> segments =getseg (s); -stack<integer> data =NewStack<integer>(); inStack<character> op =NewStack<character>(); - for(intI=0;i<segments.size (); i++){ toString Segone =Segments.get (i); + CharOPC = Segone.charat (0); - if(Character.isdigit (OPC)) { the Data.push (Integer.parseint (Segone)); *}Else{ $ Switch(OPC) {Panax Notoginseng Case+: - Case‘-‘://If you change the following code to an if statement is wrong, you should, in some cases, all of the high-priority operators in the stack are all finished the //Otherwise, high-priority operations may be shot by low-priority operations + //For example 1-2*3 +5 A //If you encounter A +, the previous minus sign does not calculate together, it will result in the final result is-10, obvious error the while(!Op.isempty ()) { + CharOpthis =Op.pop (); - intData1 =Data.pop (); $ intData2 =Data.pop (); $ intres = 0; - if(opthis== ' + ') res = data1+data2; - Else if(Opthis = = '-') res = data2-data1; the Else if(Opthis = = ' * ') res = data1 *data2; - Elseres = data2/data1;Wuyi Data.push (res); the } - Op.push (OPC); Wu Break; - Case‘*‘: About Case‘/‘: $ if(!op.isempty () && (op.peek () = = ' * ' | | Op.peek () = = '/')){ - CharOpthis =Op.pop (); - intData1 =Data.pop (); - intData2 =Data.pop (); A intres = 0; + if(Opthis = = ' * ') res = data1 *data2; the Elseres = data2/data1; - Data.push (res); $ } the Op.push (OPC); the Break; the } the } - } in while(!Op.isempty ()) { the CharOpthis =Op.pop (); the intData1 =Data.pop (); About intData2 =Data.pop (); the intres = 0; the if(opthis== ' + ') res = data1+data2; the Else if(Opthis = = '-') res = data2-data1; + Else if(Opthis = = ' * ') res = data1 *data2; - Elseres = data2/data1; the Data.push (res);Bayi } the returnData.pop (); the } -}
[Leetcode] Basic Calculator II