The simple arithmetic of "programming problems"

Source: Internet
Author: User
Tags integer division

Problem Description: Enter a simple arithmetic expression string that contains only single digits to evaluate the value of the expression
Note: 1, expression only contains +,-, *,/, (,), arithmetic characters
2, the expression value contains only single digits (0-9), and does not appear as a divisor of 0 cases
3, to consider subtraction according to the usual arithmetic the calculation priority
4, Division uses integer division, that is, only the integer portion of the result of the division operation is preserved. Like 8/3=2. The input expression guarantees that no 0 occurs as a divisor condition
5, the input string must be in accordance with the test instructions legal expression, which includes only numeric characters and arithmetic character, in addition to any other characters, there will be no calculation overflow situation
• Required implementation functions:
int calculate (int len,char *expstr)
"Input" int len: string length;
Char *expstr: an expression string;
"Output" none
"Return" Calculation results

• example
1) Input: char *expstr = "1+4*5-8/3"
function return: 19
2) Input: char *expstr = "8/3*3"
function return: 6

1  PackageMyTest;2 3 /**4 * Simple arithmetic, the number of each participating operation is between 0-9. 5  */6 7 ImportJava.util.*;8 9  Public classFourops {Ten  One      Public Static voidMain (string[] args) { AScanner in =NewScanner (system.in); -          while(In.hasnext ()) { -String expretion =In.next (); the             intLength =expretion.length (); -             intresult =compute (length, expretion); - System.out.println (result); -         } +  -     } +      A     /** at * This function has two functions - * 1. First, convert a normal infix expression into a suffix expression - * 2. Calculate the value of an expression by a suffix expression -      * @paramlength -      * @paramexpretion -      * @return in      */ -     Private Static intComputeintlength, String expretion) { to         intresult = 0; +List<character> Oneops =NewLinkedlist<>(); -Oneops.add (' + ')); theOneops.add ('-'); *linkedlist<character> Ops =NewLinkedlist<> ();//as a stack, the front must also be LinkedList $StringBuffer Changedexp =NewStringBuffer ();Panax Notoginseng          for(inti = 0; i < length; i++){ -Character temp =Expretion.charat (i); the             if(temp >= ' 0 ' && temp <= ' 9 ') + changedexp.append (temp); A             Else{ the                 if(Ops.isempty ()) + Ops.push (temp); -                 Else{ $                     if(temp = = ' (') $ Ops.push (temp); -                     Else{ -                         if(Oneops.contains (temp)) { the //if (Ops.peek ()! = ' (') -                             //The original if, consider that when you encounter + + should be the stack of arithmetic are pop out, and now use whileWuyi                              while(!Ops.isempty ()) { the                                 if(Ops.peek () = = ' (') -                                      Break; Wu Changedexp.append (Ops.pop ()); -                             }                                     About Ops.push (temp); $                         }                         -                         Else if(temp! = ') '){ - Ops.push (temp); -                         } A                         Else{//') ' Situation +                              while(Ops.peek ()! = ' ('){ the Changedexp.append (Ops.pop ()); -                             } $ Ops.pop (); the                         } the                     } the                 } the             } -         } in          while(!Ops.isempty ()) { the Changedexp.append (Ops.pop ()); the         } AboutString Changedexpstr =changedexp.tostring (); theSystem.out.println (CHANGEDEXPSTR);//output-converted suffix expression the          the         //calculate with a suffix expression +Linkedlist<integer> nums =NewLinkedlist<>(); -          for(inti = 0; I < changedexpstr.length (); i++){ theCharacter temp =Changedexpstr.charat (i);Bayi             if(temp >= ' 0 ' && temp <= ' 9 '){ theNums.push (temp-' 0 '); the             } -             Else{ -                 intA =Nums.pop (); the                 intb =Nums.pop (); the                 Switch(temp) { the                  Case' + ': Nums.push (A+B); Break; the                  Case'-': Nums.push (B-A); Break; -                  Case' * ': Nums.push (A*B); Break; the                  Case'/': Nums.push (b/a); Break; the                 } the             }94         } theresult =Nums.pop (); the         returnresult; the     }98  About}

The simple arithmetic of "programming problems"

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.