ImportJava.util.Stack; Public classOperate {PrivateStack<character> Pristack =NewStack<character>(); PrivateStack<integer> Numstack =NewStack<integer>();; Public intcaculate (String str) {string temp; StringBuffer Tempnum=NewStringBuffer (); StringBuffer string=NewStringBuffer (). append (str); while(String.Length ()! = 0) {Temp= string.substring (0, 1); String.delete (0, 1); if(!Isnum (temp)) { if(!"". Equals (Tempnum.tostring ())) { intnum =Integer.parseint (tempnum.tostring ()); Numstack.push (num); Tempnum.delete (0, Tempnum.length ()); } while(Ompare (Temp.charat (0)) && (!Pristack.empty ())) { intA = (int) Numstack.pop (); intB = (int) Numstack.pop (); CharOpe =Pristack.pop (); intresult = 0; Switch(ope) { Case+: Result= B +A; Numstack.push (result); Break; Case‘-‘: Result= B-A; Numstack.push (result); Break; Case‘*‘: Result= b *A; Numstack.push (result); Break; Case‘/‘: Result= b/A; Numstack.push (result); Break; } } if(Temp.charat (0)! = ' # ') {Pristack.push (NewCharacter (Temp.charat (0))); if(Temp.charat (0) = = ') ') {pristack.pop (); Pristack.pop (); } } } ElseTempnum=tempnum.append (temp); } returnNumstack.pop (); } Private Booleanisnum (String temp) {returnTemp.matches ("[0-9]"); } Private BooleanCompareCharstr) { if(Pristack.empty ()) {return true; } CharLast = (Char) pristack.lastelement (); if(Last = = ' (') { return true; } Switch(str) { Case‘#‘: return false; Case‘(‘: return true; Case‘)‘: return false; Case‘*‘: { if(last = = ' + ' | | last = = '-')) return true; Else return false; } Case‘/‘: { if(last = = ' + ' | | last = = '-')) return true; Else return false; } Case+: return false; Case‘-‘: return false; } return true; } Public Static voidMain (String args[]) {Operate Operate=NewOperate (); intt = Operate.caculate ("(3+4* (4*10-10/2) #"); System.out.println (t); } }
Requirement Analysis: Input 0-10 random number, calculate subtraction's arithmetic.
Summary: With the completion of the third job, the fourth time the assignment is only to add a little content, but I have not a bit of thinking, but also looked at the partner to do, only slowly have a train of thought, write not good please teacher understanding.
Fourth time assignment