fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices.ComTypes;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace LabIPZ_2
  9. {
  10. internal class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. Console.WriteLine("Введення номеру завдання:" +
  15. "\n1. Переведення дробових чисел з двiйкового в десятковий вигляд;" +
  16. "\n2. Перетворення цiлих чисел з прямого коду в додатковий при довжинi розрядної сiтки 24.");
  17.  
  18. Console.Write("Номер завдання: ");
  19. int numOfTask = int.Parse(Console.ReadLine());
  20. switch (numOfTask)
  21. {
  22. case 1:
  23. ConvertToDecimal();
  24. break;
  25. case 2:
  26. BuildAdditionalCode();
  27. break;
  28. default:
  29. Console.WriteLine("Завдання з таким номером не існує.");
  30. break;
  31. }
  32. }
  33. static void ConvertToDecimal()
  34. {
  35. Console.WriteLine("Введення дробової частини числа в бiнарному виглядi:");
  36. int fractionalPartOfNum = int.Parse(Console.ReadLine());
  37. int pow = -(CountNumberOfDigits(fractionalPartOfNum));
  38. double fractionlPartInDecimal = 0;
  39.  
  40. while(pow <= -1)
  41. {
  42. fractionlPartInDecimal += (fractionalPartOfNum % 10) * Math.Pow(2, pow);
  43. fractionalPartOfNum /= 10;
  44. pow++;
  45. }
  46. Console.WriteLine($"Число в десятковому виглядi: {fractionlPartInDecimal}");
  47. }
  48. static int CountNumberOfDigits(long number)
  49. {
  50. long moduleNumber = Math.Abs(number);
  51. int count = 0;
  52. while (moduleNumber != 0)
  53. {
  54. count++;
  55. moduleNumber /= 10;
  56. }
  57. return count;
  58. }
  59. static void BuildAdditionalCode()
  60. {
  61. Console.WriteLine("Введення цiлого бiнарного числа.");
  62. string binaryNumber = Console.ReadLine();
  63.  
  64. string directCode = "";
  65. if (binaryNumber[0] == '-')
  66. {
  67. directCode = 1 + "|" + BuildDirectCodeWithoutSeniority(binaryNumber);
  68. }
  69. else
  70. {
  71. directCode = 0 + "|" + BuildDirectCodeWithoutSeniority(binaryNumber);
  72. }
  73.  
  74. Console.WriteLine($"Прямий код з довжиною розрядної сiтки 24: \n{directCode}");
  75. if (binaryNumber[0] != '-')
  76. {
  77. Console.WriteLine("Число додатнє, отже додатковий код дорiвнює прямому коду. \nТобто додатковий код:");
  78. Console.WriteLine(directCode);
  79. }
  80. else
  81. {
  82. string directCodeWithoutSeniority = BuildDirectCodeWithoutSeniority(binaryNumber);
  83. string reverseCodeWithoutSeniority = BuildReverseCodeWithoutSeniority(binaryNumber, directCodeWithoutSeniority);
  84. Console.WriteLine($"Зворотний код: \n{1 + "|" + reverseCodeWithoutSeniority}");
  85. string additionalCode = AddingAUnit(reverseCodeWithoutSeniority);
  86. Console.WriteLine($"Додатковий код: \n{1 + "|" + additionalCode}");
  87. }
  88. }
  89. static string BuildDirectCodeWithoutSeniority(string binaryNumber)
  90. {
  91. int sizeOfNumber = binaryNumber.Length;
  92. string newString = binaryNumber;
  93. if (binaryNumber[0] == '-')
  94. {
  95. newString = binaryNumber.Remove(0, 1);
  96. }
  97. while (sizeOfNumber != 23)
  98. {
  99. newString = 0 + newString;
  100. sizeOfNumber++;
  101. }
  102.  
  103. return newString;
  104. }
  105. static string BuildReverseCodeWithoutSeniority(string binaryNumber, string directCodeWithoutSeniority)
  106. {
  107. string reverseCode = "";
  108. if (binaryNumber[0] != '-')
  109. {
  110. reverseCode = directCodeWithoutSeniority;
  111. }
  112. else
  113. {
  114. foreach (char x in directCodeWithoutSeniority)
  115. {
  116. if(x == '0')
  117. {
  118. reverseCode += 1;
  119. }
  120. else
  121. {
  122. reverseCode += 0;
  123. }
  124. }
  125. }
  126. return reverseCode;
  127. }
  128. static string AddingAUnit(string reverseCodeWithoutSeniority)
  129. {
  130. string reverseCodeWithUnit = "";
  131.  
  132. bool elementAdded = false;
  133. for (int i = reverseCodeWithoutSeniority.Length - 1; i >= 0; i--)
  134. {
  135. if (reverseCodeWithoutSeniority[i] == '0')
  136. {
  137. reverseCodeWithUnit = 1 + reverseCodeWithUnit;
  138. elementAdded = true;
  139. break;
  140. }
  141. else
  142. {
  143. reverseCodeWithUnit = reverseCodeWithoutSeniority[i] + reverseCodeWithUnit;
  144. }
  145. }
  146. for (int i = reverseCodeWithoutSeniority.Length - reverseCodeWithUnit.Length - 1; i >= 0; i--)
  147. {
  148. reverseCodeWithUnit = reverseCodeWithoutSeniority[i] + reverseCodeWithUnit;
  149. }
  150. if (!elementAdded)
  151. {
  152. reverseCodeWithUnit = 1 + reverseCodeWithUnit;
  153. }
  154. return reverseCodeWithUnit;
  155. }
  156. }
  157. }
  158.  
Success #stdin #stdout 0.05s 31052KB
stdin
1
10100101
stdout
Введення номеру завдання:
1. Переведення дробових чисел з двiйкового в десятковий вигляд;
2. Перетворення цiлих чисел з прямого коду в додатковий при довжинi розрядної сiтки 24.
Номер завдання: Введення дробової частини числа в бiнарному виглядi:
Число в десятковому виглядi: 0.64453125