1.需求分析

   支持四则运算

   支持括号

   限定题目数量

   做题正确率统计

2.代码部分

import java.util.Random;
import java.util.Scanner;

public class szysfour {
    public static void main(String[] args){
        double right = 0;
        double wrong = 0;
        String inputstring = "";
        double res = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("输入题目数量");
        int m = sc.nextInt();
        for (int i = 0; i < m; i++) {
            System.out.println("第"+(i+1)+"道题,请在下方输入答案:");

            Random r1 = new Random();
            int x = r1.nextInt(10)+1;
            Random r2 = new Random();
            int y = r2.nextInt(10)+1;
            Random r3 = new Random();
            int z = r3.nextInt(10)+1;
            Random r4 = new Random();
            int w = r4.nextInt(10)+1;
            Random r5 = new Random();
            int l = r5.nextInt(4);
            char[] chs = {'+','-','*','/'};
            String Operator1 = String.valueOf(chs[l]);
            Random r6 = new Random();
            int j = r5.nextInt(4);
            char[] chs1 = {'+','-','*','/'};
            String Operator2 = String.valueOf(chs[j]);
            Random r7 = new Random();
            int k = r5.nextInt(4);
            char[] chs2 = {'+','-','*','/'};
            String Operator3 = String.valueOf(chs[k]);
            inputstring = String.valueOf(x+Operator1+y+Operator2+z+Operator3+w);
            System.out.print(inputstring+ " = ");
            res = calculation_all_mode(inputstring);
            Scanner scf = new Scanner(System.in);
            int num = scf.nextInt();
            if(num == res){

                right++;System.out.println("答对啦,你真是个天才!!! ");
            }

            else{

                wrong++;System.out.println("再想想吧,答案似乎是"+res+"喔!");
            }
            System.out.println("-------------------------------");

        }
        System.out.println("您一共做对了"+right+"道题.");
        System.out.println("您一共做错了"+wrong+"道题.");
        if(wrong > 0){
            System.out.println("正确率为"+(right/(wrong+right))*100+"%");
        }

        else{
            System.out.println("没法除了啊 。。。。");
        }
    }
    public static char[] operator = new char[40];
    public static double[] data = new double[40];

    static int delete_char_array(int len, int pos) {
        if(pos >= len) return len;
        for(int i = pos; i < len - 1; i++) {
            operator[i] = operator[i + 1];
        }
        return len - 1;
    }

    static int delete_double_array(int len, int pos) {
        if(pos >= len) return len;
        for(int i = pos; i < len - 1; i++) {
            data[i] = data[i + 1];
        }
    return len - 1;
    }

    public static sslength analysis_equation(String input) {
        int start = 0, end = 0;
        int i, count = 0;
        sslength len = new sslength();

        function.log_printf("analysis_equation input " + input);

        for(i = 0; i < input.length(); i++) {
            if(function.match_single(input, i)) {
                function.log_printf("match " + i + ", char = " + input.charAt(i));
                end = i;
                data[count] = function.string_to_double(input.substring(start, end));
                function.log_printf("data[" + count + "] = " + data[count]);
                operator[count] = input.charAt(i);
                count++;
                start = end + 1;
            }
        }
        function.log_printf("start = " + start + ", end = " + end);
        data[count] = function.string_to_double(input.substring(start));
        function.log_printf("data[" + count + "] = " + data[count]);
        len.data_length = count + 1;
        len.operator_length = count;
        return len;
    }

    public static double calculation(sslength len) {
        int count = 0;
        int leno = len.operator_length;
        int lend = len.data_length;
        double res;

        while(true) {
            if(leno == 1) {
                res = function.two_numbers(data[0], data[1], operator[0]);
                break;
            }

            else if(leno == 0) {
                res = data[0];
                break;
            }

            if(function.compare_step(operator[count], operator[count + 1])) {
                res = function.two_numbers(data[count], data[count + 1], operator[count]);
                data[count] = res;
                lend = delete_double_array(lend, count + 1);
                leno = delete_char_array(leno, count);
                count = 0;
            }

            else {
                count++;
                if(count >= leno) count = 0;
            }
        }
        return res;
    }
    public static double calculation_mode(String input) {
        sslength len = analysis_equation(input);

        return calculation(len);
    }
    public static double calculation_all_mode(String input) {
        String sample = "";
        int start_right = 0, end_right = 0;
        double res = 0;

        while(true) {
            if(!input.contains("(")) break;
            end_right = input.length();
            for(int i = 0; i < end_right; i++) {
                if(input.charAt(i) == '(') start_right = i + 1;
                if(input.charAt(i) == ')') {
                    String str = input.substring(start_right, i);
                    function.log_printf("fle-->" + str);
                    res = calculation_mode(str);
                    input = input.replace("(" + str + ")", "" + res);
                    break;
                }
            }
        }
        return calculation_mode(input);
}
}

 

3.演示结果

posted on 2016-09-28 12:41  谭力铭  阅读(200)  评论(1编辑  收藏  举报