parent
16e6e8d180
commit
de25bcab06
@ -0,0 +1,119 @@
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Middleproblem implements Makeproblem {
|
||||
private Random random = new Random();
|
||||
static int basic = 1;
|
||||
String name = "1";
|
||||
String time = "1";
|
||||
int num = 1;
|
||||
|
||||
public static void main(String[] args) {
|
||||
for (int i = 0; i < 15; i++) {
|
||||
basic = 1;
|
||||
Middleproblem m1 = new Middleproblem();
|
||||
System.out.println((i + 1) + ". " + m1.generateSmartExpression());
|
||||
}
|
||||
}
|
||||
|
||||
void start(int num, String username) {
|
||||
name = username;
|
||||
Timemanage t1 = new Timemanage();
|
||||
time = t1.timeget();
|
||||
for (int i = 0; i < num; i++) {
|
||||
basic = 1;
|
||||
System.out.println((i + 1) + ". " + generateSmartExpression());
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateSmartExpression() {
|
||||
int operatorCount = random.nextInt(3) + 1;
|
||||
int b2 = random.nextInt(operatorCount) + 1;
|
||||
String file = time;
|
||||
String result;
|
||||
while (true) {
|
||||
int flag = 0;
|
||||
result = buildExpression(operatorCount, b2);
|
||||
filemanage f1 = new filemanage();
|
||||
f1.fileread(name);
|
||||
f1.fileread(name);
|
||||
for (String i : f1.array) {
|
||||
String[] parts = i.split("\\.", 2);
|
||||
String j = parts.length > 1 ? parts[1] : i;
|
||||
if (result.equals(j)) {
|
||||
flag = 1;
|
||||
}
|
||||
}
|
||||
if (flag == 1) {
|
||||
continue;
|
||||
}
|
||||
f1.filecreate(name, file);
|
||||
f1.filewrite(name, file, num + "." + result);
|
||||
f1.filewrite(name, file, " ");
|
||||
num++;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String buildExpression(int operatorCount, int b2) {
|
||||
if (operatorCount == 1) {
|
||||
return generateSimpleExpression(b2);
|
||||
}
|
||||
|
||||
// 随机决定分割点来添加括号
|
||||
int splitPoint = random.nextInt(operatorCount - 1) + 1;
|
||||
|
||||
String leftPart = (splitPoint > 1 && random.nextBoolean()) ?
|
||||
"(" + buildExpression(splitPoint, b2) + ")" :
|
||||
buildExpression(splitPoint, b2);
|
||||
|
||||
String rightPart = (operatorCount - splitPoint > 1 && random.nextBoolean()) ?
|
||||
"(" + buildExpression(operatorCount - splitPoint, b2) + ")" :
|
||||
buildExpression(operatorCount - splitPoint, b2);
|
||||
|
||||
char operator = getRandomOperator("+-*/");
|
||||
return leftPart + " " + operator + " " + rightPart;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateSimpleExpression(int b2) {
|
||||
int a = random.nextInt(100) + 1;
|
||||
int b = random.nextInt(100) + 1;
|
||||
char op = getRandomOperator("+-*/√²");
|
||||
if (basic == b2) {
|
||||
op = getRandomOperator("√²");
|
||||
if (op == '√') {
|
||||
basic = 0;
|
||||
return op + "" + b;
|
||||
} else if (op == '²') {
|
||||
basic = 0;
|
||||
return a + "" + op;
|
||||
}
|
||||
} else {
|
||||
basic++;
|
||||
}
|
||||
if (random.nextInt(2) == 0) {
|
||||
return a + "";
|
||||
}
|
||||
if (op == '√') {
|
||||
return op + "" + b;
|
||||
} else if (op == '²') {
|
||||
return a + "" + op;
|
||||
} else {
|
||||
if (random.nextBoolean()) {
|
||||
return a + " " + op + " " + b;
|
||||
} else {
|
||||
return "(" + a + " " + op + " " + b + ")";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getRandomOperator(String operators) {
|
||||
return operators.charAt(random.nextInt(operators.length()));
|
||||
}
|
||||
}
|
Loading…
Reference in new issue