#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct
{
int top;
int data[MAXSIZE];
}SeqStack;
typedef struct{
int data[MAXSIZE];
int front;
int rear;
}SeqQueue;
int push(SeqStack *s,int x)
{
if(s->top==MAXSIZE-1)
{
printf("栈已满");
return 0;
}
else
{
s->top++;
s->data[s->top]=x;
return 1;
}
}
void decimals(float t);
void integer(int x);
int main()
{
float x;
float z;
int y=0;
int w;
printf("请输入一个数字:");
scanf("%f",&x);
y=(int)x;
z=x-y;
if(z==0)
{
integer(x);
}
else
{
integer(x);
decimals(z);
}
}
void integer(int x)
{
SeqStack s;
s.top=-1;
int number=x;
while(number)
{
s.top++;
s.data[s.top]=number%2;
number=number/2;
}
printf("对应的二进制数为:\n");
while(s.top!=-1)
{
printf("%d",s.data[s.top--]);
}
}
void decimals(float t)
{
SeqQueue Q;
int m;
int n=6;
Q.front=Q.rear=-1;
if(t!=0)
{
printf("0.");
while(n!=0&&t!=0)
{
t=t*2;
m=(int)t;
Q.data[++Q.rear]=m;
t=t-m;
n--;
}
while(Q.front!=Q.rear)
printf("%d",Q.data[++Q.front]);
}
}