感觉我写的程序比较混乱,正确的写法应该是什么啊
要求满足最大正实数,0,一般正负实数均成立,我的程序运行后最大正实数答案错误
读入一个十进制小数,输出其在计算机中的二进制存储形式(假设64位双精度浮点数)。
输入格式:
输入在一行中给出的一个十进制小数。
输出格式:
在一行中输出其在计算机中的二进制存储形式(假设64位双精度浮点数)。
输入样例1:
123.4567
输出样例1:
0100000001011110110111010011101010010010101000110000010101010011
输入样例2:
-0.12345
输出样例2:
1011111110111111100110100110101101010000101100001111001001111100
#include <stdio.h>
i#include <stdio.h>
int main()
{
//起始数据准备
double num;
scanf("%lf", &num);
double x;
double sub;
sub = num;
if(num < 0){
num = -num;
}
if(num!=0&&num!=179770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000){
int w;
w = (int)num;
x = num -w;
//对小数部分转换为二进制储存在数组中
int integer;
int i = 1;
int c[100];
int pamount = 0;
for(i=1;(int)x!=x && i<100;i++ ){
x = x * 2;
integer = (int)x;
c[i] = integer;
x = x - integer;
pamount++;
//printf("%d", c[i]);
}
//printf("\n");
//对整数部分
int left[100];
int j=1;
int sum = 0;
int n=0;
while(w!=0){
left[j] = w%2;
w= w/2;
sum ++;
j++;
}
int degree = sum + 1022;
//阶数部分
int dleft[100];
int r=1;
int dsum = 0;
int m=0;
while(degree!=0){
dleft[r] = degree%2;
degree = degree/2;
dsum ++;
r++;
}
//组合数据
//符号位
int allthe[63];
int p = 0;
//找好每个类别的起始项
r=dsum;
j=sum-1;
i=1;
//开始循环
for(p=0;p<=63;p++){
if(p==0){
if(sub >= 0){
allthe[p] = 0;
}
if(sub < 0){
allthe[p] = 1;
}
}
//阶数
else if(p>=1&&p<=11){
if(m+dsum<11){
allthe[p] = 0;
m++;
}
else if(r>=1){
allthe[p] = dleft[r];
r--;
}
}
//尾数
else if(p>=12&&p<=sum+11-1&&j>=1){
allthe[p] = left[j];
j--;
}
else if(p>sum+11-1&&p<=63){
if(11+sum-1+pamount>=63&&i<=pamount&&11+sum-1+i<=63){
allthe[p] = c[i];
i++;
}
else if(11+sum-1+pamount<63&&i<=pamount){
allthe[p] = c[i];
i++;
}
else if(i+sum-1+11<63){
allthe[p] = 0;
}
}
}
//eventually
for(p=0;p<=63;p++){
printf("%d", allthe[p]);
}
}
else if(num==0){
printf("0000000000000000000000000000000000000000000000000000000000000000");
}
else{
printf("0111111111101111111111111111111111111111111111111111111111111111");
}
return 0;
}