Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6.  
  7. double str_to_num(const char* str1, int base1);
  8. char* num_to_str(double, int base);
  9. int char_to_num(int num, int base);
  10. int num_to_char(int num);
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14. char* str1;
  15. int base1;
  16. int base2;
  17.  
  18. if (argc != 4) {
  19. printf("bad input\n");
  20. return -1;
  21. }
  22. else {
  23. str1 = argv[1];
  24. base1 = atoi(argv[2]);
  25. base2 = atoi(argv[3]);
  26. }
  27.  
  28. double x;
  29.  
  30. x = str_to_num(str1, base1);
  31. char* result = num_to_str(x, base2);
  32. printf("%s\n", result);
  33. free(result);
  34. return 0;
  35. }
  36.  
  37. void reverse(char* array, int n) {
  38. char temp;
  39. for(int j = 0; j<n/2;j++){
  40. temp = array[j];
  41. array[j] = array[n-j-1];
  42. array[n-j-1] = temp;
  43. }
  44. /*
  45. int j = 0;
  46. while(n/2>0){
  47. temp = array[j];
  48. array[j] = array[n-j-1];
  49. array[n-j-1] = temp;
  50. }
  51. */
  52. /*
  53. while (n/2 > 0) {
  54. temp = array[0];
  55. array[0] = array[n-1];
  56. array[n-1] = temp;
  57. n--;
  58. }
  59. */
  60. }
  61.  
  62. int char_to_num(int num, int base){
  63. int c;
  64. if (isdigit(num)){
  65. c = num - '0';
  66. }
  67. else{
  68. num = tolower(num);
  69. c = num - 'a' + 10;
  70. }
  71.  
  72. if (!(c >= 0 && c < base)) {
  73. printf("bad input\n");
  74. exit(1);
  75. }
  76. return c;
  77. }
  78.  
  79. int num_to_char(int num){
  80. return (num < 10) ? num + '0' : num + 'A' - 10;
  81. }
  82.  
  83. double str_to_num(const char* str1, int base1){
  84. double x = 0;
  85. int power_base = -1;
  86.  
  87. while (str1[power_base + 1] != 0 && str1[power_base + 1] != '.')
  88. power_base++;
  89.  
  90. int j = 0;
  91.  
  92. for ( ; str1[j] != '\0'; j++) {
  93. if (str1[j] == '.')
  94. j++;
  95.  
  96. int digit = char_to_num(str1[j], base1);
  97. x += digit * pow(base1, power_base);
  98. power_base--;
  99. }
  100. return x;
  101. }
  102.  
  103. char* num_to_str(double c, int base2){
  104. long long whole = (long long)c;
  105. char* result = (char*)malloc(128);
  106. int i = 0;
  107. double fraction = c - whole;
  108. do {
  109. result[i++] = num_to_char(whole % base2);
  110. whole /= base2;
  111. } while (whole > 0);
  112.  
  113. //printf("%s\n", result);
  114. int wholeNumOfDigits = i;
  115. reverse(result, i); /*tyt ostanovilsya*/
  116.  
  117.  
  118.  
  119. if (fraction > 0) {
  120. result[i++] = '.';
  121. do {
  122. fraction *= base2;
  123. result[i++/*jopa*/] = num_to_char((int)fraction);
  124. fraction -= (int)fraction;
  125. } while (i-wholeNumOfDigits < 13 && fraction > 0);
  126. }
  127.  
  128. result[i] = 0;
  129.  
  130. return result;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement