- package romanop;
-
- import java.util.*;
-
- /**
- * @author Delache Michaël
- */
- public class RomanOp {
- private static final List<String> keys = new ArrayList<String>();
- private static final List<Integer> values = new ArrayList<Integer>();
-
- /**
- * Constructor of the RomanCalc class, initializes the two tables
- */
- public RomanOp() {
- keys.add("M");
- values.add(1000);
- keys.add("CM");
- values.add(900);
- keys.add("D");
- values.add(500);
- keys.add("CD");
- values.add(400);
- keys.add("C");
- values.add(100);
- keys.add("XC");
- values.add(90);
- keys.add("L");
- values.add(50);
- keys.add("XL");
- values.add(40);
- keys.add("X");
- values.add(10);
- keys.add("IX");
- values.add(9);
- keys.add("V");
- values.add(5);
- keys.add("IV");
- values.add(4);
- keys.add("I");
- values.add(1);
- }
- /**
- * Convert Roman to int
- * @param s Roman number
- * @return An int representing the roman number
- */
-
- public int toInt(final String s) {
- int res = 0;
- int size = s.length();
- String tmp = "";
- if (size == 1) {
- res = this.getInt(s);
- }
- if (size == 2) {
- if (keys.contains(s)) {
- res = this.getInt(s);
- }
- else {
- res += this.toInt(s.substring(0,1));
- res += this.toInt(s.substring(1,2));
- }
- }
- if (size > 2) {
- if (keys.contains(tmp = s.substring(size-2, size))) {
- res += this.getInt(tmp);
- res += this.toInt(s.substring(0, size-2));
- }
- else {
- tmp = s.substring(size-1, size);
- res += this.getInt(tmp);
- res += this.toInt(s.substring(0, size-1));
- }
- }
- return res;
- }
-
- /**
- * Returns the integer value of the roman value given by <i>s</i> or 0 if <i>s</i> is not in the table
- */
- public int getInt(final String s) {
- int res = 0;
- for (String str : keys) {
- if (str.equals(s)) {
- int index = keys.indexOf(str);
- res = values.get(index);
- }
- }
- return res;
- }
-
- /**
- * Converts integer value into roman value
- * @param i the integer to convert
- * @return a string representing the roman value
- */
- public String toRoman(final int i) {
- int cpt = i;
- String res = "";
- while (cpt > 0) {
- for (int j : values) {
- if ((cpt/j) >= 1) {
- int index = values.indexOf(j);
- res += keys.get(index);
- cpt -= j;
- break;
- }
- }
- }
- return res;
- }
-
- /**
- * Calculates the result of the operation determined by the two operands and the operator
- * @param x operand 1
- * @param y operand 2
- * @param op operator
- * @return the result
- */
- public String calc (final String x, final String y, final char op) {
- if (this.isOk(x) && this.isOk(y)) {
- int x1 = this.toInt(x);
- int y1 = this.toInt(y);
- switch (op) {
- case '+' :
- return this.toRoman(x1 + y1);
- case '-' :
- return this.toRoman(x1 - y1);
- case '*' :
- return this.toRoman(x1 * y1);
- case '/' :
- return this.toRoman(x1 / y1);
- default :
- break;
- }
- }
- return null;
- }
-
- /**
- * Says if the roman value given by <i>s</i> respect the roman value syntax
- * @param s the roman value to verify
- * @return true if <i>s</i> is correctly formed according to the roman value syntax (ie IV for 4 and not IIII), false else
- */
- public Boolean isOk(final String s) {
- int i = this.toInt(s);
- String str = this.toRoman(i);
- return s.equals(str);
- }
-
- }
package romanop;
import java.util.*;
/**
* @author Delache Michaël
*/
public class RomanOp {
private static final List<String> keys = new ArrayList<String>();
private static final List<Integer> values = new ArrayList<Integer>();
/**
* Constructor of the RomanCalc class, initializes the two tables
*/
public RomanOp() {
keys.add("M");
values.add(1000);
keys.add("CM");
values.add(900);
keys.add("D");
values.add(500);
keys.add("CD");
values.add(400);
keys.add("C");
values.add(100);
keys.add("XC");
values.add(90);
keys.add("L");
values.add(50);
keys.add("XL");
values.add(40);
keys.add("X");
values.add(10);
keys.add("IX");
values.add(9);
keys.add("V");
values.add(5);
keys.add("IV");
values.add(4);
keys.add("I");
values.add(1);
}
/**
* Convert Roman to int
* @param s Roman number
* @return An int representing the roman number
*/
public int toInt(final String s) {
int res = 0;
int size = s.length();
String tmp = "";
if (size == 1) {
res = this.getInt(s);
}
if (size == 2) {
if (keys.contains(s)) {
res = this.getInt(s);
}
else {
res += this.toInt(s.substring(0,1));
res += this.toInt(s.substring(1,2));
}
}
if (size > 2) {
if (keys.contains(tmp = s.substring(size-2, size))) {
res += this.getInt(tmp);
res += this.toInt(s.substring(0, size-2));
}
else {
tmp = s.substring(size-1, size);
res += this.getInt(tmp);
res += this.toInt(s.substring(0, size-1));
}
}
return res;
}
/**
* Returns the integer value of the roman value given by <i>s</i> or 0 if <i>s</i> is not in the table
*/
public int getInt(final String s) {
int res = 0;
for (String str : keys) {
if (str.equals(s)) {
int index = keys.indexOf(str);
res = values.get(index);
}
}
return res;
}
/**
* Converts integer value into roman value
* @param i the integer to convert
* @return a string representing the roman value
*/
public String toRoman(final int i) {
int cpt = i;
String res = "";
while (cpt > 0) {
for (int j : values) {
if ((cpt/j) >= 1) {
int index = values.indexOf(j);
res += keys.get(index);
cpt -= j;
break;
}
}
}
return res;
}
/**
* Calculates the result of the operation determined by the two operands and the operator
* @param x operand 1
* @param y operand 2
* @param op operator
* @return the result
*/
public String calc (final String x, final String y, final char op) {
if (this.isOk(x) && this.isOk(y)) {
int x1 = this.toInt(x);
int y1 = this.toInt(y);
switch (op) {
case '+' :
return this.toRoman(x1 + y1);
case '-' :
return this.toRoman(x1 - y1);
case '*' :
return this.toRoman(x1 * y1);
case '/' :
return this.toRoman(x1 / y1);
default :
break;
}
}
return null;
}
/**
* Says if the roman value given by <i>s</i> respect the roman value syntax
* @param s the roman value to verify
* @return true if <i>s</i> is correctly formed according to the roman value syntax (ie IV for 4 and not IIII), false else
*/
public Boolean isOk(final String s) {
int i = this.toInt(s);
String str = this.toRoman(i);
return s.equals(str);
}
}