Advertisement
TheWhiteFang

ECE3206_MidTerm_1_Main_Q1 COMPLETED

Jan 26th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. //pastebin.com/u/thewhitefang
  2. //Midterm Q1
  3. #include <iostream>
  4. #include <string>
  5. #include <fstream>
  6. using namespace std;
  7. // Incomplete Class
  8. class DecBin{
  9. private:
  10.     int m_Decimal;
  11.     string m_Binary;
  12.  
  13. public:
  14.     DecBin(int inDec){
  15.         if (inDec < 255){
  16.             m_Decimal = inDec;
  17.             DecToBin(inDec);
  18.         }
  19.         else
  20.         {
  21.             m_Decimal = 0;
  22.         }
  23.     }
  24.  
  25.     void LeftShiftBin(int inBin){
  26.    
  27.         string  temp;
  28.         temp.assign(m_Binary);
  29.         temp = m_Binary.substr(0, inBin); //temp will store the shifted bits
  30.         m_Binary.append(temp); //append temp to the back
  31.         m_Binary.erase(0,inBin);
  32.         /*m_Binary = x;*/
  33.         BinToDec(m_Binary);
  34.  
  35.     }
  36.  
  37.     void DecToBin(int inDec){
  38.  
  39.         //iterative - will repeat until inDec < 0
  40.         while (inDec > 0)
  41.         {
  42.             //if got remainder, add 1 infront      
  43.             if (inDec % 2 != 0)
  44.             {
  45.                 m_Binary = '1' + m_Binary;
  46.             }
  47.             else
  48.             {
  49.                 m_Binary = '0' + m_Binary;
  50.             }
  51.  
  52.             inDec = inDec / 2;
  53.         }
  54.  
  55.         while (m_Binary.length() != 8)
  56.         {
  57.  
  58.                 m_Binary = '0' + m_Binary;
  59.         }
  60.     }
  61.    
  62.     int GetDecimal(){
  63.         return m_Decimal;
  64.     }
  65.  
  66.     void GetBinary(string &inBin){
  67.         inBin = m_Binary;
  68.     }
  69.  
  70.     void BinToDec(string inBin){
  71.    
  72.         /*std::string::size_type sz;*/
  73.         /*int temp;
  74.         temp = stoi(inBin,nullptr,2);*/
  75.         reverse(inBin.begin(), inBin.end());
  76.         m_Decimal = 0;
  77.         for (int i = 0; i < inBin.size(); ++i) {
  78.             m_Decimal += (int(inBin[i])-48)*pow(2, i);
  79.             // 48 is '0' converted from string
  80.         }
  81.        
  82.  
  83.     }
  84. };
  85.  
  86.  
  87. //string CConversion::decimalToBinaryRecursive(int num)
  88. //{
  89. //  if (num <= 0)
  90. //  {return "";}
  91. //  else
  92. //  {
  93. //      if (num % 2 != 0)
  94. //      {
  95. //          return decimalToBinaryRecursive(num / 2) + '1';
  96. //      }
  97. //      else
  98. //      {
  99. //          return  decimalToBinaryRecursive(num / 2) + '0';
  100. //      }
  101. //  }
  102. //
  103. //}
  104. int main(){
  105.     int decimal;
  106.     cout << "Enter a decimal number between 0 & 255: ";
  107.     cin >> decimal;
  108.     DecBin obj(decimal);
  109.     string bin;
  110.     string z = { "0" };
  111.  
  112.     /*int x;
  113.     x = (int(z[0]));
  114.     cout << x << endl << endl;*/
  115.  
  116.     obj.GetBinary(bin);
  117.     cout << "Before left shift. Binary of decimal " << decimal << " is: " << bin << endl;
  118.     obj.LeftShiftBin(2);
  119.     obj.GetBinary(bin);
  120.     cout << "After left shift. Decimal of binary " << bin << " is: " << obj.GetDecimal() << endl;
  121.         return 0;
  122.        
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement