Guest User

Untitled

a guest
Nov 16th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. long long int NtoTen (char value[], int n, int z, long long int sum, int ctr);
  5. void TentoM (long long int sum, int m);
  6. int toNum (char a);
  7.  
  8.  
  9. int main ()
  10. {
  11. int c;
  12. char value[100], ans[100];
  13. int n;
  14. int m;
  15. int i;
  16. int z;
  17. int ctr=0;
  18. long long int sum=0;
  19.  
  20. do {
  21. printf ("Number of cases: ");
  22. scanf ("%d", &c);
  23. }
  24. while (c<1);
  25.  
  26. for (i=0; i<c; i++)
  27. {
  28. printf ("Original base: ");
  29. scanf ("%d", &n);
  30. printf ("New base: ");
  31. scanf ("%d", &m);
  32. printf ("Input value: ");
  33. scanf ("%s", value);
  34.  
  35. z=strlen(value);
  36. //printf ("Base10 equivalent is %d.\n", NtoTen (value, n, z, sum, ctr));
  37. sum= NtoTen (value, n, z, sum, ctr);
  38. printf("sum:%lld\n", sum);
  39. printf("\nNew: ");
  40. TentoM (sum, m);
  41. printf("\n");
  42. }
  43. }
  44.  
  45. int toNum (char a)
  46. {
  47. switch (a)
  48. {
  49. case '0': return 0;
  50. case '1': return 1;
  51. case '2': return 2;
  52. case '3': return 3;
  53. case '4': return 4;
  54. case '5': return 5;
  55. case '6': return 6;
  56. case '7': return 7;
  57. case '8': return 8;
  58. case '9': return 9;
  59. case 'A': return 10;
  60. case 'B': return 11;
  61. case 'C': return 12;
  62. case 'D': return 13;
  63. case 'E': return 14;
  64. case 'F': return 15;
  65. case 'G': return 16;
  66. case 'H': return 17;
  67. case 'I': return 18;
  68. case 'J': return 19;
  69. case 'K': return 20;
  70. case 'L': return 21;
  71. case 'M': return 22;
  72. case 'N': return 23;
  73. case 'O': return 24;
  74. case 'P': return 25;
  75. case 'Q': return 26;
  76. case 'R': return 27;
  77. case 'S': return 28;
  78. case 'T': return 29;
  79. case 'U': return 30;
  80. case 'V': return 31;
  81. case 'W': return 32;
  82. case 'X': return 33;
  83. case 'Y': return 34;
  84. case 'Z': return 35;
  85. }
  86. }
  87.  
  88.  
  89. long long NtoTen (char value[], int n, int z, long long int sum, int ctr)
  90. {
  91. long long int dval;
  92. int g;
  93. long long int h=1;
  94.  
  95. if (value[ctr]!='\0')
  96. {
  97. for (g=1; g<z; g++)
  98. {
  99. h=h*n;
  100. }
  101. dval=toNum(value[ctr]);
  102. sum+=h*dval;
  103. //printf("%lld*%lld", dval, h);
  104. //printf("sum:%lld\n", sum);
  105. sum=NtoTen(value, n, z-1, sum, ctr+1);
  106. return sum;
  107. }
  108. else {return sum;}
  109. }
  110.  
  111. void TentoM (long long int sum, int m)
  112. {
  113. char str[36]={'0','1','2','3','4','5','6','7','8','9',
  114. 'A','B','C','D','E','F','G','H','I','J',
  115. 'K','L','M','N','O','P','Q','R','S','T',
  116. 'U','V','W','X','Y','Z'};
  117.  
  118. if (sum/m<=0)
  119. {
  120. printf ("%c", str[sum%m] );
  121. }
  122. else
  123. {
  124. TentoM (sum/m, m);
  125. printf ("%c", str[sum%m] );
  126. }
  127. }
Add Comment
Please, Sign In to add comment