10 to 2



贊助商連結


Neoq
2002-01-01, 04:07 AM
//C++
//請問一下,有沒有會用C++寫十進位轉二進位的程式,幫忙一下!!Thanks!

//不知這你的意思是不是這樣....

#include <iostream.h>

int GetData(void);
unsigned long Transfer(int);
void Display(unsigned long);

void main(void)
{ int Ten;
unsigned long Two;

Ten = GetData();
Two = Transfer(Ten);
Display(Two);
}

int GetData(void)
{ int Ten;
cout << "Input number to be transfered:";
cin >> Ten;
return Ten;
}

unsigned long Transfer(int Ten)
{ int quotient = Ten, over; //quotient=商, over=餘
unsigned long Two = 0, exp = 1;

do{ over = quotient % 2; //求餘數
quotient /= 2; //求商數
Two = Two + over * exp; //依餘數轉換成二進位的十進位表示法
exp *= 10;
} while(quotient > 0);

return Two;
}

void Display(unsigned long Two)
{ cout << "\nThe answer is : " << Two; }