Advertisement
a53

Base Converter

a53
Feb 26th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. string str,nb;
  4. int b,c;
  5. char cn[10001];
  6.  
  7. char * Convert(int from,int to,const char * s,char * out)
  8. {
  9. if(s==NULL)
  10. return NULL;
  11. if(from<2||from>36||to<2||to>36)
  12. return NULL;
  13. int il=strlen(s);
  14. int *fs=new int[il];
  15. int k=0;
  16. int i,j;
  17. for(i=il-1;i>=0;--i)
  18. {
  19. if(s[i]>='0'&&s[i]<='9')
  20. {
  21. fs[k]=(int)(s[i]-'0');
  22. }
  23. else
  24. {
  25. if(s[i]>='A'&&s[i]<='Z')
  26. {
  27. fs[k]=10+(int)(s[i]-'A');
  28. }
  29. else if(s[i]>='a'&&s[i]<='z')
  30. {
  31. fs[k]=10+(int)(s[i]-'a');
  32. }
  33. else
  34. {
  35. delete[]fs;
  36. return NULL;
  37. }
  38. }
  39. ++k;
  40. }
  41. for(i=0;i<il;++i)
  42. {
  43. if(fs[i]>=from )
  44. return NULL;
  45. }
  46. double x=ceil(log(from )/log (to));
  47. int ol=1+(il*x);
  48. int * ts=new int[ol];
  49. int * cums=new int [ol];
  50. for (i=0;i<ol;i++)
  51. {
  52. ts[i]=0;
  53. cums[i]=0;
  54. }
  55. ts[0]=1;
  56. for(i =0;i<il;++i)
  57. {
  58. for(j=0;j<ol;++j)
  59. {
  60. cums[j]+=ts[j]*fs[i];
  61. int temp=cums[j],rem=0,ip=j;
  62. do
  63. {
  64. rem=temp/to;
  65. cums[ip]=temp-rem*to;
  66. ++ip;
  67. if(ip>=ol)
  68. {
  69. if(rem>0)
  70. {
  71. delete[]ts;
  72. delete[]cums;
  73. delete[]fs;
  74. return NULL;
  75. }
  76. break;
  77. }
  78. cums[ip]+=rem;
  79. temp=cums[ip];
  80. }
  81. while (temp>=to);
  82. }
  83. for(j=0;j<ol;++j)
  84. ts[j]=ts[j]*from;
  85. for(j=0;j<ol;++j)
  86. {
  87. int temp=ts[j],rem=0,ip=j;
  88. do
  89. {
  90. rem=temp/to;
  91. ts[ip]=temp-rem*to;
  92. ++ip;
  93. if (ip >= ol)
  94. {
  95. if (rem>0)
  96. {
  97. delete[]ts;
  98. delete[]cums;
  99. delete[]fs;
  100. return NULL;
  101. }
  102. break;
  103. }
  104. ts[ip]+=rem;
  105. temp = ts[ip];
  106. }
  107. while(temp>=to);
  108. }
  109. }
  110. if(out==NULL )
  111. out=(char*)malloc(sizeof(char)*(ol+1));
  112. int spos=0;
  113. bool first=false;
  114. for(i=ol-1;i>=0;--i)
  115. {
  116. if (cums[i]!=0)
  117. first=true;
  118. if (!first)
  119. continue;
  120. if (cums[i]<10)
  121. out[spos]=(char)(cums[i]+'0');
  122. else
  123. out[spos]=(char)(cums[i]+'A'-10);
  124. ++spos;
  125. }
  126. out[spos]=0;
  127. delete[]ts;
  128. delete[]cums;
  129. delete[]fs;
  130.  
  131. return out;
  132. }
  133.  
  134. int main()
  135. {
  136. ifstream f("base_converter.in");
  137. getline(f,str);
  138. f.close();
  139. vector<string>x;
  140. istringstream buffer(str);
  141. for(string w;buffer>>w;)
  142. x.push_back(w);
  143. for(unsigned int j=0;j<x.size();++j)
  144. switch(j)
  145. {
  146. case 0: nb=x[j];break;
  147. case 1: b=stoi(x[j]);break;
  148. case 2: c=stoi(x[j]);break;
  149. }
  150. int lnb=nb.length();
  151. char char_array[lnb+1];
  152. strcpy(char_array,nb.c_str());
  153. Convert(b,c,char_array,cn);
  154. ofstream g("base_converter.out");
  155. g<<cn;
  156. g.close();
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement