Advertisement
avskyRB

Dec-Bin-Oct.cxx

Sep 2nd, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 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.  
  21.   for (int i=buffer.size()-1; i>=0; i--)
  22.     std::cout << buffer[i];
  23. }
  24.  
  25. int main()
  26. {
  27.   // Richiesta del numero da convertire
  28.   std::cout << "Immettere intero in rappresentazione decimale \n";
  29.   int num;
  30.   std::cin >> num;
  31.  
  32.   convert(num,2);
  33.   std::cout << std::endl;
  34.  
  35.   convert(num,8);
  36.   std::cout << std::endl;
  37.  
  38.   return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement