"WEEK2" pair programming-arithmetic and impressions

Source: Internet
Author: User

First of all I have to say, I have to do my homework, but limited capacity, but also need to practice.

Arithmetic, improving the code flow:

1. Manual input calculation (infix expression)

2. Converting infix expressions to suffix expressions to generate an out array

3. An operand stack, a stack of operators, by the suffix expression calculation method to put the characters out in two stacks

4. Ends the calculation when the operand stack has only one count.

The code fragment parsing is as follows:

infix variable suffix:

Public list<string> Solveorder (string[] in,hashmap<string,integer>precedence) {//compliant with inverse polish (suffix) output list< String> out = new arraylist<string> ();//Operator stack<string> ops= new stack<string> (); for (int i=0;i <in.length;i++) {String s=in[i];//Enter the value into the out array at the end of the IF (!precedence.containskey (In[i])) {Out.add (s); continue;} while (true) {//when the stack of the storage operator is "empty" or "meets opening parenthesis" or "top operator precedence                                //less than the priority of reading the current character of the string", the symbol is in the stack if (Ops.isempty () | | | S.equals ("(") | | (Precedence.get (s)                                 >precedence.get (Ops.peek ()))) {Ops.push (s); break;} String Op=ops.pop ();//out-Stack operator if (op.equals (")") {break;} Else{out.add (OP);}}} If the operation Fu Yi is not empty, then the remaining operators are placed in the out array while (!ops.isempty ()) {Out.add (Ops.pop ());} return out;}                     

Calculated according to the suffix (inverse polish type).

Public double calculateout (string[] out) {//Assuming the output of the inverse polish is not empty but the length is not zero assert (out! = NULL && out.length! = 0);//operand stack sta ck<double> stack = new stack<double> (), for (int i = 0; i < out.length; i++) {if (Isnumber (Out[i])) {STACK.P Ush (Double.parsedouble (Out[i));} else {Double V1 = stack.pop ();d ouble v2 = Stack.pop ();d ouble result = eval (Out[i], v2, v1); Stack.push (result);}} return Stack.pop ();}

Main function: Console input complete calculation.

    public static void Main (string[] args) {System.out.println ("Please enter the calculation to be computed:"); Scanner sc= New Scanner (system.in); String inbefor = Sc.nextline (); String[] In=inbefor.split (""); Cal cc = new Cal (); Hashmap<string, integer> precedence=cc.priorityinfo (); Cc. Solveorder (in, precedence); SYSTEM.OUT.PRINTLN ("The result of the calculation entered is:" +cc.calculateout (in));}

Teacher, this code I look at other people's blog written, can only say I understand, not to your request to become their own, but, teacher, I really can't write.

About pairing programming, from the beginning of this course is from scratch understanding. Programming this thing is like thinking, two people share is two people have two kinds. We will make progress in the quarrel.

1. Is the initial debate about using Java or C + +? Since we might want to use Java more in the future, we finally decided to use Java.

2. There are two kinds of ideas about complex operations: four operators for five numbers are discussed, four positions are discussed separately +-*/the other is to use the stack to calculate, and finally the stack code is smaller, selected!

3. about how numbers and symbols are stored, one is a class, and the other is divided into two arrays. With classes, it is inconvenient to work with an int string type of array and character, and the final decision is to use two arrays.

4. The final code does not add the original two operands of the code, and finally did not add.

5. The code is still a bit of a problem, do not send a blog? Finally, ...

(about the photos, the work was done in the early days of school, and now we both go home.) )

Conclusion: I went to bed, now it's 5:27.

Code Address: Https://git.coding.net/yumiaomiao/Arithmetic.git

SSH:[email protected]: Yumiaomiao/arithmetic.git

"WEEK2" pair programming-arithmetic and impressions

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.