public class Convertor {
public String convertor(String nr)
{
nr.trim();
if(IsArabic(nr)) return convert(nr);
else return Long.toString(getNumber(nr));
}
private boolean IsArabic(String nr){
String ValidChars = "0123456789";
boolean IsNumber=true;
char ch;
for (int i = 0; i < nr.length() && IsNumber == true; i++){
ch = nr.charAt(i);
if (ValidChars.indexOf(ch) == -1){
IsNumber = false;
}
}
return IsNumber;
}
public String convert(String nr) {
String conversie = "";
try {
long number = Long.parseLong(nr);
if (number > 0 && number <= 3999) {
conversie += getThousands(number);
conversie += getHundreds(number);
conversie += getDigits(number);
conversie += getUnits(number);
}
if (number > 3999 && number <= 3999999) {
conversie += "(";
long aNumber = number / 1000;
conversie += getThousands(aNumber);
conversie += getHundreds(aNumber);
conversie += getDigits(aNumber);
conversie += getUnits(aNumber);
conversie += ")";
long bNumber = number % 1000;
conversie += getThousands(bNumber);
conversie += getHundreds(bNumber);
conversie += getDigits(bNumber);
conversie += getUnits(bNumber);
}
if (number > 3999999 && number <= 3999999999L) {
conversie += "[";
long aNumber = number / 1000000;
conversie += getThousands(aNumber);
conversie += getHundreds(aNumber);
conversie += getDigits(aNumber);
conversie += getUnits(aNumber);
conversie += "](";
long bNumber = (number % 1000000) / 1000;
conversie += getThousands(bNumber);
conversie += getHundreds(bNumber);
conversie += getDigits(bNumber);
conversie += getUnits(bNumber);
conversie += ")";
long cNumber = number % 1000;
conversie += getThousands(cNumber);
conversie += getHundreds(cNumber);
conversie += getDigits(cNumber);
conversie += getUnits(cNumber);
}
} catch (Exception e) {
if(nr.contains("["))
{
long a=getNumber(nr.substring(nr.indexOf("[")+1, nr.indexOf("]")));
long b=getNumber(nr.substring(nr.indexOf("(")+1, nr.indexOf(")")));
long c=getNumber(nr.substring(nr.indexOf(")")+1, nr.length()));
long result=a*1000000+b*1000+c;
conversie=Long.toString(result);
}
else if(nr.contains("("))
{
long a=getNumber(nr.substring(nr.indexOf("(")+1, nr.indexOf(")")));
long b=getNumber(nr.substring(nr.indexOf(")")+1, nr.length()));
long result=a*1000+b;
conversie=Long.toString(result);
}
else
{
long a=getNumber(nr);
long result=a;
conversie=Long.toString(result);
}
}
return conversie;
}
private long getNumber(String num)
{
int i=0,h=0,t=0,u=0;
long n=0;
num=num.toUpperCase();
if(num.length()>1){
while(num.charAt(i)=='M') {i++;}
n=i*1000;
if(num.substring(i,i+1).contentEquals("D")) {h=5;i++;}
else if(num.substring(i,i+2).contentEquals("CM")) {h=9;i+=2;}
else if(num.substring(i,i+2).contentEquals("CD")) {h=4;i+=2;}
else if(h==0 || h==5) {while (i<num.length() && num.charAt(i)=='C'){h++;i++;}}
n+=h*100;
if(num.substring(i,i+1).contentEquals("L")) {t=5;i++;}
else if(num.substring(i,i+2).contentEquals("XC")) {t=9;i+=2;}
else if(num.substring(i,i+2).contentEquals("XL")) {t=4;i+=2;}
else if(t==0 || t==5) {while (i<num.length() && num.charAt(i)=='X'){t++;i++;}}
n+=t*10;
if(num.substring(i,i+1).contentEquals("V")) {u=5;i++;}
else if(num.substring(i,i+2).contentEquals("IX")) {u=9;i+=2;}
else if(num.substring(i,i+2).contentEquals("IV")) {u=4;i+=2;}
else if(u==0 || u==5) {while (i<num.length() && num.charAt(i)=='I'){u++;i++;}}
n+=u;
return n;
}
if(num.equals("I")) return 1;
if(num.equals("V")) return 5;
if(num.equals("X")) return 10;
if(num.equals("L")) return 50;
if(num.equals("C")) return 100;
if(num.equals("D")) return 500;
if(num.equals("M")) return 1000;
return 0;
}
private String getThousands(long number) {
return "MMM".substring(0, (int) number / 1000);
}
private String getHundreds(long number) {
String roman = "";
number = (number % 1000) / 100;
if (number == 9) {
roman += "CM";
} else if (number > 4) {
roman += "DCCC".substring(0, (int) number - 4);
} else if (number == 4) {
roman += "CD";
} else {
roman += "CCC".substring(0, (int) number);
}
return roman;
}
private String getDigits(long number) {
String roman = "";
number = (number % 100) / 10;
if (number == 9) {
roman += "XC";
} else if (number > 4) {
roman += "LXXX".substring(0, (int) number - 4);
} else if (number == 4) {
roman += "XL";
} else {
roman += "XXX".substring(0, (int) number);
}
return roman;
}
private String getUnits(long number) {
String roman = "";
number = (number % 10);
if (number == 9) {
roman += "IX";
} else if (number > 4) {
roman += "VIII".substring(0, (int) number - 4);
} else if (number == 4) {
roman += "IV";
} else {
roman += "III".substring(0, (int) number);
}
return roman;
}
}
评论0