1 import java.io.BufferedReader;
2 import java.io.IOException;
3 import java.io.InputStreamReader;
4 import java.util.HashMap;
35 {
"",
"I",
"II",
"III",
"IV",
"V",
"VI",
"VII",
"VIII",
"IX" },
36 {
"",
"X",
"XX",
"XXX",
"XL",
"L",
"LX",
"LXX",
"LXXX",
"XC" },
37 {
"",
"C",
"CC",
"CCC",
"CD",
"D",
"DC",
"DCC",
"DCCC",
"CM" },
38 {
"",
"M",
"MM",
"MMM",
"(IV)",
"(V)",
"(VI)",
"(VII)",
"(VIII)",
"(IX)"},
39 {
"",
"(X)",
"(XX)",
"(XXX)",
"(XL)",
"(L)",
"(LX)",
"(LXX)",
"(LXXX)",
"(XC)"},
40 {
"",
"(C)",
"(CC)",
"(CCC)",
"(CD)",
"(D)",
"(DC)",
"(DCC)",
"(DCCC)",
"(CM)"},
41 {
"",
"(M)",
"(MM)",
"(MMM)",
"",
"",
"",
"",
"",
""}
47 private static Map<Character, Integer>
d =
null;
50 d =
new HashMap<Character, Integer>();
63 private static int[]
l =
null;
84 private static String[]
rSymbols = {
"M",
"CM",
"D",
"CD",
"C",
"XC",
"L",
"XL",
"X",
"IX",
"V",
"IV",
"I"};
88 private static int[]
decimals = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
93 private String
rnum =
null;
116 if ( val.length() > 0 ) {
117 throw new ValueError(String.format (
"Exception %s : Invalid roman \"%s\"", val, r));
142 return String.format(
"%d in roman = %s",
inum,
rnum );
170 StringBuilder
roman =
new StringBuilder();
171 if ( (num >= 4000000) || (num < 1) )
175 int scnum = cnum.length();
176 int strlen = scnum-1;
183 for (
int i=0; i<scnum; i++) {
184 dig = cnum.charAt(i) -
'0';
201 if ((num >= 4000000) || (num < 1))
210 excess = excess/10*10;
218 for (
int i = 0; i < temp; ++i )
249 int total = 0, pptr = 0, cptr = 0;
251 int len =
roman.length();
253 String validc =
"MDCLXVImdclxvi()";
254 for (
int i = 0; i < len; i++ ) {
255 c =
roman.charAt ( len-i-1 );
256 if ( validc.indexOf(c) >= 0 ) {
262 cptr =
l[Character.toUpperCase(c)];
263 if ( m ) cptr *= 1000;
272 throw new ValueError(String.format (
"Invalid character \"%c\"", c));
284 romano = romano.toUpperCase();
285 String[] s = {romano,
""};
287 int position = romano.indexOf(
"(" );
288 if ( position >= 0 ) {
289 int position2 = romano.indexOf(
")" );
290 s[0] = romano.substring (position+1, position2);
291 s[1] = romano.substring (position2+1, romano.length());
294 for (
int j = 0; j < 2; ++j ) {
297 if ( lr == 0 )
continue;
298 if (
"MDCLXVI".indexOf(r.charAt(lr-1)) < 0 )
return Character.toString(r.charAt(lr-1));
299 for (
int i=0; i < lr-1; ++i ) {
300 char c = r.charAt(i);
301 char c1 = r.charAt(i+1);
303 if (
"MDCLXVI".indexOf(c) < 0)
return Character.toString(c);
305 char c2 = r.charAt(i+2);
307 char c3 = r.charAt(i+3);
308 if ( c == c1 && c == c2 && c == c3 )
309 return "0: " + r.substring (i,i+4);
311 if (
d.get(c2)!=
null && (
d.get(c) <
d.get(c2)) )
312 return "1: " + r.substring (i,i+3);
313 if (
"VLD".indexOf(c) >= 0 && c == c2 )
314 return "2: " + r.substring (i,i+3);
315 if ( c ==
'I' && c2 ==
'I' && c1 !=
'I' )
316 return "3: " + r.substring (i,i+3);
317 if ( c ==
'X' && c2 ==
'X' &&
"LC".indexOf(c1) >= 0 )
318 return "4: " + r.substring (i,i+3);
321 if (
d.get(c1)!=
null && (
d.get(c) <
d.get(c1)) ) {
322 if (
"IXC".indexOf(c) < 0 )
323 return "5: " + r.substring (i,i+2);
324 if ( c ==
'I' &&
"VX".indexOf(c1) < 0 )
325 return "6: " + r.substring (i,i+2);
326 if ( c ==
'X' &&
"LC".indexOf(c1) < 0 )
327 return "7: " + r.substring (i,i+2);
328 if ( c ==
'C' &&
"DM".indexOf(c1) < 0 )
329 return "8: " + r.substring (i,i+2);
331 if (
"VLD".indexOf(c) >= 0 && c == c1 )
332 return "9: " + r.substring (i,i+2);
343 public static void main(String[] args) {
344 String jVersion = System.getProperty(
"java.version");
345 jVersion = jVersion.substring(0, 3);
346 Float f = Float.valueOf(jVersion);
347 if (f.floatValue() < (
float)1.5) {
348 System.out.println(String.format(
"Java version %s is too low ....", jVersion));
353 System.out.print (
"Type an integer: " );
355 r = Integer.parseInt(System.console().readLine());
358 catch (NumberFormatException e) {
359 System.out.println (
"A decimal number should have been typed\n" );
361 catch (NullPointerException e) {
364 BufferedReader bufferedReader =
new BufferedReader(
new InputStreamReader(System.in));
366 r = Integer.parseInt(bufferedReader.readLine());
367 }
catch (IOException e2) {
368 System.out.println (e2);
369 System.out.println (
"A decimal number should have been typed\n" );
374 System.out.println ( rm );
375 System.out.println ( String.format(
"%d in roman = %s", r, rs ) );
378 rm =
new roman ( rs );
379 System.out.println ( String.format(
"%s in decimal = %d", rs, rm.
toInt() ) );
382 System.out.println (e.getMessage());
385 String invalidRoman[] = { rs,
"XXXX",
"ivLdi",
"IIV",
"LXL",
"IVI",
"XLX",
"DM",
"iM",
"mxm",
"CmC",
"VV",
"va",
"xvibc" };
387 for ( String s : invalidRoman ) {
389 System.out.println ( String.format(
"%s is %s%s", s, (val.length()==0)?
"valid":
"not valid -> ", val ) );
393 TextIO.
putln(
"Enter a Roman numeral and I will convert it to an ordinary");
394 TextIO.
putln(
"arabic integer. Enter an integer in the range 1 to 3,999,999");
395 TextIO.
putln(
"and I will convert it to a Roman numeral. Press return when");
411 rm =
new roman(arabic);
417 rm =
new roman (str);