20172325 "Java Programming" course pair programming exercises _ Arithmetic second-week phase summary pair Partners
- Study No.: 20172306
- Name: Chen Liu
- Knot-to-partner blog links
- Chen Liu students on the high degree of programming, and in the programming ability is very good, have their own ideas and can put the idea of concrete practice out, just in the process of programming and classmate communication is not enough, always in the end to find the problem, it is easy to cause naught, hope in the programming process more communication, Reduce unnecessary waste of time and energy.
Part of the group's own responsibility in the project for the programming photo
I am responsible for two parts, namely:
- 1, using the stack to the infix expression to the suffix expression;
- 2, the suffix expression is calculated.
Division of Personal contribution
I think our contribution is divided into "50%: 50%"; we have two members, then the task allocation is also easier, must do part of four, we are two parts, because each part of the completion of the need to devote time and energy, so I think there is no difficulty, 55 open.
Generation of related process-graded topics
-Ability to achieve true scores
-Calculation of suffix expressions
Key code Explanation
- Calculation of postfix expressions
Package knot pair Project; /* * Suffix expression calculation Deng, Chen Liu * * Deng Responsible part */import java.util.Stack; Import Java.util.StringTokenizer; Import Java.util.Scanner; public class HZ {/* * Because our test file is not yet made up, the parts are not yet connected, so I will first declare the four symbols here */private final char ADD = ' + '; Private final char SUBTRACT = '-'; Private final char MULTIPLY = ' * '; Private final char DIVIDE = '/'; Private stack<integer> Stack; /* Initialize an empty stack to store the expression */public HZ () {stack = new stack<integer> (); }/* is used here to determine whether the specified char is an operator. * If it is an operator, the Boolean value is True */private Boolean isoperator (String str) {return (Str.equals ("+") | | | |-ST R.equals ("*") | | Str. Equals ("/")); }/* * Below is the specific calculation process */private int calculatesingleop (char operator, int num1, int num2) {int result = 0; Switch (operator) {case Add:result = num1 + num2; Break; Case subtract:result = num1-num2; Break Case Multiply:result = num1 * NUM2; Break Case divide:result = num1/num2; Break } return result; }/* * The following is the calculation of the specified suffix expression. * (1). If you encounter an operand, push it onto the stack. * (2). If an operator is encountered, two operands are popped for the operator to calculate. * (3). Push the calculation results onto the stack. * string represents a suffix expression. */public int evaluate (String str) {int NUM1, num2, result = 0; Specifies that a specific character delimiter is a space. String token = ""; StringTokenizer tokenizer = new StringTokenizer (str); while (Tokenizer.hasmoretokens ()) {//Each character comes from a string. token = Tokenizer.nexttoken (); if (Isoperator (token)) {num2 = (Stack.pop ()). Intvalue (); NUM1 = (Stack.pop ()). Intvalue (); result = Calculatesingleop (Token.charat (0), NUM1, num2); Pushes the result of the calculation onto the stack stack.push (result); } else {Stack.push (new Integer (Integer.parseint (token))); }} return result; public static void Main (string[] args) {String expression = ""; String again = ""; int result = 0; try {@SuppressWarnings ("resource") Scanner input = new Scanner (system.in); do {Hz evaluator = new Hz (); Reads a valid suffix expression. That is, the user enters a valid suffix expression, and then constructs the parameter in the test file. System.out. println ("Please enter a valid Postfix expression:"); expression = Input.nextline (); result = evaluator.evaluate (expression); System.out.println (); System.out. println ("After evaluating, the calculated result is:" + result); Repeat Operation System.out.println ("Does want to test again? [y/n] "); again = Input.nextline (); System.out.println (); } while (Again.equalsignorecase ("Y")); } catch (Exception IOException) {System.out.println ("Input Exception reported."); } } }
20172325 "Java Programming" course pair programming Exercises _ Arithmetic third-week phase summary