Guest User

Untitled

a guest
Apr 25th, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. package com.company;
  2.  
  3. public class rimcalk
  4. {
  5.  
  6. static String[] Rome = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
  7. static int[] Arab = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
  8.  
  9.  
  10.  
  11. public static int Acalc(String opera, int n1, int n2)
  12. {
  13. int result = 0;
  14.  
  15.  
  16. //Write the calulator
  17. if (opera.equals("+")) {result=n1+n2;}
  18. if (opera.equals("-")) {result=n1-n2;}
  19. if (opera.equals("*")) {result=n1*n2;}
  20. if (opera.equals("/")) {result=n1/n2;}
  21.  
  22. System.out.println(result);
  23.  
  24. return result;
  25. }
  26.  
  27. public static String Roman(double input)
  28. {
  29.  
  30. String s = "";
  31.  
  32. if (input < 0)
  33. {
  34. //Convert to roman numerals
  35. s = "-" + s;
  36. input *= -1;
  37. }
  38.  
  39. else if (input == 0)
  40. return "nulla";
  41.  
  42. while (input >= 1000) {
  43. s += "M";
  44. input -= 1000;
  45. }
  46.  
  47. while (input >= 900) {
  48. s += "CM ";
  49. input -= 900;
  50. }
  51.  
  52. while (input >= 500) {
  53. s += "D";
  54. input -= 500;
  55. }
  56.  
  57. while (input >= 400) {
  58. s += "CD ";
  59. input -= 400;
  60. }
  61.  
  62.  
  63. while (input >= 100) {
  64. s += "C";
  65. input -= 100;
  66. }
  67. while (input >= 90) {
  68. s += "XC";
  69. input -= 90;
  70. }
  71. while (input >= 50) {
  72. s += "L";
  73. input -= 50;
  74. }
  75. while (input >= 40) {
  76. s += "XL";
  77. input -= 40;
  78. }
  79. while (input >= 10) {
  80. s += "X";
  81. input -= 10;
  82. }
  83. while (input >= 9) {
  84. s += "IX";
  85. input -= 9;
  86. }
  87. while (input >= 5) {
  88. s += "V";
  89. input -= 5;
  90. }
  91. while (input >= 4) {
  92. s += "IV";
  93. input -= 4;
  94. }
  95. while (input >= 1) {
  96. s += "I";
  97. input -= 1;
  98. }
  99. return s;
  100. }
  101.  
  102.  
  103. //arabic
  104. public static int RometoArab(String rome)
  105. {
  106.  
  107. StringBuffer romeNumber = new StringBuffer(rome);
  108. int arabNumber = 0, i = 0;
  109. // Проверяем переданную строку на наличие символов
  110. if (romeNumber.length() > 0) {
  111. while (true) {
  112. do {
  113.  
  114. if (Rome[i].length() <= romeNumber.length()) {
  115. // Выделяем из строки подстроку и сравниваем со
  116. // значением из массива Arab
  117. if (Rome[i].equals(romeNumber.substring(0,
  118. Rome[i].length()))) {
  119. // После чего прибавляем число соответствующее
  120. // индексу элемента римской цифры из массива Arab
  121. arabNumber += Arab[i];
  122. // и удаляем из строки римскую цифру
  123. romeNumber.delete(0, Rome[i].length());
  124. if (romeNumber.length() == 0)
  125. return arabNumber;
  126. } else
  127.  
  128. break;
  129. } else
  130.  
  131. break;
  132. } while (true && romeNumber.length() != 0);
  133. i++;
  134. }
  135. }
  136. return 0;
  137.  
  138. }
  139.  
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment