-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.java
More file actions
201 lines (181 loc) · 5 KB
/
Copy pathSort.java
File metadata and controls
201 lines (181 loc) · 5 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import java.util.*;
public class Sort {
protected int stepCount = 0;
protected boolean isNumber = false;
protected ArrayList<String> dataset;
protected ArrayList<String> result = new ArrayList<String>();
protected ArrayList<String> record = new ArrayList<String>();
public Sort() {
dataset = new ArrayList<String>();
isNumber = judgeType();
}
public Sort(ArrayList<String> input) {
dataset = new ArrayList<String>(input);
String temp = "Before sort:";
for (String str : input) {
temp += " " + str;
}
record.add(temp);
isNumber = judgeType();
}
public Sort(String... input) {
dataset = new ArrayList<String>();
String temp = "Before sort:";
for (String str : input) {
dataset.add(str);
temp += " " + str;
}
record.add(temp);
isNumber = judgeType();
}
public boolean judgeType() {
for (String i : dataset) {
if (i.charAt(0) != '-' && (i.charAt(0) < '0' || i.charAt(0) > '9')) {
return false;
} else if (i.indexOf("-") != i.lastIndexOf("-")) {
return false;
} else if (i.indexOf(".") != i.lastIndexOf(".")) {
return false;
} else if (i.indexOf(".") == i.length() - 1) {
return false;
}
for (char j : i.toCharArray()) {
if (j != '-' && j != '.' && (j < '0' || j > '9')) {
return false;
}
}
}
return true;
}
public boolean getIsNumber() {
return isNumber;
}
public ArrayList<String> getResult() {
return result;
}
public ArrayList<String> getDataset() {
return dataset;
}
public ArrayList<String> getRecord() {
return record;
}
public boolean addString(String... input) {
if (input.length == 0) {
return false;
}
stepCount = 0;
record.clear();
for (String str : input) {
dataset.add(str);
}
String temp = "Before sort:";
for (String str : dataset) {
temp += " " + str;
}
record.add(temp);
isNumber = judgeType();
return true;
}
public boolean randomNumber(int amount, double n1, double n2) {
if (amount < 1) {
return false;
}
double max, min;
if (n1 > n2) {
max = n1;
min = n2;
} else {
max = n2;
min = n1;
}
stepCount = 0;
record.clear();
Random r = new Random();
String d = new String();
for (int i = 0; i < amount; i++) {
d = Double.toString(Math.random() * (max - min) + min);
switch (r.nextInt(3)) {
case 0:
d = d.substring(0, d.indexOf("."));
break;
case 1:
d = d.substring(0, d.indexOf(".") + 2);
break;
case 2:
d = d.substring(0, d.indexOf(".") + 3);
}
dataset.add(d);
}
String temp = "Before sort:";
for (String str : dataset) {
temp += " " + str;
}
record.add(temp);
isNumber = judgeType();
return true;
}
public boolean randomString(int amount, int n1, int n2) {
if (amount < 1 || n1 < 1 || n2 < 1) {
return false;
}
int max, min;
if (n1 > n2) {
max = n1;
min = n2;
} else {
max = n2;
min = n1;
}
stepCount = 0;
record.clear();
Random r = new Random();
for (int i = 0; i < amount; i++) {
int size = r.nextInt(max - min + 1) + min;
String d = new String();
for (int j = 0; j < size; j++) {
d += (char) (r.nextInt(94) + 33);
}
dataset.add(d);
}
String temp = "Before sort:";
for (String str : dataset) {
temp += " " + str;
}
record.add(temp);
isNumber = false;
return true;
}
public String previous() {
if (record.size() == 1) {
return null;
}
record.remove(stepCount--);
String temp = record.get(record.size() - 1);
dataset.clear();
StringTokenizer st = new StringTokenizer(temp);
st.nextToken();
st.nextToken();
while (st.hasMoreTokens()) {
dataset.add(st.nextToken());
}
return temp;
}
public boolean aStepByStep(){
return false;
}
public int aTimeComplexity(){
return -1;
}
public String aNext(){
return null;
}
public boolean dStepByStep(){
return false;
}
public int dTimeComplexity(){
return -1;
}
public String dNext(){
return null;
}
}