求C预言大神帮忙解释一下代码。 每行都解释,,在线等。

#include <stdio.h>
#include "stack.h"

void dtob(int n)
{
stack s;
init(&s);
while(n!=0)
{
push(&s, n%2);
n/=2;
}
while(!isEmpty(&s))
{
printf("%d",pop(&s));
}
printf("\n");
}

void main()
{
dtob(56);
}
//stack.h
#define MAX 100

typedef struct
{
int data[MAX];
int top;
}stack;

/*初始化栈,把指针置于栈顶*/
void init(stack *s)
{
s->top=0;
};
/*判断栈是否为空
栈为空返回1
栈不为空返回0
*/
int isEmpty(stack *s)
{
if(s->top == 0)
return 1;
else
return 0;
}

/*向栈中压入一个值*/
void push(stack *s, int v)
{
if(s->top == MAX)
{
printf("stack is full!");
}
else
{
s->data[s->top] = v;
s->top++;
}
}

/*从栈中弹出一个值*/
int pop(stack *s)
{
if(isEmpty(s))
{
printf("stack is empty!");
return -1;
}
else
{
s->top--;
return s->data[s->top];
}
}

#包括<一>。H
#包括栈”。“
无效的dtob(int n)
{
堆栈的;
init(&);
而(n!0。
{
推(& S,N % 2);
N / = 2;
}
什么时候。是空的(&))
{
printf(“%d”,流行(&));
}
printf(“\n”);
}
无效的main()
{
dtob(56);
}
/ / H堆栈。
#定义最大100
typedef struct
{
int数据[最高];
国际上;
}栈;
/*初始化栈,把指针置于栈顶*/
初始化链表(堆栈的)
{
的S - > = 0;
};
/*判断栈是否为空
栈为空返回1
栈不为空返回0
* /
INT是空的(堆栈的)
{
如果(S -> = = 0)
返回1;
其他的
返回0;
}
向栈中压入一个值* / / *
无效的推(堆栈*,int V)
{
如果(S -> = = max)
{
printf(“栈满!”);
}
其他的
{
的S - >数据[顶] = V - >;
的S - >高级+ +;
}
}
从栈中弹出一个值* / / *
国际流行(堆栈的)
{
如果(是空的(S))
{
printf(“堆栈为空!”);
返回1;
}
其他的
{
的S - >上—;
返回s >数据[ ->顶];
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-12
#include <stdio.h>
#include "stack.h"
//将十进制数n 转换成 二进制输出
void dtob(int n)
{
stack s;//定义一个栈s
init(&s);//初始化栈s
//转换主循环
while(n!=0)
{
push(&s, n%2);//从低位到高位,逐个求出并压入栈s
n/=2;
}
while(!isEmpty(&s))
{
printf("%d",pop(&s));//将所有位出栈,顺序则为高位到低位
}
printf("\n");
}

void main()
{
dtob(56);
}本回答被网友采纳
相似回答