Guest User

Untitled

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