10进制小数转换成2进制问题
想法是用队列实现那里出错了?
#include "stdio.h"
#include "stdlib.h"
typedef int datatype;
typedef struct linknode
{
datatype info;
struct linknode *next;
}node;
typedef struct
{
node *rear,*front;
}queue;
queue *initqueue()
{
queue *q;
q=(queue*)malloc(sizeof(queue));
q->rear=NULL;
q->front=NULL;
return q;
}
queue *insert(queue *q,datatype oPara)
{
node *p;
p=(node*)malloc(sizeof(node));
p->info=oPara;
p->next=NULL;
if(q->front==NULL)
{
printf("empty");
}
else
{
q->rear->next=p;
q->rear=p;
}
return q;
}
void printqueue(queue *q)
{
node *p;
p=q->front;
if(!p)
{
printf("empty");
}
else
{
while(p)
{
printf("%d",p->info);p=p->next;
}
}
}
/*node *push(node *top,datatype oPara)
{
node *p;
p=(node*)malloc(sizeof(node));
p->info=oPara;
p->next=top;
top=p;
return top;
}
void printstack(node *top)
{
node *p;
p=top;
if(!p)printf("empty");
else{
while(p){printf("%d",p->info);p=p->next;}
}
}
void convertint(datatype oPara)
{
node *top=NULL;
while(oPara!=0)
{
top=push(top,oPara%2);
oPara=oPara/2;
}
printstack(top);
}*/
void convertfloat(float oPara)
{
queue *q=initqueue();
while(oPara!=0.0)
{
q=insert(q,(int)(oPara*2));
oPara=oPara*2;
oPara=oPara-(int)oPara;
}
printqueue(q);
}
int main()
{
//convertint(5);
convertfloat(0.1);getchar();
}