Advertisement
Farjana_akter

Untitled

Sep 3rd, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll;
  4.  
  5.  
  6. string con(string s,int base1,int base2)
  7. {
  8. string news,ans;
  9. ll i,j,x=0,k,len=s.size(),a,jog=0,power=1,rem;
  10. for(i=len-1; i>=0; i--)
  11. {
  12. if(s[i]>='A' && s[i]<='F')
  13. a=(s[i]-'A')+10;
  14. else
  15. a=(s[i]-'0');
  16.  
  17. jog+=(a*power);
  18. power*=base1;
  19. }
  20. while(jog)
  21. {
  22. rem=jog%base2;
  23. if(rem>9)
  24. {
  25. /*
  26. if(rem==10)
  27. ans+='A';
  28. else if(rem==11)
  29. ans+='B';
  30. else if(rem==12)
  31. ans+='C';
  32. else if(rem==13)
  33. ans+='D';
  34. else if(rem==14)
  35. ans+='E';
  36. else if(rem==15)
  37. ans+='F';*/
  38. ans+=(rem-10+'A');
  39. }
  40. else
  41. ans+=(rem+'0');
  42. jog/=base2;
  43. }
  44. reverse(ans.begin(),ans.end());
  45. return ans;
  46. }
  47.  
  48.  
  49.  
  50. bool check(string s, int base)
  51. {
  52. int i,j,k,a;
  53. for(i=0; i<s.size(); i++)
  54. {
  55. if(s[i]>='A' && s[i]<='Z')
  56. a=s[i]-'A'+10;
  57. else
  58. a=s[i]-'0';
  59. if(a>=base)
  60. return false;
  61. }
  62. return true;
  63. }
  64.  
  65. int main()
  66. {
  67. string s;
  68. int base1,base2,i,j,k;
  69. while(cin>>base1>>base2>>s)
  70. {
  71. if(check(s,base1)==true)
  72. {
  73. if(s[0]=='0')
  74. cout<<s<<" base "<<base1<<" = "<<0<<" base "<<base2<<endl;
  75. else
  76. {
  77. string ans=con(s,base1,base2);
  78. cout<<s<<" base "<<base1<<" = "<<ans<<" base "<<base2<<endl;
  79. }
  80. }
  81. else
  82. {
  83. cout<<s<<" is an illegal base "<<base1<<" number"<<endl;
  84. }
  85. }
  86. return 0;
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement