#include <iostream>
#include<string>
using namespace std;
int main()
{
cout << "请输入十进制数:";
double num;
cin >> num;
cout << "请输入(2-9)目标进制数:";
int base;
cin >> base;
int integer = (int)num;
double small = num - integer;
cout << integer << " " << small << endl;
//整数部分的转换
int integers[32]{};
int i = 0;
while (integer != 0 && i < 32)
{
integers[i] = integer % base;
integer = integer / base;
i = i + 1;
};
//小数部分的转换
int smalls[8]{};
double number = small;
for (int j = 0; j < 8; j++)
{
number *= base;
cout << number << endl;
if (number >= 1.0)
{
int s = floor(number);
smalls[j] = s;
number = number - s;
}
if (number == 0)
{
break;
}
}
string num0;
std::cout << "十进制数:(" << num << ")=(";
for (int k = i - 1; k >= 0; k--)
{
cout << integers[k];
num0 += to_string(integers[k]);
}
cout << ".";
num0 += '.';
for (int s : smalls)
{
cout << s;
num0 += to_string(s);
}
cout << ") " << base << "进制数" << endl;
cout << num0;
return 0;
};
十进制数转换(2-9)的进制数转换
于 2023-03-23 19:05:19 首次发布