Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. int flagOfWrongInput = 0;
  5. int counterOfDots = 0, positionOfDot = -1;
  6. int lengthOfNumber = 0;
  7.  
  8. void getNumber(char str[], int b1);
  9. void calculateInOtherSystemWhole(long int part, int power);
  10. void calculateInOtherSystemFraction(double fraction, int power);
  11.  
  12. void getNumber(char str[], int b1) {
  13. for (int i = 0; i < 13; i++) {
  14. str[i] = 0;
  15. }
  16. char currentChar = 0;
  17. int currentPosition = 0;
  18. scanf("%c", &currentChar);
  19. while (scanf("%c", &currentChar) && currentChar != '\n') {
  20. if (currentChar == '.') {
  21. if (counterOfDots == 1) {
  22. flagOfWrongInput++;
  23. return;
  24. } else {
  25. counterOfDots++;
  26. positionOfDot = currentPosition;
  27. }
  28. } else if (!isdigit(currentChar) && !((toupper(currentChar) >= 'A') && (toupper(currentChar) <= 'F'))) {
  29. flagOfWrongInput++;
  30. return;
  31. } else {
  32. if (b1 <= 10) {
  33. if (currentChar - '0' < b1) {
  34. str[currentPosition] = currentChar;
  35. } else {
  36. flagOfWrongInput++;
  37. return;
  38. }
  39. } else {
  40. if (toupper(currentChar) <= 'A' + b1 - 10) {
  41. str[currentPosition] = (char) toupper(currentChar);
  42. } else {
  43. flagOfWrongInput++;
  44. return;
  45. }
  46. }
  47. }
  48. currentPosition++;
  49. }
  50. lengthOfNumber = currentPosition;
  51. if (positionOfDot == 0 || positionOfDot == currentPosition - 1) {
  52. flagOfWrongInput++;
  53. }
  54. }
  55. void calculateInOtherSystemWhole(long int part, int power) {
  56. long int currentValue = power;
  57. int degree = 1;
  58. while (currentValue < part) {
  59. currentValue *= power;
  60. degree++;
  61. }
  62. if (currentValue != part) {
  63. currentValue /= power;
  64. degree--;
  65. while (degree >= 0) {
  66. if (power <= 10) {
  67. printf("%li", part / currentValue);
  68. } else {
  69. if (part / currentValue < 10) {
  70. printf("%li", part / currentValue);
  71. } else {
  72. printf("%c", (char)('A' + (part / currentValue) - 10));
  73. }
  74. }
  75. part = part - ((part / currentValue) * currentValue);
  76. currentValue /= power;
  77. degree--;
  78. }
  79. } else {
  80. printf("%d", 1);
  81. for (int i = 0; i < degree; i++) {
  82. printf("%d", 0);
  83. }
  84. }
  85. }
  86.  
  87. void calculateInOtherSystemFraction(double fraction, int power) {
  88. double currentFraction = 1;
  89. int degree = 1;
  90. while (degree <= 12 && fraction != 0) {
  91. currentFraction = currentFraction / (double)power;
  92. if (fraction / currentFraction < 10) {
  93. printf("%d", (int)(fraction / currentFraction));
  94. } else {
  95. printf("%c", (char)('A' + (int)(fraction / currentFraction) - 10));
  96. }
  97. fraction = fraction - (double)((int)(fraction / currentFraction)) * currentFraction;
  98. degree++;
  99. }
  100. }
  101.  
  102. int main() {
  103. int b1, b2;
  104. long int whole = 0;
  105. double fraction = 0;
  106. scanf("%d%d", &b1, &b2);
  107. if (b1 >= 2 && b1 <= 16 && b2 >= 2 && b2 <= 16) {
  108. char str[13];
  109. getNumber(str, b1);
  110. if (flagOfWrongInput == 0) {
  111. if (positionOfDot == -1) {
  112. for (int i = 0; i < lengthOfNumber; i++) {
  113. if (isdigit(str[i])) {
  114. whole = whole * b1 + str[i] - '0';
  115. } else {
  116. whole = whole * b1 + str[i] - 'A' + 10;
  117. }
  118. }
  119. if (b2 != 10) {
  120. calculateInOtherSystemWhole(whole, b2);
  121. } else {
  122. printf("%d", whole);
  123. }
  124. } else {
  125. for (int i = 0; i < positionOfDot; i++) {
  126. if (isdigit(str[i])) {
  127. whole = whole * b1 + str[i] - '0';
  128. } else {
  129. whole = whole * b1 + str[i] - 'A' + 10;
  130. }
  131. }
  132. int currentValue = b1;
  133. for (int i = positionOfDot + 1; i < 13 && str[i] != '\0'; i++) {
  134. if (isdigit(str[i])) {
  135. fraction += (double)(str[i] - '0') / currentValue;
  136. } else {
  137. fraction += (double)(str[i] - 'A' + 10) / currentValue;
  138. }
  139. currentValue *= b1;
  140. }
  141. if (b2 != 10) {
  142. calculateInOtherSystemWhole(whole, b2);
  143. if (fraction != 0) {
  144. printf("%c", '.');
  145. calculateInOtherSystemFraction(fraction, b2);
  146. }
  147. } else {
  148. printf("%f", whole + fraction);
  149. }
  150. }
  151. }
  152. } else {
  153. flagOfWrongInput++;
  154. }
  155. if (flagOfWrongInput > 0) {
  156. printf("Bad input\n");
  157. }
  158. return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement