|博客班级 | https://edu.cnblogs.com/campus/ahgc/AHPU-SE-19/ |
|作业要求 | https://edu.cnblogs.com/campus/ahgc/AHPU-SE-19/homework/11376|
|作业目标 | 写一个能自动生成小学四则运算题目的程序 |
|学号 | 3190704130 |
代码如下:
//main函数所在区域主要实现生成问题
`package xiaohu;
import java.util.Scanner;
public class CT {
public static String str = "";
public static int num = 5;
public static int num_i = 0;
public static int numberRange = 100;
public static double sum = 0;
public static void main(String[] args) {
System.out.println("当然是小胡啦");
System.out.println("控制台实现出题判断");
System.out.println("注意:结果保留1位小数!");
System.out.println("共10道题目:");
Scanner in = new Scanner(System.in);
double answer = 0;
double result = 0;
String[] question = new String[10];
int questionNumber = 0;
int answerTrue = 0;
boolean flag;
for(;😉 {
answer = 0; result = 0; flag = true; str="";
if((questionNumber + 1)%5 != 0) {
GetQuestion_int();
} else {
GetQuestion_div();
}
for(int j = questionNumber-1; j >= 0; j --) {
if(question[j].equals(str)) {
flag = false; break;
}
}
if(!flag) continue;
else {question[questionNumber] = new String(str); questionNumber++;}
System.out.print("" + questionNumber + ". " + str+" = ");
answer = in.nextDouble();
if(!str.isEmpty()) {
result = Arithmetic.arithmetic(str);
}
if(answer == result) {
System.out.println(" ✔️");
answerTrue++;
} else {
System.out.println(" ❌ " + " 正确答案:" + result);
}
if(questionNumber == 10) break; // 满10个跳出
}
System.out.println("你的正确概率:" + answerTrue + "/10");
in.close();
}
private static void GetQuestion_int() {
//得到问题函数,在这里调用递归函数quesGrow()。
str = "";
sum = 0;
num_i = num;//用前都清零
quesGrow_int();
}
private static void GetQuestion_div() {
str = "";
sum = 0;
num_i = num;//用前都清零
quesGrow_div();
}
private static void quesGrow_int() {
//
if( num_i > 1 ) {
int j = num_i;//记录这是第几层调用。
num_i--;
quesGrow_int();//递归
int w=1+(int)(Math.random()*numberRange);//随机生成一个数
int t=1+(int)(Math.random()*100);//向左生成,还是向右生成,类似于树。
int f=1+(int)(Math.random()*100);//运算符控制
if(t>50)//新数往右加
{
if(f>50) {
sum = sum + w;
str = str + "+" + String.valueOf( w );
}
else {
sum = sum - w;
str = str + "-" + String.valueOf( w );
}
}
else//否则 新数往左加
{
if(f>50) {
sum = w + sum;
str = String.valueOf( w ) + "+" + str;
}
else {
if( j < 3 ) {//3——摸索出的数,不用给自己套上括号。实际上就是j=2
sum = w - sum;
str = String.valueOf( w ) + "-" + str;
}
else {
sum = w - sum;
str = String.valueOf( w ) + "-" + "(" +str+ ")";
//向左添减法的时候加括号,打破顺序计算模式。
}
}
}
}
else if( num_i == 1 ) {
//最后一层,也是输出的第一层
int w=1+(int)(Math.random()*numberRange);
sum = sum + w;
str = str + String.valueOf( w );
}
}
private static void quesGrow_div() {
if( num_i > 1 ) {
int j = num_i;//记录这是第几层调用。
num_i--;
quesGrow_div();//递归
double w=Math.random();//随机生成一个数
int t=1+(int)(Math.random()*100);//向左生成,还是向右生成,类似于树。
int f=1+(int)(Math.random()*100);//运算符控制
if(t>50)//新数往右加
{
if(f>50) {
sum = sum + w;
str = str + "+" + ChangeToFenshuDemo.toFenshu((""+(w+0.01)).substring(0, 4));
}
else {
sum = sum - w;
str = str + "-" + ChangeToFenshuDemo.toFenshu((""+(w+0.01)).substring(0, 4));
}
}
else//否则 新数往左加
{
if(f>50) {
sum = w + sum;
str = ChangeToFenshuDemo.toFenshu((""+(w+0.01)).substring(0, 4)) + "+" + str;
}
else {
if( j < 3 ) {//3——摸索出的数,不用给自己套上括号。实际上就是j=2
sum = w - sum;
str = ChangeToFenshuDemo.toFenshu((""+(w+0.01)).substring(0, 4)) + "-" + str;
}
else {
sum = w - sum;
str = ChangeToFenshuDemo.toFenshu((""+(w+0.01)).substring(0, 4)) + "-" + "(" +str+ ")";
//向左添减法的时候加括号,打破顺序计算模式。
}
}
}
}
else if( num_i == 1 ) {
//最后一层,也是输出的第一层
double w=Math.random();
sum = sum + w;
str = str + ChangeToFenshuDemo.toFenshu((""+(w+0.01)).substring(0, 4));
}
}
}`
//ChangeToFenshuDemo类实现将数转化为分数
`package xiaohu;
public class ChangeToFenshuDemo {
public static int getGongYueShu(int a, int b) {
int t = 0;
if(a < b){
t = a;
a = b;
b = t;
}
int c = a % b;
if(c == 0){
return b;
}else{
return getGongYueShu(b, c);
}
}
public static String toFenshu(String xiaoshu) {
String[] array = new String[2];
array = xiaoshu.split("\\.");
int a = Integer.parseInt(array[0]);//获取整数部分
int b = Integer.parseInt(array[1]);//获取小数部分
int length = array[1].length();
int FenZi = (int) (a * Math.pow(10, length) + b);
int FenMu = (int) Math.pow(10, length);
int MaxYueShu = getGongYueShu(FenZi, FenMu);
return new String(FenZi / MaxYueShu + "/" + FenMu / MaxYueShu);
}
}`
//Arithmetic类实现数的运算
`package xiaohu;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Arithmetic {
public static double arithmetic(String exp){
String result = parseExp(exp).replaceAll("[\[\]]", "");
return Double.parseDouble(result);
}
/**
* 解析计算四则运算表达式,例:2+((3+4)2-22)/23
*/
public static String parseExp(String expression){
expression=expression.replaceAll("\\s+", "").replaceAll("^\\((.+)\\)$", "$1");
String minExp="^((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\+\\-\\*\\/]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))$";
//最小表达式计算
if(expression.matches(minExp)){
String result=calculate(expression);
return Double.parseDouble(result)>=0?result:"["+result+"]";
}
//计算不带括号的四则运算
String noParentheses="^[^\\(\\)]+$";
String priorOperatorExp="(((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\*\\/]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\])))";
String operatorExp="(((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\+\\-]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\])))";
if(expression.matches(noParentheses)){
Pattern patt=Pattern.compile(priorOperatorExp);
Matcher mat=patt.matcher(expression);
if(mat.find()){
String tempMinExp=mat.group();
expression=expression.replaceFirst(priorOperatorExp, parseExp(tempMinExp));
}else{
patt=Pattern.compile(operatorExp);
mat=patt.matcher(expression);
if(mat.find()){
String tempMinExp=mat.group();
expression=expression.replaceFirst(operatorExp, parseExp(tempMinExp));
}
}
return parseExp(expression);
}
//计算带括号的四则运算
String minParentheses="\\([^\\(\\)]+\\)";
Pattern patt=Pattern.compile(minParentheses);
Matcher mat=patt.matcher(expression);
if(mat.find()){
String tempMinExp=mat.group();
expression=expression.replaceFirst(minParentheses, parseExp(tempMinExp));
}
return parseExp(expression);
}
/**
* 计算最小单位四则运算表达式(两个数字)
*/
public static String calculate(String exp){
exp=exp.replaceAll("[\\[\\]]", "");
String number[]=exp.replaceFirst("(\\d)[\\+\\-\\*\\/]", "$1,").split(",");
BigDecimal number1=new BigDecimal(number[0]);
BigDecimal number2=new BigDecimal(number[1]);
BigDecimal result=null;
/*
* 设置精度,否则报错(计算机并不知道要保留几位,所以很干脆的报错
*/
MathContext mc = new MathContext(2, RoundingMode.HALF_DOWN);
//精度为2,舍入模式为大于0.5进1,否则舍弃。
String operator=exp.replaceFirst("^.*\\d([\\+\\-\\*\\/]).+$", "$1");
if("+".equals(operator)){
result=number1.add(number2);
}else if("-".equals(operator)){
result=number1.subtract(number2);
}else if("*".equals(operator)){
result=number1.multiply(number2);
}else if("/".equals(operator)){
result=number1.divide(number2,mc);
}
return result!=null?result.toString():null;
}
}`
运行结果展示
psp表格: