Public paste
final
By: poleXican | Date: Nov 27 2007 03:33 | Format: C++ | Expires: never | Size: 2.35 KB | Hits: 1199

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