- 這代碼寫的真的是越寫越冗長
- 無力吐槽
#include <iostream>
using namespace std;
void tran_dayu0_b_hex(int x)//轉換函數1
{
if (x > 0)
{
static int a[1000];
static int cal = 0;//用於store、計數
int yushu;
yushu = x % 2;
x = x / 2;
a[cal] = yushu;
cal++;
if (x != 0)
{
tran_dayu0_b_hex(x);//調用函數本身
}
else//x=0時結束遞歸
{
for (int i = cal - 1; i >= 0; i--)
{
cout << a[i];//打印輸出二進制
}
}
}
else if (x == 0)
{
cout << "0" << endl;
}
}
void tran_xiaoyu0_b_hex(float x)//轉換函數2
{
static int a[1000];
static int cal = 0;//store
x = x * 2;
int j = x;
//float jz
x = x - int(x);
a[cal] = j;
cal++;
if (x==0)//x=0時結束遞歸
{
//cout << "0.";
for (int i = 0; i <= cal-1 ; i++)
{
cout << a[i];//打印輸出二進制
}
}
else
{
tran_xiaoyu0_b_hex(x);//調用函數本身
}
}
void tran_b_hex(float x)//主轉換函數
{
if (x>1)
{
if (x - int(x) == 0) //輸入的x為大於1的整數
{
int y;
y = x;
tran_dayu0_b_hex(y); //調用轉換函數1
}
else //輸入一個x為大於1的實數(帶小數點)
{
int y;
y = x;
tran_dayu0_b_hex(y);//調用轉換函數1
cout << ".";
float m;
m = x - int(x);
tran_xiaoyu0_b_hex(m);//調用轉換函數2
}
}
else //輸入的x為小於的1的小數
{
cout << "0.";
tran_xiaoyu0_b_hex(x);
}
}
int main()
{
cout << "請輸入一個十進制的任意實數" << endl;
float x;
cin >> x;
tran_b_hex(x);//主轉換函數
return 0;
}
---yyz
2018.11.4