?? 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 + -