Advertisement
avskyRB

Dec-Bin-Oct-Hex.cxx

Sep 2nd, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <vector>
  4.  
  5. void convert(int num, int base)
  6. {
  7.  
  8.  std::vector<int> buffer;
  9.  
  10.   while(num>0)
  11.   {
  12.     buffer.push_back(num%base);
  13.     num = num / base;
  14.   }
  15.  
  16.   if (base==2)
  17.     std::cout << "Rappresentazione binaria     = ";
  18.   if (base==8)
  19.     std::cout << "Rappresentazione ottale      = ";
  20.   if (base==16)
  21.     std::cout << "Rappresentazione esadecimale = ";
  22.  
  23.   for (int i=buffer.size()-1; i>=0; i--)
  24.   {
  25.    if (buffer[i]>=10)
  26.      std::cout << char(buffer[i]+55);
  27.    else
  28.     std::cout << buffer[i];
  29.   }
  30. }
  31.  
  32.  
  33. int main()
  34. {
  35.   // Richiesta del numero da convertire
  36.   std::cout << "Immettere intero in rappresentazione decimale \n";
  37.   int num;
  38.   std::cin >> num;
  39.  
  40.   convert(num,2);
  41.   std::cout << std::endl;
  42.  
  43.   convert(num,8);
  44.   std::cout << std::endl;
  45.  
  46.   convert(num,16);
  47.   std::cout << std::endl;
  48.  
  49.   return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement