-
C++
請問一下,有沒有會用C++寫十進位轉二進位的程式,幫忙一下!!Thanks!
-
會員
Re: C++
最初由 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; }
-
會員
Re: C++ 另一個版本
//上一個版本很容易暴掉,故加以修改成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;
}
發表文章規則
- 您不可以發表新主題
- 您不可以回覆文章
- 您不可以上傳附加檔案
- 您不可以編輯自己的文章
-
討論版規則
書籤