Public paste
s
By: s | Date: Nov 27 2007 02:30 | Format: C++ | Expires: never | Size: 2.89 KB | Hits: 1188

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <stack>
  5.  
  6.  
  7.  
  8.  
  9. using namespace std;  
  10. string dectobin(unsigned int num);
  11. string convert(int num, int n);
  12.  
  13. int main()
  14.     {
  15.     ifstream infile;
  16.     ofstream outfile;
  17.    
  18.     infile.open("input6.txt");
  19.     outfile.open("ouput6.txt");
  20.    
  21.     string x;
  22.     int decimalnum;
  23.     int bits;
  24.     infile >> bits;
  25.    
  26.     if (!infile)
  27.     cout<<"Unable to open file input5.txt";  
  28.    
  29.     while(infile >> decimalnum)
  30.     {
  31.     infile >> decimalnum;                                
  32.     //cout <<  decimalnum;
  33.     x = convert(decimalnum,bits);
  34.     outfile <<  decimalnum << " = " << x << endl; //outputs the value of dectobin
  35.     }              
  36.  
  37.  
  38.  
  39.      
  40.       infile.close();
  41.       outfile.close();
  42.      
  43.      
  44.      
  45.     system("PAUSE");
  46.     return 0;
  47. }  
  48.  
  49.  
  50. string convert(int num, int n)
  51. {      
  52.  
  53.        string y;
  54.        int num2;
  55.                 stack<char> tmpStack;
  56.                 stack<char> myStack;
  57.                 string tmp;
  58.        bool doIt = false;
  59.  
  60.    
  61.           if ( num > 0)
  62.           {
  63.           y = dectobin(num);
  64.           }
  65.              while (y.size() <= n-1 ) // adds 0's
  66.        {
  67.             y = "0" + y;
  68.            
  69.        }
  70.          
  71.           if ( num <0)
  72.           {
  73.          num2 = (num*-1);
  74.            tmp = dectobin(num2);
  75.                while (y.size() <= n-1 )// adds 0's
  76.        {
  77.             y = "0" + y;
  78.            
  79.        }
  80.            
  81.             for (int k=0;tmp.length(); k++)
  82.             {
  83.             tmpStack.push(tmp[k]);
  84.             }
  85.           while (!tmpStack.empty())
  86.           {
  87.                if (tmpStack.top() == '1' && doIt)
  88.                {
  89.                myStack.push('0');
  90.                tmpStack.pop();
  91.                }
  92.  
  93.                else
  94.                if (tmpStack.top() == '0' && doIt)
  95.                {
  96.                myStack.push('1');
  97.                tmpStack.pop();
  98.                }
  99.  
  100.                else
  101.                if (tmpStack.top() == '1' && doIt == false)
  102.                {
  103.                myStack.push('1');
  104.                 doIt = true;
  105.                 tmpStack.pop();
  106.                  }
  107.  
  108.                  else
  109.                  if (tmpStack.top() == '0' && doIt == false)
  110.                  {
  111.                   myStack.push('0');
  112.                   tmpStack.pop();
  113.                   }  
  114.                   while (!myStack.empty())
  115.                   {
  116.                   y += myStack.top();
  117.                   myStack.pop();
  118.                   }
  119. }
  120.  
  121. }
  122.      
  123.           return y;    
  124.          
  125. }
  126. string dectobin(unsigned int num)
  127.  
  128. {    
  129.      
  130.         string b;  
  131.         if( num > 0)
  132.         {
  133.         b = dectobin(num/2) +
  134.         static_cast<char>('0'+ (num % 2));
  135.         return b;  // <--- return the concatenation
  136.         }    
  137.         return ""; // <--- return the empty string (in case num == 0)
  138. }