-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionGenerator.java
More file actions
75 lines (65 loc) · 1.68 KB
/
Copy pathExpressionGenerator.java
File metadata and controls
75 lines (65 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.Scanner;
public class ExpressionGenerator {
private static int maxDepth;
public static int randInt(int min, int max) {
return (int) (Math.random() * ((max + 1) - min)) + min;
}
/*
* < Digit > ::= "0" | "1" | "2" | ... | "9"
*/
public static String digit(int depth) {
return Character.toString((char) ('0' + randInt(0, 9)));
}
/*
* < Number > ::= < Number > < Digit >
* | < Digit >
*/
public static String number(int depth) {
int rule;
if(depth >= maxDepth) {
rule = 2;
} else {
rule = randInt(1,2);
}
if (rule == 1) {
return number(depth+1) + digit(depth+1);
} else {
return digit(depth+1);
}
}
/*
* < Expression > ::= < Expression > "+" < Expression >
* | < Expression > "-" < Expression >
* | < Expression > "*" < Expression >
* | < Expression > "/" < Expression >
* | < Number >
*/
public static String expression(int depth) {
int rule;
if(depth >= maxDepth) {
rule = 5;
} else {
rule = randInt(1,5);
}
switch(rule) {
case 1:
return expression(depth+1) + "+" + expression(depth+1);
case 2:
return expression(depth+1) + "-" + expression(depth+1);
case 3:
return expression(depth+1) + "*" + expression(depth+1);
case 4:
return expression(depth+1) + "/" + expression(depth+1);
default:
return number(depth+1);
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Max Depth: ");
maxDepth = scan.nextInt();
for (int i = 0; i < 1000; i++) {
System.out.println(expression(0));
}
}
}