41 lines
718 B
Java
41 lines
718 B
Java
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) {
|
|
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;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
}
|