c语言编程,如何将十进制转化为2进制
展开全部
#include
"stdio.h"
#define
StackSize
100
typedef
int
ElemType;
typedef
struct
{
ElemType
elem[StackSize];
int
top;
}SqStack;
InitStack(SqStack
*pS)
{
pS->top=0;
/*
top指向栈顶的上一个元素
*/
}
int
Push(SqStack
*pS,ElemType
e)
{
if
(pS->top==StackSize-1)
/*
栈满
*/
return
0;
pS->elem[pS->top]=e;
pS->top=pS->top+1;
return
1;
}
int
Pop(SqStack
*pS,ElemType
*pe)
{
if
(pS->top==0)
/*
栈空
*/
return
0;
pS->top
=
pS->top
-
1;
*pe
=
pS->elem[pS->top];
return
1;
}
main()
{
SqStack
S;
ElemType
e;
int
N;
InitStack(&S);
printf("请输入一个十进制整数:\n");
scanf("%d",&N);
do
{
e
=
N
%
2;
Push(&S,e);
N
=
N/2;
}
while(N);
while(Pop(&S,&e))
{
printf("%d",e);
}
printf("\n");
return
0;
}
"stdio.h"
#define
StackSize
100
typedef
int
ElemType;
typedef
struct
{
ElemType
elem[StackSize];
int
top;
}SqStack;
InitStack(SqStack
*pS)
{
pS->top=0;
/*
top指向栈顶的上一个元素
*/
}
int
Push(SqStack
*pS,ElemType
e)
{
if
(pS->top==StackSize-1)
/*
栈满
*/
return
0;
pS->elem[pS->top]=e;
pS->top=pS->top+1;
return
1;
}
int
Pop(SqStack
*pS,ElemType
*pe)
{
if
(pS->top==0)
/*
栈空
*/
return
0;
pS->top
=
pS->top
-
1;
*pe
=
pS->elem[pS->top];
return
1;
}
main()
{
SqStack
S;
ElemType
e;
int
N;
InitStack(&S);
printf("请输入一个十进制整数:\n");
scanf("%d",&N);
do
{
e
=
N
%
2;
Push(&S,e);
N
=
N/2;
}
while(N);
while(Pop(&S,&e))
{
printf("%d",e);
}
printf("\n");
return
0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询