20172302 "Java Programming" Course Pair Programming Exercise_Four Algorithms Second Week Phase Summary

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. Explanation of key codes
Generating true scores in questions and converting questions containing true scores into suffix 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. Write

(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());
}
}

Guess you like

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