Guest User

Dec to [2-36]-th base

a guest
Feb 7th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     if (argc < 3)
  10.     {
  11.         cout << "Uzycie:   " << argv[0] << " <liczba DEC> <baza>" << endl;
  12.         cout << "Przyklad: " << argv[0] << " 254 16" << endl;
  13.        
  14.         return 0;
  15.     }
  16.    
  17.     if (atoi(argv[2]) < 2 or atoi(argv[2]) > 36)
  18.     {
  19.         cout << "Baza musi byc miedzy 2 a 36!" << endl;
  20.        
  21.         return 0;
  22.     }
  23.    
  24.     vector<char> cyfry;
  25.     int baza = atoi(argv[2]);
  26.    
  27.     cout << "cyfry systemu " << argv[2] << ":" << endl;
  28.    
  29.     for (int i = 0; i < baza; i++)
  30.     {
  31.         if (i < 10)
  32.         {
  33.             cyfry.push_back((char)('0' + i));
  34.         }
  35.         else
  36.         {
  37.             cyfry.push_back((char)('A' + i - 10));
  38.         }
  39.        
  40.         cout << cyfry[i] << " ";
  41.     }
  42.    
  43.     cout << endl << endl;
  44.    
  45.     int liczba = atoi(argv[1]);
  46.     string hex = "";
  47.     int temp = liczba;
  48.     vector<int> div;
  49.     int i = 0;
  50.    
  51.     while (temp > 0)
  52.     {
  53.         div.push_back(temp % baza);
  54.        
  55.         temp -= div[i];
  56.         temp /= baza;
  57.        
  58.         i++;
  59.     }
  60.    
  61.     for (int i = div.size() - 1; i >= 0; i--)
  62.     {
  63.         hex += cyfry[div[i]];
  64.     }
  65.    
  66.     cout << liczba << "{10} = " << hex << "{" << baza << "}" << endl;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment