-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateRoman.java
More file actions
115 lines (94 loc) · 2.15 KB
/
GenerateRoman.java
File metadata and controls
115 lines (94 loc) · 2.15 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class GenerateRoman {
private String printRomanEquivalent(int number) {
int dig;
int idx;
StringBuffer romanEquivalent = new StringBuffer();
if (number >= 1000) {
dig = number/1000;
for (idx = 0; idx < dig; idx++) {
romanEquivalent.append("M");
}
number-=dig*1000;
}
if (number >= 900) {
romanEquivalent.append("CM");
number-=900;
}
if (number >= 500) {
romanEquivalent.append("D");
number-=500;
}
if (number >= 400) {
romanEquivalent.append("CD");
number-=400;
}
if (number >= 100) {
dig = number/100;
for (idx = 0; idx < dig; idx++) {
romanEquivalent.append("C");
}
number-=dig*100;
}
if (number >= 90) {
romanEquivalent.append("XC");
number-=90;
}
if (number >= 50) {
romanEquivalent.append("L");
number-=50;
}
if (number >= 40) {
romanEquivalent.append("XL");
number-=40;
}
if (number >= 10) {
dig = number/10;
for (idx = 0; idx < dig; idx++) {
romanEquivalent.append("X");
}
number-=dig*10;
}
if (number == 9) {
romanEquivalent.append("IX");
number-=9;
}
if (number >= 5) {
romanEquivalent.append("V");
number-=5;
}
if (number == 4) {
romanEquivalent.append("IV");
number-=4;
}
if (number >= 1) {
for (idx = 0; idx < number; idx++) {
romanEquivalent.append("I");
}
number-=number;
}
return romanEquivalent.toString();
}
public GenerateRoman(String filename) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
int number;
while ((line = br.readLine()) != null) {
number = Integer.valueOf(line).intValue();
if (number > 0 && number < 4000) {
System.out.println(printRomanEquivalent(number));
} else {
System.out.println("Unsupported number.");
}
}
}
public static void main (String[] args) throws IOException {
if (args.length != 1) {
System.out.println("Insufficient number of parameters passed. Exiting.");
System.exit(1);
}
GenerateRoman genRom = new GenerateRoman(args[0]);
}
}