⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ieee754.cpp

📁 IEEE754格式
💻 CPP
字号:
#include <iostream.h>   
#include <stdio.h>  
#include <stdlib.h>
#include <math.h>
//#include <windows.h>
//#include<conio.h>
void tentohex();
void binarytoten();
const int max=100; 

    
void  main()   
{   
	int ch;
	
	cout<<"1 for a float to hex."<<endl;
	cout<<"2 for IEEE754 to a float."<<endl;
	cout<<"please choose a function(zero to end):"<<endl;
	cin>>ch;
	while(1)
	{
	  switch(ch)
	  {
	   case 1:
	    	tentohex();
	    	break;
	   case 2:
	    	binarytoten();
	    	break;
    	case 0:
		    exit(0);

	  }
	  //clrscr();
	  	cout<<"1 for a float to hex."<<endl;
	    cout<<"2 for IEEE754 to a float."<<endl;
	    cout<<"please choose a function(zero to end):"<<endl;
	    cin>>ch;
	}


	


}   
void tentohex()//浮点数化地址
{
  float a; 
  cout<<"enter a float number:"<<endl;
  cin>>a;
  int *p =(int *)&a;   
  printf("it's hex is:%X\n",*p); 
  cout<<endl;
 
}

void binarytoten()//32位二进制化浮点数
{
	char Q[max],E[max],M[max];
	int i=0,j=0,e=0;
	char *p=NULL,ch;
	int change;
	float sum_a=0,sum_b=0,sum=0;
	int e1;
	cout<<"please enter 32 bit:"<<endl;
	cin>>Q;
	char s;
	s=Q[0];//S部分
	for(i=0;i<8;i++)//存放E
	{
		E[i]=Q[i+1];
	}
	for(i=0;i<32;i++)//M部分
	{
		M[i]=Q[i+9];
	}
	for(i=0;i<8;i++)//把E化成e
	{
		ch=E[i];
		p=&ch;
		change=atoi(p);
		e+=change*pow(2,7-i);
	}
	e=e-127;
	//cout<<e<<endl;
	/////////////开始求小数点前面的十进制
	e1=e;
	sum_a=pow(2,e);
	for(i=0;i<e;i++)
	{
		ch=M[i];
		p=&ch;
		change=atoi(p);
		sum_a+=change*pow(2,e1-1);
		e1--;
	}
	/////////////求小数部分的十进制

	j=-1;

	for(i=e;i<23;i++)
	{
		ch=M[i];
		p=&ch;
		change=atoi(p);
		
		sum_b+=change*pow(2,j);
		j--;
	}


	sum=sum_a+sum_b;//整数与小数部分的和

	if(s=='0')
		cout<<"it's float is :"<<sum<<endl;
	if(s=='1')
		cout<<"it's float is :"<<'-'<<sum<<endl;
	cout<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -