#include<iostream>
using namespace std;
//十进制转化为二进制
#define Array_Size 32
#define Point_Size 8
//交换函数
void Integer_Swap(int & intLeft, int &intRight)
{
int temp = intLeft;
intLeft = intRight;
intRight = temp;
}
//数组置逆
void Array_Reverse(int *a, int len)//数组做参数
{//指针出界
for (int i = 0; i < (len + 1) / 2; i++)
{
Integer_Swap(*(a + i), *(a + len - 1 - i));
}
}
//定义一个浮点转化类
class FLOAT_BIANRY
{
private:
//原始函数
float Material;
float Material_trans;
//正负号
int Symbol;
//整数部分
int Ingeter_Part;
//小数部分
float Decimal_Part;
//整数部分二进制数组
int Ingeter_Part_Array[Array_Size] = { 0 };
//小数部分二进制数组
int Decimal_Part_Array[Array_Size] = { 0 };
//二进制指数
int Array_Exp[Point_Size] = { 0 };
//指数偏移量
int OFFSET_Exp = 127;
//小数点移动位数
int Bits_Point=-1;
//总体
int Material_Binary[Array_Size] = { 0 };
public:
//构造函数
FLOAT_BIANRY(float material)
{
this->Material = material;
}
//析构函数
//0.判断正负
void Judge_PosiOrnega();
//1.提取整数部分
void Ingeter_Abs();
//2.提取小数部分
void Decimal_Abs();
//3.整数部分二进制化
void Ingeter_Binary();
//4.小数部分二进制化
void Decimal_Binary();
//5.确定小数点移动位数
void Judge_Point_Move();
//6.总体
void Array_Total();
//7.打印输出
void Print_Binary();
};
//0.判断正负
void FLOAT_BIANRY::Judge_PosiOrnega()
{
if (Material < 0)
{
this->Symbol = 1;
this->Material_trans = -this->Material;
}
else
{
this->Symbol = 0;
this->Material_trans = this->Material;
}
}
//1.提取整数部分
void FLOAT_BIANRY::Ingeter_Abs()
{
Ingeter_Part = (int)this->Material_trans;
}
//2.提取小数部分
void FLOAT_BIANRY::Decimal_Abs()
{
this->Decimal_Part = this->Material_trans - this->Ingeter_Part;
}
//3.整数部分二进制化
void FLOAT_BIANRY::Ingeter_Binary()
{
int Temp= Ingeter_Part;
int index = 0;
while (Temp != 0)
{
if (Temp % 2 == 0)
{
Ingeter_Part_Array[index++] = 0;
}
else
{
Ingeter_Part_Array[index++] = 1;
}
Temp /= 2;
(this->Bits_Point)++;
}
//调换顺序
Array_Reverse(Ingeter_Part_Array, sizeof(Ingeter_Part_Array) / sizeof(*Ingeter_Part_Array));
}
//4.小数部分二进制化
void FLOAT_BIANRY::Decimal_Binary()
{
float Temp = this->Decimal_Part;
int index = 0;
while (Temp != 0)
{
if (Temp *2 >= 1)
{
Decimal_Part_Array[index++] = 1;
Temp = 2 * Temp - 1;
}
else
{
Decimal_Part_Array[index++] = 0;
Temp = 2 * Temp ;
}
}
}
//5.确定小数点移动位数
void FLOAT_BIANRY::Judge_Point_Move()
{
int Temp = OFFSET_Exp + Bits_Point;
int index = 0;
while (Temp != 0)
{
if (Temp % 2 == 0)
{
Array_Exp[index++] = 0;
}
else
{
Array_Exp[index++] = 1;
}
Temp /= 2;
}
//调换顺序
Array_Reverse(Array_Exp, sizeof(Array_Exp) / sizeof(*Array_Exp));
}
//6总体
void FLOAT_BIANRY::Array_Total()
{ //初始化第31位,第0位
if (Symbol == 1) Material_Binary[0] = 0;
//初始化30-23位,第1-8位
for (int i = 1, j = 0; i <= 8; i++,j++)
Material_Binary[i] = Array_Exp[j];
//初始化22-0位,第9-31位,0-22,,整数部分和小数部分
for (int i = 9, j = 31; i <9+ Bits_Point; i++, j--)
Material_Binary[i] = Ingeter_Part_Array[j];
for(int i=9+ Bits_Point,j=0;i<32;i++,j++)
Material_Binary[i] = Decimal_Part_Array[j];
}
//7.打印输出
void FLOAT_BIANRY::Print_Binary()
{
Judge_PosiOrnega();//1.判断正负
Ingeter_Abs();//2.提取整数部分
Ingeter_Binary();//整数部分二进制
Decimal_Abs(); //3.提取小数部分
Decimal_Binary();//4.小数部分二进制
Judge_Point_Move();//5.确定小数点移动位数
Array_Total();//6.最终函数
for (int i = 0; i < Array_Size; i++)
{
if (i == 1 || i == 9) cout << " ";
cout << Material_Binary[i];
}
cout << endl;
}
int main() {
float float_User;
cout << "用户您好,请您输入一个浮点数:" << endl;
cin >> float_User;
FLOAT_BIANRY Float_User(float_User);
Float_User.Print_Binary();
system("pause");
return 0;
}
FLOAT型的二进制输出.rar
需积分: 9 132 浏览量
2021-11-12
09:43:55
上传
评论
收藏 10MB RAR 举报
兆谦
- 粉丝: 0
- 资源: 1
最新资源
- LM324_datasheet.pdf
- 全新Storm+Core+API管理系统源码
- 基于RP2040的电子沙漏,使用RP2040游戏机开发板,灯板是74HC595驱动的8*8LED矩阵
- 基于SSM和VUE的商店POS积分管理系统(免费提供全套java开源项目源码+论文)
- 基于SpringBoot的“在线动漫信息平台”的设计与实现.rar
- NPP xml tools
- 基于python开发的树莓派RP2040的游戏机
- 基于SNMP网络设备MIB信息采集系统(免费提供全套java开源项目源码+论文)
- 基于SSM和VUE的五子棋游戏的设计(免费提供全套java开源项目源码+论文)
- qiun-data-charts
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0