20172325 "Java Programming" course pair programming practice _ four arithmetic operations second week phase summary

20172325 "Java Programming" course pair programming practice _ four arithmetic operations second week phase summary

pairing partner

  • Student ID: 20172306
  • Name: Liu Chen
  • Pairing Partner Blog Link
  • Liu Chen is very active in programming, and has a good programming ability. He has his own ideas and can put them into practice, but he does not communicate enough with his classmates in the process of programming, and he always fails in the end. It is easy to cause the failure of previous efforts when the problem is discovered. I hope to communicate more during the programming process to reduce unnecessary waste of time and energy.

group pair programming photo

own part of the project

I am responsible for two parts, namely:

  • 1. Use stack to convert infix expression to postfix expression;
  • 2. Calculate the postfix expression.

personal contribution division

I think our contribution division is "50%: 50%"; we have two members, and task assignment is also easier, there are four must-do parts, and we have two parts per person, because the completion of each part requires Put in the time and effort, so I don't think it's easy or difficult.

Screenshots of the relevant process

- Generation of graded questions

- Able to achieve true scores

- Evaluation of postfix expressions

Key code explanation

  • Evaluation of Postfix Expressions

````package pair project;
/*

  • The calculation of suffix expressions Author: Deng Yukun, Liu Chen
  • Deng Yukun is responsible for the part
    */

import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Scanner;

public class HZ {

/
Because our Test file has not been compiled yet, and the various parts have not been linked, so I first made a declaration of the four symbols here
*/

private final char ADD = '+';
private final char SUBTRACT = '-';
private final char MULTIPLY = '*';
private final char DIVIDE = '/';

private Stack<Integer> stack;

/*
 * 初始化一个空栈,用来存储表达式
 */
public HZ (){
    stack = new Stack<Integer>();
}

/*
 * 此处用来判断指定的char是否为操作符。
 *  如果是运算符,布尔值为真
 */
private boolean isOperator(String str) {

    return (str.equals("+") || str.equals("-") || str.equals("*") || str
            .equals("/"));
}

/*
 * 下面是具体的计算过程
 */
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;
}

/*
 *下面是计算指定的后缀表达式。
 *(1).如果遇到操作数,将其推送到栈上。
 *(2).如果遇到操作符,则弹出两个操作数以供运算符计算。
 *(3).将计算结果推到栈上。
 *字符串表示后缀表达式。
 */
public int evaluate (String str) {

    int num1, num2, result = 0;
    // 指定特定的字符分隔符为空格。
    String token = "";
    StringTokenizer tokenizer = new StringTokenizer(str);

    while (tokenizer.hasMoreTokens()) {
        // 每一个字符都来自于字符串。
        token = tokenizer.nextToken();

        if (isOperator(token)) {
            num2 = (stack.pop()).intValue();
            num1 = (stack.pop()).intValue();
            result = calculateSingleOp(token.charAt(0), num1, num2);
            // 将计算结果推到栈上
            stack.push(new Integer(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();
            // 读取一个有效的后缀表达式。即用户输入有效后缀表达式,之后在Test文件里将参数构建过来就好了。
            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);

            // 重复操作
            System.out.println("Do you 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.");
    }
}

```}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325862735&siteId=291194637