C++



贊助商連結


pukman2
2001-12-10, 09:59 PM
請問一下,有沒有會用C++寫十進位轉二進位的程式,幫忙一下!!Thanks!

贊助商連結


Neoq
2002-01-01, 04:15 AM
最初由 pukman2 發表
請問一下,有沒有會用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; }

Neoq
2002-01-01, 06:11 PM
//上一個版本很容易暴掉,故加以修改成String版。

//C++
//請問一下,有沒有會用C++寫十進位轉二進位的程式,幫忙一下!!Thanks!

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

#include <iostream.h>
#include <string.H>

void GetData(unsigned int *);
void Transfer(unsigned int *);

void main(void)
{ unsigned int *Ten;

GetData(Ten);
Transfer(Ten);
}

void GetData(unsigned int *nTen)
{
cout << "Input number to be transfered:";
cin >> *nTen;

}

void Transfer(unsigned int *uTen)
{ int over, i = 0; //quotient=商, over=餘
char Two[64] = {0};

cout << "\nThe answer is: ";

do{over = *uTen % 2; //求餘數
*uTen /= 2; //求商數
(over) ? (Two[i++] = '1') : (Two[i++] = '0'); //依餘數轉換成二進位的表示法
} while(*uTen > 0);

strrev(Two);
cout << Two;
}