进制转换

作者在 2010-10-17 09:30:20 发布以下内容
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define LISTINCREMENT 10
#define INFEASIBLE -1
#define MAXSIZE 100
#define STACKINCREMENT 10
typedef int SElemType;
typedef int Status;
typedef int ElemType;
typedef struct SqStack
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
SqStack S;
Status InitStack();
Status Push(SElemType e);
Status Pop(SElemType *e);
Status StackEmpty();
void main()
{
int n,M,e;
SqStack S;
InitStack();
printf("请输入要转换的数字:");
scanf("%d",&n);
M=n;
while(M)
{
Push(M % 2);
M = M / 2;
}
printf("转换为二进制数为:");
while(!StackEmpty())
{
Pop(&e);
printf("%d",e);
}
printf("\n");
getch();
}
Status InitStack()
{
S.base = (SElemType *)malloc(sizeof(SElemType));
if(!S.base)
exit(OVERFLOW);
S.top = S.base;
S.stacksize=MAXSIZE;
return OK;
}
Status Pop(SElemType *e)
{
if(S.top == S.base)
return ERROR;
*e = *--S.top;
return OK;
}

Status Push(SElemType e)
{
if(S.top - S.base >= S.stacksize)
{
S.base = (ElemType *)realloc(S.base,(S.stacksize + LISTINCREMENT) * sizeof(ElemType));
if(!S.base)
exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}
Status StackEmpty()
{
if(S.top == S.base)
return OK;
else
return ERROR;
}
 
 
有没有更简短一点的算法,就是用C语言版的VC++编写的,有的话大家帮回复给WO。。。thanks everybody
默认分类 | 阅读 739 次
文章评论,共0条
游客请输入验证码
文章分类