fork(1) download
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4. #include<string>
  5. #include<iostream>
  6.  
  7. using namespace std;
  8.  
  9. long int convert_base10(const string &num, int base)
  10. {
  11. int len, dig;
  12. long int result = 0;
  13. len = num.size();
  14. // printf("len = %d\n", len);
  15. // converting to base 10
  16. for(int i=0; i<len; i++)
  17. {
  18. if((num[len-i-1] >= 'A') && (num[len-i-1] <= 'F'))
  19. dig = num[len-i-1] - 55;
  20. else
  21. dig = num[len-i-1] - 48;
  22. result += (dig * pow(base, i));
  23. // printf("num[%d] = %d\n", len-i-1, dig);
  24. }
  25. return result;
  26. }
  27.  
  28. void convert_basei(long int num, int base, std::string& result)
  29. {
  30. char rem;
  31. result = "";
  32. while (num)
  33. {
  34. if ((num % base) >= 10)
  35. {
  36. rem = (num % base) + 55;
  37. }
  38. else
  39. {
  40. rem = (num % base) + 48;
  41. }
  42. result.insert(0, 1, rem);
  43. num /= base;
  44. }
  45. }
  46. int main()
  47. {
  48. string num;
  49. string result;
  50. int base1, base2, no_spaces;
  51. while(cin>>num>>base1>>base2)
  52. {
  53. long int temp = convert_base10(num, base1);
  54. convert_basei(temp, base2, result);
  55. if(result.size() > 7)
  56. cout<<" ERROR"<<endl;
  57. else
  58. {
  59. no_spaces = 7 - result.size();
  60. for(int i=0; i<no_spaces; i++)
  61. cout<<" ";
  62. cout<<result<<endl;
  63. }
  64.  
  65. }
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0s 3036KB
stdin
1111000  2 10
1111000  2 16
     2102101        3      10
2102101  3 15
  12312  4  2
     1A 15  2
1234567 10 16
   ABCD 16 15
stdout
    120
     78
   1765
    7CA
  ERROR
  11001
 12D687
   D071