asedf
By: asdf | Date: Nov 25 2007 03:39 | Format: C++ | Expires: never | Size: 2.11 KB | Hits: 1241
- #include <iostream>
- #include <sstream>
- using namespace std;
- void dectobin(int num, int base);
- void convert(int n, int decimalnum, int base);
- int main()
- { char choice;
- int decimal;
- int base;
- int bits;
- base = 2;
- cout << "enter decimal: ";
- cin >> decimal;
- cout << endl;
- cout<< "Decimal " << decimal << " = ";
- convert(bits, decimal, base);
- cout << " binary" <<endl;
- cout << "do it again? enter y or n: ";
- cin >> choice;
- switch (choice)
- {
- case 'y':
- return main();
- break;
- case 'n':
- return 0;
- break;
- }
- }
- void convert(int n, int decimalnum, int base)
- {
- int code= 0;
- if (decimalnum > 0)
- {
- dectobin(decimalnum, base);
- }else
- if (decimalnum <0)
- {
- decimalnum = decimalnum * -1;
- dectobin(decimalnum, base);
- }
- if (decimalnum==0)
- {
- cout << code;
- }
- }
- void dectobin(int num, int base)
- {
- string str;
- stringstream out;
- int i;
- /*
- i = num % base;
- if( num/base > 0)
- {
- string test;
- string str;
- stringstream out;
- out << i;
- str = out.str();
- test = dectobin ( num/base, base );
- cout << "here is hello from " << num << " and my result is: >" << test << "< !" << endl;
- return test;
- }
- if ( num/base == 0)
- {
- i = num % base;
- string str;
- stringstream out;
- out << i;
- str = out.str();
- return str;
- }
- */
- if (num == 0)
- {
- i = 0;
- out << i;
- str = out.str();
- cout << str;
- }
- if( num > 0)
- {
- dectobin (num/base, base);
- i = num % base;
- out << i;
- str = out.str();
- cout << str;
- }
- }
Latest pastes
2 days ago
2 days ago
5 days ago
7 days ago
7 days ago