Advertisement
Guest User

javac

a guest
Mar 29th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. public class RzymArab
  2. {
  3.  
  4. private static String[] rzymskie = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
  5. private static int[] arabskie = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  6.  
  7. private static boolean isInt(String line)
  8. {
  9. boolean isInt=true;
  10. try
  11. {
  12. Integer.parseInt(line);
  13. }
  14. catch(NumberFormatException ex)
  15. {
  16. isInt=false;
  17. }
  18. return isInt;
  19. }
  20.  
  21.  
  22.  
  23.  
  24. static boolean allCharactersSame(String rzym)
  25. {
  26. int n = rzym.length();
  27. for (int i = 1; i < n; i++)
  28. if (rzym.charAt(i) != 'X' && rzym.charAt(i) != 'M' && rzym.charAt(i) != 'C' && rzym.charAt(i) != 'D' && rzym.charAt(i) != 'L' && rzym.charAt(i) != 'V' && rzym.charAt(i) != 'I')
  29. return false;
  30.  
  31. return true;
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. static boolean aarabskie(String t)
  40. {
  41. int n = t.length();
  42. for (int i = 1; i < n; i++)
  43. if (t.charAt(i) != '0' && t.charAt(i) != '1' && t.charAt(i) != '2' && t.charAt(i) != '3' && t.charAt(i) != '4' && t.charAt(i) != '5' && t.charAt(i) != '6' && t.charAt(i) != '7' && t.charAt(i) != '8' && t.charAt(i) != '9')
  44. return false;
  45.  
  46. return true;
  47. }
  48.  
  49. public static int rzym2arab (String rzym) throws RzymArabException
  50. {
  51. int parametr=0;
  52. int parametr2=0;
  53. int tmp=0;
  54. int counter=0;
  55. int end=0;
  56. int kek=0;
  57. boolean lol=allCharactersSame(rzym);
  58.  
  59. int j=0;
  60.  
  61. int nameLength = rzym.length();
  62. int[] array=new int[nameLength];
  63. while(j<nameLength){
  64. j=j+1;
  65. //System.out.println(j+"j");
  66. }
  67. if(lol==false){
  68. kek=kek+1;
  69. }
  70.  
  71. for(int i=0; i<rzymskie.length; i++)
  72. {
  73. while(rzym.startsWith(rzymskie[i], tmp))
  74. {
  75. parametr=parametr+arabskie[i];
  76. tmp=tmp+rzymskie[i].length();
  77. parametr2=j-1;
  78. }
  79. }
  80.  
  81. if( parametr>3999 || kek>0 ){
  82. throw new RzymArabException("Nieprawidłowa dana"+nameLength);
  83. }
  84. if( parametr2>=parametr){
  85. throw new RzymArabException("Nieprawidłowa dana");
  86. }
  87. return parametr;
  88. }
  89.  
  90. public static String arab2rzym (int arab,int sumuj) throws RzymArabException
  91. {
  92.  
  93. if(arab<1 || arab>3999 || sumuj>0)
  94. throw new RzymArabException("Nieprawidłowa dana");
  95.  
  96. StringBuilder parametr = new StringBuilder("");
  97. for(int i=0; i<arabskie.length; ++i)
  98. {
  99. while(arab>=arabskie[i])
  100. {
  101. parametr.append(rzymskie[i]);
  102. arab=arab-arabskie[i];
  103. }
  104. }
  105.  
  106. return parametr.toString();
  107. }
  108.  
  109. public static void main(String[] args)
  110. {
  111. for(String t: args)
  112. {
  113. try
  114. {
  115. if(isInt(t))
  116. {
  117. int n=Integer.parseInt(t);
  118. boolean element=aarabskie(t);
  119. int sumuj=0;
  120. if(element==false){
  121. sumuj=sumuj+1;
  122. }
  123. System.out.println(arab2rzym(n,sumuj));
  124. }
  125. else
  126. {
  127. System.out.println(rzym2arab(t));
  128. }
  129. }
  130. catch(RzymArabException e)
  131. {
  132. System.out.println(e.getMessage());
  133. continue;
  134. }
  135. }
  136. }
  137.  
  138. }
  139.  
  140. class RzymArabException extends Exception
  141. {
  142. public RzymArabException(String message)
  143. {
  144. super(message);
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement