Baidu software development written test big question 3

Source: Internet
Author: User

Evaluates an expression. The known operators are only subtraction, no negative numbers, and the result is not negative. (similar to the inverse Polish method of algorithmic thinking)

Thought: As long as the numbers into the stack can be, when needed to calculate. For symbols, there are several situations:

(1) The new symbol added before the symbol stack is not signed, then directly into the stack;

(2) The new symbol is the addition and subtraction operation, then the symbols in the previous symbol stack all out to calculate, consumes 1 symbols at a time, 2 numbers, and then the resulting results are pressed into the digital stack;

(3) The new symbol is multiplication, before the symbol is also multiplication, then only need to consume a symbol on the line;

(4) The new symbol multiplication, the original symbol plus minus, then the symbol pressure stack can.

Eg. 1+4*5+2*3, defines two stacks, each with numbers and symbols, called Numstack and Sigstack.

1) The number "1" into the stack numstack;

2) because there is no symbol in the stack, the symbol "+" into the stack sigstack;

3) Digital "4" into the stack numstack;

4) "*" belongs to the multiplication, and the top of the stack is added and reduced, so directly into the stack sigstack;

5) 5 directly into the stack numstack;

6) New symbol addition and subtraction operation, means that the previous operation can be completed, the order is: 5,4 respectively out of the stack, * out of the stack, 4*5=20 into the stack numstack, and then 20,1 respectively out of the stack, + out of the stack,

1+20=21 into the stack numstack, thus numstack leaves only 21,sigstack for empty, then "+" into the stack sigstack;

7) 2 into the stack numstack;

8) New symbol *, stack top symbol +, directly into the stack;

9) 3 into the stack.

At this point all the elements have been processed, the next can consume all the symbols, the order is: 2,3 out the Stack, * out of the stack, 3*2=6 into the stack, 6,21 out the stack, + out of the stack, 21+6=27 get results.

Here is the Java implementation code:

1  Packagepretest;2 3 ImportJava.util.Scanner;4 ImportJava.util.Stack;5 6  Public classMain3 {7      Public StaticStack Sigstack =NewStack ();//For Char8      Public StaticStack Numstack =NewStack ();//for int9     Ten      Public Static voidMain (string[] args) { OneScanner in =NewScanner (system.in); A          while(In.hasnext ()) { -String str =in.nextline (); -             intresult =cal (str); the System.out.println (result); -         } -     } -      Public Static intcal (String str) { + sigstack.clear (); - numstack.clear (); +StringBuilder SB =NewStringBuilder (); A         intLen =str.length (); at         intresult= 0; -         //complete the stacking of numbers and symbols -          for(inti=0; i<len;i++){ -             Charc =Str.charat (i); -             //Digital -             if(c>= ' 0 ' && C <= ' 9 '){ in Sb.append (c); -                 if(i==len-1){ to                     intx =Integer.parseint ((sb.tostring ())); + Numstack.push (x); -                 } the}Else{ *                 intx =Integer.parseint ((sb.tostring ())); $ Numstack.push (x);Panax NotoginsengSb.replace (0, Sb.length (), ""); -                 if(sigstack.size () = = 0){ the Sigstack.push (c); +}Else{ A                     Charexisted = (Char) Sigstack.peek (); the                     //if the symbol priority of the stack has been pressed >= the current symbol C, the operation of the stack symbol is completed +                     if(c== ' + ' | | c== '-'){ - Zhengli (); $ Sigstack.push (c); $                         //When two of them are multiplication operations, -}Else if(existed = = ' * ' | | existed = = '/')){ - Tinyadjust (c); the}Else{//new symbol multiplication, old symbol plus minus - Sigstack.push (c);Wuyi                     }     the                 } -             } Wu         } -         //Calculation Results About Zhengli (); $result = (int) Numstack.pop (); -         returnresult;  -     } -      A     //Simple Subtraction Calculation +      Public Static intComputeintAintBCharc) { the         intresult = 0; -         Switch(c) { $          Case' + ': result = a +b; the              Break; the          Case'-': result = A-b; the              Break; the          Case' * ': result = A *b; -              Break; in          Case'/': result = A/b; the              Break; the         } About         returnresult; the     } the      the     //fully digest the already stacked symbol for the new symbol is the plus and minus +      Public Static voidZhengli () { -          while(Sigstack.size () > 0){ the             intA = (int) Numstack.pop ();Bayi             intB = (int) Numstack.pop (); the             Charc = (Char) Sigstack.pop (); the             intx =Compute (B,A,C); - Numstack.push (x); -         } the     } the      the     //Digest a symbol for two consecutive subtraction symbols appearing the      Public Static voidTinyadjust (Charc) { -         intX1 = (int) Numstack.pop (); the         intx2 = (int) Numstack.pop (); the         intx3 = Compute (x2, x1, (Char) Sigstack.pop ()); the Numstack.push (x3);94 Sigstack.push (c); the     } the  the}

Baidu software development written test big question 3

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.