BetinaUKTC

моп2

Nov 15th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.03 KB | None | 0 0
  1. public class Utilities2 {
  2. public static String binToHex(String bin) {
  3. Hashtable<String, Character> htable = new Hashtable<>();
  4. htable.put("0000",'0');
  5. htable.put("0001",'1');
  6. htable.put("0010",'2');
  7. htable.put("0011",'3');
  8. htable.put("0100",'4');
  9. htable.put("0101",'5');
  10. htable.put("0110",'6');
  11. htable.put("0111",'7');
  12. htable.put("1000",'8');
  13. htable.put("1001",'9');
  14. htable.put("1010",'A');
  15. htable.put("1011",'B');
  16. htable.put("1100",'C');
  17. htable.put("1101",'D');
  18. htable.put("1110",'E');
  19. htable.put("1111",'F');
  20.  
  21. String zeros = "";
  22. if(bin.length()%4 != 0) {
  23. for (int i = 0; i < 4-(bin.length()%4); i++) {
  24. zeros += "0";
  25. }
  26. }
  27.  
  28. String[] arr = (zeros+bin).split("");
  29. String hexNumber = "";
  30.  
  31. for (int i = 0; i < arr.length; i+=4) {
  32. String fourstr = arr[i] + arr[i+1] + arr[i+2] + arr[i+3];
  33. hexNumber += htable.get(fourstr);
  34. }
  35. return hexNumber;
  36. }
  37. public static String hexToBin(String hex) {
  38. char[] arr = hex.toCharArray();
  39. String[] strArr = {
  40. "0000", "0001", "0010", "0011",
  41. "0100", "0101", "0110", "0111",
  42. "1000", "1001", "1010", "1011",
  43. "1100", "1101", "1110", "1111"
  44. };
  45.  
  46. String number = "";
  47. String n;
  48.  
  49. for (int i = 0; i < arr.length; i++) {
  50. int num = arr[i] - '0';
  51. if(num >= 0 && num <= 10){
  52. n = strArr[num];
  53. }else{
  54. num = arr[i] - 'A' + 10;
  55. if(num >= 10 && num <= 15){
  56. n = strArr[num];
  57. }else{
  58. num = arr[i] - 'a' + 10;
  59. n = strArr[num];
  60. }
  61. }
  62. number += n;
  63. }
  64.  
  65. boolean bool = false;
  66. int k = 0;
  67. String str = "";
  68. String[] arraySum = number.split("");
  69.  
  70. //Махаме отпред нулите
  71. while(bool != true) {
  72. if (arraySum[k].equals("1")) {
  73. for (int j = k; j <= arraySum.length - 1; j++) {
  74. str += arraySum[j];
  75. }
  76. bool = true;
  77. }
  78. k++;
  79. }
  80. return str;
  81. }
  82. public static int hexToDecFix(String hex) {
  83. char[] arr = hex.toCharArray();
  84.  
  85. int power = 1;
  86. int sum = 0;
  87. int n;
  88.  
  89. for (int i = arr.length - 1; i >= 0; i--) {
  90. n = arr[i] - '0';
  91. if(!(n >= 0 && n < 10)){
  92. n = ((arr[i] - 'A') + 10);
  93. if(!(n >= 10 && n <= 15)){
  94. n = arr[i] - 'a' + 10;
  95. }
  96. }
  97.  
  98. n *= power;
  99. sum += n;
  100. power *= 16;
  101. }
  102. return sum;
  103. }
  104. public static String hexToDecRGB(String hex) {
  105. String[] mainArr = hex.split("");
  106. String str = "rgb(";
  107.  
  108. for (int i = 1; i < mainArr.length; i+=2) {
  109. str += hexToDecFix(mainArr[i] + mainArr[i+1]);
  110. if(i != 5){
  111. str += ", ";
  112. }
  113. }
  114. str += ")";
  115.  
  116. return str;
  117. }
  118. public static String decToHex(int dec) {
  119. Stack<Integer> st = new Stack<>();
  120.  
  121. while(dec != 0){
  122. int diff = dec%16;
  123. st.push(diff);
  124. dec = dec/16;
  125. }
  126.  
  127. String str = "";
  128. while(st.size() != 0){
  129. int digit = st.pop();
  130. if(digit < 10){
  131. str += (digit + "");
  132. }else{
  133. str += (char)(digit - 10 + 'A');
  134. }
  135. }
  136. return str;
  137. }
  138. public static int letterCounter(char[] arr,int count,int sum) {
  139. if(count == -1){
  140. return sum;
  141. }
  142. int digit = arr[count] - '0';
  143. if(digit >= 0 && digit < 10){
  144. return letterCounter(arr,count-1,sum);
  145. }
  146.  
  147. return letterCounter(arr,count-1,sum+=1);
  148. }
  149. }
  150. ////////////////////////////////////////////////////////////////////////////////////
  151. public class BinToHex {
  152. public static void main(String[] args) {
  153. Scanner scanner = new Scanner(System.in);
  154.  
  155. String bin = scanner.nextLine();
  156. System.out.println(Utilities2.binToHex(bin));
  157. }
  158. }
  159. ////////////////////////////////////////////////////////////////////////////////
  160. public class HexLettersCounter {
  161. public static void main(String[] args) {
  162. Scanner scanner = new Scanner(System.in);
  163.  
  164. int dec = scanner.nextInt();
  165. String str = Utilities2.decToHex(dec);
  166. char[] arr = str.toCharArray();
  167.  
  168. System.out.println(Utilities2.letterCounter(arr,arr.length-1,0));
  169. }
  170. }
  171. //////////////////////////////////////////////////////////////////////////
  172. public class HexToDec {
  173. public static void main(String[] args) {
  174. Scanner scanner = new Scanner(System.in);
  175.  
  176. String hex = scanner.nextLine();
  177. System.out.println(Utilities2.hexToDecFix(hex));
  178. }
  179. }
  180.  
  181. //////////////////////////////////////////////////////////////////////////
  182. public class HexToBin {
  183. public static void main(String[] args) {
  184. Scanner scanner = new Scanner(System.in);
  185.  
  186. String hex = scanner.nextLine();
  187. System.out.println(Utilities2.hexToBin(hex));
  188. }
  189. }
  190. //////////////////////////////////////////////////////////////////////////
  191.  
  192. public class romanToDecimal {
  193. public static int romanToInteger(String roman) {
  194. Map<Character,Integer> numbersMap = new HashMap<>();
  195. numbersMap.put('I',1);
  196. numbersMap.put('V',5);
  197. numbersMap.put('X',10);
  198. numbersMap.put('L',50);
  199. numbersMap.put('C',100);
  200. numbersMap.put('D',500);
  201. numbersMap.put('M',1000);
  202.  
  203. int result=0;
  204.  
  205. for(int i=0;i<roman.length();i++)
  206. {
  207. char ch = roman.charAt(i); // Current Roman Character
  208.  
  209. //Case 1
  210. if(i>0 && numbersMap.get(ch) > numbersMap.get(roman.charAt(i-1)))
  211. {
  212. result += numbersMap.get(ch) - 2*numbersMap.get(roman.charAt(i-1));
  213. }
  214.  
  215. // Case 2: just add the corresponding number to result.
  216. else
  217. result += numbersMap.get(ch);
  218. }
  219.  
  220. return result;
  221. }
  222.  
  223.  
  224. public static void main(String[] args) {
  225. // we take input as a String
  226. String romanNumber="MCMXCIV";
  227. int result = romanToInteger(romanNumber);
  228. System.out.println(romanNumber);
  229. System.out.println(result);
  230.  
  231.  
  232.  
  233. }
  234. }
  235. //////////////////////////////////////////////////////////////////
  236. public class decimalToRoman {
  237. public static void main(String[] args) {
  238. // initialize reader for reading decimal number
  239. Scanner reader = new Scanner(System.in);
  240. System.out.println("Enter a number");
  241. // read number
  242. int number = reader.nextInt();
  243. methodOne(number);
  244. // close reader
  245. reader.close();
  246. }
  247.  
  248. static void methodOne(int number) {
  249. // declare arrays for Roman numbers
  250. String[] thousands = { "", "M", "MM", "MMM" };
  251. String[] hundreds = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
  252. String[] tens = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
  253. String[] units = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X" };
  254. int numberOfThousands = number / 1000;
  255. int numberOfHundreds = (number / 100) % 10;
  256. int numberOfTens = (number / 10) % 10;
  257. int numberOfUnits = number % 10;
  258. String romanNumber = thousands[numberOfThousands] + hundreds[numberOfHundreds]
  259. + tens[numberOfTens] + units[numberOfUnits];
  260. System.out.println(romanNumber);
  261. }
  262. }
  263. ///////////////////////////////////////////////////////////////////////////
  264. public class eightToSixteen {
  265. public static void main(String[] input)
  266. {
  267. String octnum, hexnum;
  268. int decnum;
  269. Scanner scan = new Scanner(System.in);
  270.  
  271.  
  272. octnum = scan.nextLine();
  273.  
  274. decnum = Integer.parseInt(octnum, 8);
  275. hexnum = Integer.toHexString(decnum).toUpperCase();
  276.  
  277. System.out.println(hexnum);
  278. }
  279. }
  280. /////////////////////////////////////////////////////////
  281. public class sixteenToEight {
  282. public static void main(String[] args) {
  283. int decimalNumber, a = 1, b;
  284. int[] octalNumber = new int[100];
  285. Scanner sc = new Scanner(System.in);
  286. String strHexNumber = sc.nextLine();
  287. decimalNumber = hexToDecimal(strHexNumber);
  288. while (decimalNumber != 0) {
  289. octalNumber[a++] = decimalNumber % 8;
  290. decimalNumber = decimalNumber / 8;
  291. }
  292.  
  293. for (b = a - 1; b > 0; b--) {
  294. System.out.print(octalNumber[b]);
  295. }
  296. System.out.print("\n");
  297. sc.close();
  298. }
  299.  
  300. public static int hexToDecimal(String str) {
  301. String strDigits = "0123456789ABCDEF";
  302. str = str.toUpperCase();
  303. int value = 0;
  304. for (int a = 0; a < str.length(); a++) {
  305. char ch = str.charAt(a);
  306. int deci = strDigits.indexOf(ch);
  307. value = 16 * value + deci;
  308. }
  309. return value;
  310. }
  311. }
  312. ////////////////////////////////////////////////////////////////////
  313.  
  314. public class tenToThree {
  315. public static void convertToTernary(int N) {
  316. if (N == 0)
  317. return;
  318.  
  319.  
  320. int x = N % 3;
  321. N /= 3;
  322. if (x < 0)
  323. N += 1;
  324.  
  325.  
  326. convertToTernary(N);
  327.  
  328.  
  329. if (x < 0)
  330. System.out.print( x + (3 * -1));
  331. else
  332. System.out.print( x);
  333. }
  334.  
  335.  
  336. static void convert(int Decimal) {
  337. System.out.print("Ternary number of " +Decimal +" is: ");
  338.  
  339. if (Decimal != 0) {
  340. convertToTernary(Decimal);
  341. }
  342. else
  343. System.out.println("0" );
  344. }
  345.  
  346.  
  347. public static void main (String[] args) {
  348. int Decimal = 2747;
  349. convert(Decimal);
  350. }
  351. }
  352. //////////////////////////////////////////////////////
  353. public class threeToTen {
  354. public static void convertToDecimal(int N) {
  355. System.out.print("Decimal number of " +
  356. N + " is: ");
  357.  
  358. if (N != 0) {
  359. int decimalNumber = 0,
  360. i = 0, remainder;
  361.  
  362. while (N != 0) {
  363. remainder = N % 10;
  364. N /= 10;
  365.  
  366. decimalNumber += remainder *
  367. Math.pow(3, i);
  368. ++i;
  369. }
  370. System.out.print(decimalNumber + "\n");
  371. }
  372. else
  373. System.out.print("0" + "\n");
  374. }
  375.  
  376. // Driver code
  377. public static void main(String[] args) {
  378. int Ternary = 10202202;
  379. convertToDecimal(Ternary);
  380. }
  381. }
  382.  
  383.  
Advertisement
Add Comment
Please, Sign In to add comment