1
+ /*Copyright (c) Jan 10, 2016 CareerMonk Publications and others.
2
+
3
+ * Creation Date : 2015-01-10 06:15:46
4
+ * Last modification : 2006-05-31
5
+ by : Narasimha Karumanchi
6
+ * File Name : RomanToDecimal.java
7
+ * Book Title : Data Structures And Algorithms Made In Java
8
+ * Warranty : This software is provided "as is" without any
9
+ * warranty; without even the implied warranty of
10
+ * merchantability or fitness for a particular purpose.
11
+ *
12
+ */
13
+
14
+
15
+ package chapter21miscconcepts ;
16
+
17
+ import java .util .ArrayList ;
18
+ import java .util .List ;
19
+
20
+ public class RomanToDecimal {
21
+ public static int romanToDecimal (java .lang .String romanNumber ) {
22
+ int decimal = 0 ;
23
+ int lastNumber = 0 ;
24
+ if (romanNumber .isEmpty ())
25
+ return 0 ;
26
+ String romanNumeral = romanNumber .toUpperCase ();
27
+
28
+ /* operation to be performed on upper cases even if user enters Roman values in lower case chars */
29
+ for (int x = romanNumeral .length () - 1 ; x >= 0 ; x --) {
30
+ char convertToDecimal = romanNumeral .charAt (x );
31
+
32
+ switch (convertToDecimal ) {
33
+ case 'M' :
34
+ decimal = processDecimal (1000 , lastNumber , decimal );
35
+ lastNumber = 1000 ;
36
+ break ;
37
+
38
+ case 'D' :
39
+ decimal = processDecimal (500 , lastNumber , decimal );
40
+ lastNumber = 500 ;
41
+ break ;
42
+
43
+ case 'C' :
44
+ decimal = processDecimal (100 , lastNumber , decimal );
45
+ lastNumber = 100 ;
46
+ break ;
47
+
48
+ case 'L' :
49
+ decimal = processDecimal (50 , lastNumber , decimal );
50
+ lastNumber = 50 ;
51
+ break ;
52
+
53
+ case 'X' :
54
+ decimal = processDecimal (10 , lastNumber , decimal );
55
+ lastNumber = 10 ;
56
+ break ;
57
+
58
+ case 'V' :
59
+ decimal = processDecimal (5 , lastNumber , decimal );
60
+ lastNumber = 5 ;
61
+ break ;
62
+
63
+ case 'I' :
64
+ decimal = processDecimal (1 , lastNumber , decimal );
65
+ lastNumber = 1 ;
66
+ break ;
67
+
68
+ default :
69
+ return -1 ;
70
+
71
+ }
72
+ }
73
+
74
+ return decimal ;
75
+ }
76
+
77
+ public static int processDecimal (int decimal , int lastNumber , int lastDecimal ) {
78
+ if (lastNumber > decimal ) {
79
+ return lastDecimal - decimal ;
80
+ } else {
81
+ return lastDecimal + decimal ;
82
+ }
83
+ }
84
+
85
+ public static void main (java .lang .String args []) {
86
+ List <String > romanList = new ArrayList <String >();
87
+ romanList .add ("" );
88
+ romanList .add ("MMDCDXLIV" );
89
+ romanList .add ("MMLVII" );
90
+ romanList .add ("100" );
91
+ romanList .add ("MMDCDXLV" );
92
+ romanList .add ("VIV" );
93
+ romanList .add ("MIX" );
94
+ romanList .add ("LID" );
95
+ romanList .add ("DIX" );
96
+ romanList .add ("LICX" );
97
+ romanList .add ("CLIX" );
98
+ romanList .add ("MMDLIV" );
99
+ romanList .add ("MMXDLIV" );
100
+ romanList .add ("MCMXCX" );
101
+ romanList .add ("CCCXCIX5" );
102
+ for (int i =0 ; i <romanList .size ();i ++){
103
+ int decimalValue = romanToDecimal (romanList .get (i ));
104
+ if (decimalValue == -1 )
105
+ System .out .println ("Not a valid Roman Number." );
106
+ else
107
+ System .out .println (decimalValue );
108
+ }
109
+ }
110
+ }
0 commit comments