parent
9f9468503a
commit
4ab0b123e8
@ -1,120 +0,0 @@
|
|||||||
import java.util.Random;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class Primaryproblem implements Makeproblem {
|
|
||||||
private static Random random = new Random();
|
|
||||||
static int basic = 1;
|
|
||||||
static int basic1 = 1;
|
|
||||||
String name = "1";
|
|
||||||
String time = "1";
|
|
||||||
int num = 1;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Primaryproblem p1 = new Primaryproblem();
|
|
||||||
p1.name = "张三1";
|
|
||||||
Timemanage t1 = new Timemanage();
|
|
||||||
p1.time = t1.timeget();
|
|
||||||
for (int i = 0; i < 15; i++) {
|
|
||||||
basic = 1;
|
|
||||||
basic1 = 1;
|
|
||||||
System.out.println((i + 1) + ". " + p1.generateSmartExpression());
|
|
||||||
p1.num++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void start(int num, String username) {
|
|
||||||
name = username;
|
|
||||||
Timemanage t1 = new Timemanage();
|
|
||||||
time = t1.timeget();
|
|
||||||
for (int i = 0; i < num; i++) {
|
|
||||||
basic = 1;
|
|
||||||
basic1 = 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 (basic1 == 3) {
|
|
||||||
return a + "";
|
|
||||||
}
|
|
||||||
if (basic == b2) {
|
|
||||||
op = getRandomOperator("+-*/");
|
|
||||||
basic1++;
|
|
||||||
return a + " " + op + " " + b;
|
|
||||||
} else {
|
|
||||||
basic++;
|
|
||||||
}
|
|
||||||
if (random.nextInt(2) == 0) {
|
|
||||||
return a + "";
|
|
||||||
}
|
|
||||||
if (random.nextBoolean()) {
|
|
||||||
basic1++;
|
|
||||||
return a + " " + op + " " + b;
|
|
||||||
} else {
|
|
||||||
basic1++;
|
|
||||||
return "(" + a + " " + op + " " + b + ")";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public char getRandomOperator(String operators) {
|
|
||||||
return operators.charAt(random.nextInt(operators.length()));
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue