十进制转二进制的程序...(限制溢出...呵呵..)
#include <stdio.h>#include <string.h>
void outint(int);
void outfloat(float ,int );
int main()
{
float num;
int i,count;
float f;
printf("Please enter a decimal count:");
scanf("%f",&num);
if(num>10240.0||num<-10240)
{
printf(" You must use a high-level calculator,I can't help you!!!!\n");
exit(1);
}
i=num;
f=num-i;
if(f!=0)
{
printf("How many decimal do you want to retain:");
scanf("%d",&count);
printf("The count entered by you has been changed,look!!!-->:");
if(i!=0)
outint(i);
else
printf("0");
outfloat(f,count);
}
else if(i!=0)
outint(i);
else
printf("0");
printf("B\n");
}
void outint(int i)
{
int a[32],m=0,n=0,mod;
int next=0;
if(i>=0)
{
while(i/2>=1)
{
a[next]=i%2;
//printf("%d",a[next]);
++next;
i/=2;
}
a[next]=1;
next++;
a[next]=0;
//printf("%d",a[next]);
//printf("%d\n",a[0]);
//printf("%d\n",next);
}
else
{
i*=-1;
while(i/2>=1)
{
a[next]=i%2;
//printf("%d",a[next]);
++next;
i/=2;
}
a[next]=1;
next++;
a[next]=1;
}
for(m=0;m<=next-m;m++)
{
mod=a[m];
a[m]=a[next-m];
a[next-m]=mod;
}
for(m=0;m<=next;m++)
printf("%d",a[m]);
}
void outfloat(float f,int count)
{
int a[32],next=0,i;
printf(".");
while(f*2.0!=1.0)
{
if(f*2.0>1.0)
{
a[next]=1;
next++;
f=f*2.0-1.0;
}
else
{
a[next]=0;
next++;
f=f*2.0;
}
}
for(i=0;i<=next&&i<=count-1;i++)
printf("%d",a[i]);
}