2017-2018-2 1723 "Java Programming" course pair programming practice _Four arithmetic operations second week

1. Pairing objects

2. Contents of this week

(1) Continue to write the unfinished code last week

1. Continue to write code this week, so that the code supports fractional class calculation
2. Screenshot of the relevant process
a. The picture below is the class that generated the question written last week and cannot be run.

The following picture is the code updated this week, which can support the generation of scores. The red box is the main change area.

b. The following figure is a screenshot of part of the code and the running result of the title-to-suffix expression in the code

c. The following figure is a screenshot of part of the code and the running result of the calculation result of the suffix expression in the code

3. Key code explanation

Generating True Scores in Questions and Converting Questions Containing True Scores to Postfix Expressions

  • Generating true scores: Here we let the generated random numbers contain complex numbers. For example, we generate random numbers between -4 and 9. If the random numbers generated are negative, assign a randomly generated true score to a String figure. , if the generated random number is 0~9, convert the random number to String type and assign it to the figure, thus completing the generation of the true score in the question.
  • Converting suffix expressions for questions with true scores: We call the method in the StringTokenizer class to convert the above question into an object of the StringTokenizer class, and then use the nextToken() method to divide it into String strings, and then judge the strings If the length of the string is greater than 1, the string will be output directly, and then the following is the string of 1, including integers and operators. This has been solved last week, including pushing and popping.
  • Postfix expression evaluation: in writing: convert all numbers into fractions, then call the addition, subtraction, multiplication and division methods of fractions in the previously written class, and then complete the evaluation, in writing.

(2) Difficulties encountered and solutions

1. About the solution of converting a question into a
suffix expression

, low to output the top element of the stack and push the scanned operator onto the stack.
Solution:
a. The idea given by the teacher is to write an array, which stores the priority comparison of operators, and then use it for comparison when calling.
b. For the ideas given by the teacher, there are some places that I do not understand very well. The first is that the two-dimensional array that stores the operator priority comparison is not very good at editing, and the second is that the code implementation of the specific call is not very clear. So I changed my thinking and thought about the essence of the principle: the four operations required by the title only require four operators of addition, subtraction, multiplication and division (because our group does not need to implement the generation of parentheses), and the comparison of only four operators, through The if statement is judged several times and it is OK. First, judge whether the obtained operator is multiplication and division level. If it is, then judge whether the operator at the top of the stack is addition or subtraction. If it is, the outside priority is high, otherwise the outside priority is low; if the obtained operator is Add or subtract one level, the priority must be lower than the priority of the top element of the stack. So as to achieve the purpose of judging the priority.

(3) All codes

Part 1: Generating a topic // Person in charge: Hou Zeyang

import java.util.Stack;
import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;

class Questions {
    ArrayList<Object> array = new ArrayList<Object>();
    Random generator = new Random();
    char[] newchar = {'+', '-', '*', '/'};
    protected int number;
    int NUM;

    public Questions() {
        number = 0;
    }

    public Object getQuestion(int num) {
        int num1 = num;

        while (num > 0) {
            int figure = (int) generator.nextInt(9) + 1;
            array.add(figure);
            number = (int) (Math.random() * 4);
            array.add(newchar[number]);
            num--;
        }
        String obj = "";
        while (num < 2 * num1) {
            obj += array.get(num);
            num++;
        }
        int other = (int) generator.nextInt(9) + 1;
        array.add(other);
        obj += other + "=";

        return obj;
    }
}

Part 2: Item Operation // Persons in charge: Zhou Yajie, Hou Zeyang

//生成后缀表达式

public class Calculations {
    public static void main(String[] args) {
        Questions questions=new Questions();
        Stack stack = new Stack();

        Scanner Scan=new Scanner(System.in);

        char c;
        int count=0,answer;
        char[] operation = new char[100];
        String str = (String) questions.getQuestion(3);

        System.out.println("请回答以下问题:\n"+str);
        System.out.println("请输入你的答案:");
        answer=Scan.nextInt();

        for (int i = 0; i < str.length(); i++) {
            c = str.charAt(i);
            if (c >= '0' && c <= '9') {

                operation[i] = c;
                count++;

               }
            else {

                if (c == '*' || c == '/') {
                    if (stack.empty()) {
                        stack.push((char) c);
                    } else if ((char) stack.peek() == '*' || (char) stack.peek() == '/') {
                        operation[i] = (char) stack.pop();
                        stack.push(c);
                    } else
                        stack.push(c);
                } else if (c == '+' || c == '-') {
                    if (stack.empty()) {
                        stack.push(c);
                    } else if ((char) stack.peek() == '+' || (char) stack.peek() == '-') {
                        operation[i] = (char) stack.pop();
                        stack.push(c);
                    } else {
                        operation[i] = (char) stack.pop();
                        stack.push(c);
                    }

                } else
                    stack.push(c);

            }
        }
        int num = stack.size();
        for (int a = 0; a < num; a++) {
            operation[str.length() + a] = (char) stack.pop();
        }
//后缀表达式计算    //负责人:周亚杰

Stack<Integer> stack1 = new Stack<Integer>();

    int m, n, sum,num1=str.length()+(str.length()-count);

        for (int b = 0; b <= num1; b++) {
        if (operation[b] >= '0' && operation[b] <= '9')
            stack1.push((int) operation[b]-48);
        else {
            if (operation[b] == '+') {
                m =  stack1.pop();
                n =  stack1.pop();
                sum = n + m;
                stack1.push(sum);
            } else if (operation[b] == '-') {
                m = stack1.pop();
                n = stack1.pop();
                sum = n- m;
                stack1.push(sum);
            } else if (operation[b] == '*') {
                m = stack1.pop();
                n = stack1.pop();
                sum = n * m;
                stack1.push(sum);
            } else if (operation[b] == '/') {
                m =  stack1.pop();
                n =  stack1.pop();
                sum = n / m;
                stack1.push(sum);
            }
            else if (operation[b] == ' ')
                continue;
        }
    }
        if ((int)stack1.peek()==answer)
            System.out.println("恭喜你答对了!");
        else
            System.out.println("很遗憾,答错了!答案是:"+stack1.peek());
}
}

3. Division of personal contribution

Zhou Yajie: 50%
Hou Zeyang: 50%

Guess you like

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