-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.java
More file actions
90 lines (78 loc) · 2.05 KB
/
Program.java
File metadata and controls
90 lines (78 loc) · 2.05 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
/**************************************
* Henrik Schulz - 2016 *
* DM803 - Advanced Datastructures *
**************************************/
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;
import java.nio.file.Files;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
public class Program {
public static void main(String[] args){
if (args.length == 0) {
System.out.println("Missing input file");
System.exit(0);
}
if (args.length > 1) {
System.out.println("Too many arguments");
System.exit(0);
}
List<String> lines = new ArrayList<String>();
try{
lines = Files.readAllLines(Paths.get(args[0]), Charset.defaultCharset());
}
catch (IOException e){
System.out.println("Something went wrong");
}
Scanner sc = new Scanner(System.in);
System.out.println("Please choose an Alpha between 1,0 and 0,5");
double alpha = sc.nextDouble();
int size = lines.size();
ScapegoatTree tree = new ScapegoatTree(alpha);
try {
File file = new File("output.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for(int i = 0; i < size; i++) {
char operation = lines.get(i).charAt(0);
int number = Integer.parseInt(lines.get(i).substring(2));
switch (operation) {
case 'I': if (tree.insert(number)) {
bw.write("S\n");
}
else {
bw.write("F\n");
}
break;
case 'D': if (tree.delete(number)) {
bw.write("S\n");
}
else {
bw.write("F\n");
}
break;
case 'S': if (tree.delete(number)) {
bw.write("S\n");
}
else {
bw.write("F\n");
}
break;
default: System.out.println("unknown operation");
}
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}