2019-06-17 16:47:17 +00:00
|
|
|
import static utils.Utils.*;
|
|
|
|
|
|
|
|
public class Romain {
|
|
|
|
|
|
|
|
public static final void main(String[] args) {
|
|
|
|
afficher("Conversion nombre romains (I à X)");
|
|
|
|
|
|
|
|
testRomains(Romain::convertirEnDecimal);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int convertirEnDecimal(char[] romain) {
|
2019-06-17 16:54:20 +00:00
|
|
|
int cur = 0;
|
|
|
|
int prec = 0;
|
|
|
|
int dec = 0;
|
|
|
|
for (int i = 0; i < romain.length; i++) {
|
|
|
|
prec = cur;
|
|
|
|
cur = asInt(romain[i]);
|
|
|
|
if (prec<cur) {
|
|
|
|
dec -= prec;
|
|
|
|
} else {
|
|
|
|
dec += prec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dec+=cur;
|
|
|
|
return dec;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int asInt(char c) {
|
|
|
|
switch (c) {
|
|
|
|
case 'I':
|
|
|
|
return 1;
|
|
|
|
case 'V':
|
|
|
|
return 5;
|
|
|
|
case 'X':
|
|
|
|
return 10;
|
|
|
|
}
|
2019-06-17 16:47:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|