Advertisement
Guest User

Untitled

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