Java 生成小学生100以内的四则运算加减乘除,不能出现小数和负数

此文针对3个数的加减乘除,没有括号,小数,负数

运行示例

在这里插入图片描述

import java.util.*;

public class Student {

    /**
     * 存储表达式
     */
    private static String strTest = "";
    private static final Random RANDOM = new Random();
    private static final Scanner SCANNER = new Scanner(System.in);
    private static Integer number_random;

    public static void main(String[] args) {
        System.out.print("请输入参与数字的最大值(建议33):");
        number_random = SCANNER.nextInt();
        System.out.print("请输入要生成题目数量:");
        getExpression(SCANNER.nextInt());
    }

    /**
     * 计算公约数(在下面getExpression方法里面已经排除 0 )
     */
    public static int gcd(int a, int b) {
        if (a % b == 0) {
            return b;
        } else {
            return gcd(b, a % b);
        }
    }

    /**
     * 获取随机运算符
     */
    public static char getOperator() {
        char[] signal = {'+', '-', '*', '/'};
        return signal[RANDOM.nextInt(4)];
    }


    /**
     * 生成表达式
     */
    public static void getExpression(int getNums) {
        char operator1, operator2;
        int num1, num2, num3;
        while (getNums > 0) {
            boolean flag = false;
            getNums--;
            operator1 = getOperator();
            operator2 = getOperator();
            //保证两个不同的运算符
            while (operator1 == operator2) {
                operator1 = getOperator();
                operator2 = getOperator();
            }
            // + 1 是避免出现 0
            num1 = RANDOM.nextInt(number_random) + 1;
            num2 = RANDOM.nextInt(number_random) + 1;
            num3 = RANDOM.nextInt(number_random) + 1;

            //以下判断只是为了初步判断是否出现小数和负数
            if (operator1 == '/') {
                if (gcd(num1, num2) != num2) {
                    flag = true;
                }
            }
            if (operator2 == '/') {
                if (gcd(num2, num3) != num3) {
                    flag = true;
                }
            }
            if (operator1 == '-') {
                if (num1 - num2 <= 0) {
                    flag = true;
                }
            }
            if (operator2 == '-') {
                if (num2 - num3 <= 0) {
                    flag = true;
                }
            }
            if (operator1 == '*') {
                if (num1 * num2 > 99) {
                    flag = true;
                }
            }
            if (operator2 == '*') {
                if (num2 * num3 > 99) {
                    flag = true;
                }
            }
            if (operator1 == '+') {
                if (num1 + num2 > 99) {
                    flag = true;
                }
            }
            if (operator2 == '+') {
                if (num2 + num3 > 99) {
                    flag = true;
                }
            }
            if (flag) {
                getNums++;
                continue;
            }
            strTest += num1 + "" + operator1 + "" + num2 + "" + operator2 + num3;
            String str = calculate(strTest);
            strTest = "";
            double res = Double.parseDouble(str);
            int intRes = (int) res;
            if (intRes >= 0 && intRes <= 100) {
                String res2 = "" + num1 + "" + operator1 + "" + num2 + "" + operator2 + "" + num3 + "" + '=' + "" + intRes;
                String replace = res2.replace('/', '÷');
                System.out.println(replace);
            } else {
                getNums++;
            }
        }
    }

// -----------------------------核心算法-----------------------------

    /**
     * 计算
     */
    public static String calculate(String expression) {
        List<String> num;
        Stack<Double> stack;
        num = transformEnd(expression);
        stack = new Stack<>();
        double sum;
        while (!num.isEmpty()) {
            String temp = String.valueOf(num.remove(0));
            if (isNumber(temp)) {
                double s = Double.parseDouble(temp);
                stack.push(s);
            } else {
                double a = stack.pop();
                double b = stack.pop();
                double c = calTwo(b, a, temp);
                stack.push(c);
            }
        }
        sum = stack.pop();
        return String.valueOf(sum);
    }

    /**
     * 计算两个值
     */
    private static double calTwo(double a, double b, String opr) {
        double sum = 0.0;
        switch (opr) {
            case "+":
                sum = a + b;
                break;
            case "-":
                sum = a - b;
                break;
            case "*":
                sum = a * b;
                break;
            case "/":
                sum = a / b;
                break;
        }
        return sum;
    }

    /**
     * 将中缀表达式转化为后缀表达式(栈是用来进出运算的符号)
     */
    public static List<String> transformEnd(String expre) {
        List<String> sb = new ArrayList<>();
        Stack<String> stack = new Stack<>();
        expre = expre.replaceAll("(\\D)", "o$1o");
        String[] esp = expre.trim().split("o");
        for (String value : esp) {
            String s = value.trim();
            if (isNumber(s)) {
                // 如果是数字则输出
                sb.add(s);
            } else if (!s.isEmpty()) {
                if (!stack.isEmpty() && !isMaxExp(s.charAt(0), stack.peek().charAt(0))) {
                    while (!stack.isEmpty() && !isMaxExp(s.charAt(0), stack.peek().charAt(0))) {
                        sb.add(stack.pop());
                    }
                }
                stack.push(s);
            }
        }
        while (!stack.isEmpty()) {
            sb.add(stack.pop());
        }
        return sb;
    }

    /**
     * 判断是否是数字
     */
    private static boolean isNumber(String str) {
        try {
            Double.parseDouble(str);
        } catch (RuntimeException e) {
            return false;
        }
        return true;
    }

    /**
     * 判断两个操作符优先级
     */
    private static boolean isMaxExp(char exp1, char exp2) {
        return transExp(exp1) > transExp(exp2);
    }

    /**
     * 优先级
     */
    private static int transExp(char exp) {
        int re = 0;
        switch (exp) {
            case '*':
            case '/':
                re = 2;
                break;
            case '+':
            case '-':
                re = 1;
                break;
        }
        return re;
    }
}
  • 0
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值