-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslator.java
More file actions
71 lines (63 loc) · 2.63 KB
/
Translator.java
File metadata and controls
71 lines (63 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package sample;
public class Translator {
public static void main(String[] args) {
//Проверка работы
printSolveOfSum("X + X");
printSolveOfSum("10 + 10");
}
static void printSolveOfSum(String equation) {
String romPat = "IVX";
if (romPat.indexOf(equation.charAt(0)) != -1)
System.out.println(getSolveRomanianNumsSum(equation.split("\\+")));
else System.out.println(getSolveArabicNumsSum(equation.split("\\+")));
}
private static int getSolveArabicNumsSum(String [] nums) {
return Integer.valueOf(nums[0].trim()) + Integer.valueOf(nums[1].trim());
}
private static String getSolveRomanianNumsSum(String [] nums) {
return arabToRoman(romanToArabic(nums[0].trim()) + romanToArabic(nums[1].trim()));
}
public static String arabToRoman(int num) {
String c[] = {"", "C"};
String x[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
String i[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
String hundereds = c[(num % 1000) / 100];
String tens = x[(num % 100) / 10];
String ones = i[num % 10];
return hundereds + tens + ones;
}
private static int value(char r) {
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
return -1;
}
public static int romanToArabic(String str) {
int res = 0;
for (int i = 0; i < str.length(); i++) {
int s1 = value(str.charAt(i));
if (s1 < 1) throw new IllegalStateException("Введенное значение не может быть меньше I" +(s1));
if (i + 1 < str.length()) {
int s2 = value(str.charAt(i + 1));
if (s2 < 1) throw new IllegalStateException("Введенное значение не может быть меньше I" +(s2));
if (s1 >= s2) {
res = res + s1;
} else {
res = res + s2 - s1;
i++;
}
} else {
res = res + s1;
i++;
}
}
return res;
}
}