Fourth time assignment

Source: Internet
Author: User

    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

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.