写出利用栈将一个十进制整数转化为二进制数的算法

来源:百度知道 编辑:UC知道 时间:2024/05/20 09:07:04
我们的作业题~~谢谢大家帮忙~~~

#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define LISTINCREMENT 10
#define INFEASIBLE -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int SElemType;
typedef int Status;
typedef int ElemType;
typedef struct SqStack
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;

Status InitStack(SqStack &S);
Status Push(SqStack &S,SElemType e);
Status Pop(SqStack &S,SElemType &e);
Status StackEmpty(SqStack S);
void convert(int a);
void main()
{
int n,M,e;
SqStack S;
InitStack(S);
printf("请输入要转换的数字:");
scanf("%d",&n);
M=n;
while(M)
{
Push(S,M % 2);
M = M / 2;
}
printf("转换为二进制数为:");
while(!StackEmpty(S))
{
Pop(S,e);
printf("%d",e);