dddd
By: dddd | Date: Nov 27 2007 01:51 | Format: C++ | Expires: never | Size: 1.69 KB | Hits: 1227
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <stack>
- using namespace std;
- string dectobin(unsigned int num);
- string convert(int num, int n);
- int main()
- {
- ifstream infile;
- ofstream outfile;
- infile.open("input6.txt");
- outfile.open("ouput6.txt");
- string x;
- int decimalnum;
- int bits;
- infile >> bits;
- if (!infile)
- cout<<"Unable to open file input5.txt";
- while(!infile.eof())
- {
- infile >> decimalnum;
- //cout << decimalnum;
- x = convert(decimalnum,bits);
- outfile << decimalnum << " = " << x << endl; //outputs the value of dectobin
- }
- infile.close();
- outfile.close();
- system("PAUSE");
- return 0;
- }
- string convert(int num, int n)
- {
- string y;
- int num2;
- stack<char> s;
- if ( num > 0)
- {
- y = dectobin(num);
- cout << y << endl;
- }else
- if ( num <0)
- {
- num2 = (num*-1);
- y = dectobin(num2);
- reverse(y);
- }
- while (y.size() <= n-1 )
- {
- y = "0" + y;
- }
- return y;
- }
- string dectobin(unsigned int num)
- {
- string b;
- if( num > 0)
- {
- b = dectobin(num/2) +
- static_cast<char>('0'+ (num % 2));
- return b; // <--- return the concatenation
- }
- return ""; // <--- return the empty string (in case num == 0)
- }
Latest pastes
1 days ago
2 days ago
5 days ago
6 days ago
6 days ago