#define STACK_INIT_SIZE 8
#define STACKINCRMENENT 4
#include <stdio.h>
#include<math.h>
typedef struct{
int *base;
int *top;
int stacksize;
}sqstack;
typedef sqstack *stacklist;
stacklist p;
stacklist initstack (stacklist p)
{
p->base=(int *)malloc(STACK_INIT_SIZE *sizeof(int));
if(!p->base)
{
printf("overflow!\n");
exit(0);
}
p->top=p->base;
p->stacksize=STACK_INIT_SIZE ;
printf("succeed in applying for a room!\n");
return(p);
}
stacklist push (stacklist p,int m)
{
if(p->top-p->base>=p->stacksize)
{
p->base=(int *)realloc(p->base,(p->stacksize+STACKINCRMENENT)*sizeof(int));
if(!p->base)
{
printf("overflow leads to failure!\n");
exit(0);
}
p->top=p->base+p->stacksize;
p->stacksize+=STACKINCRMENENT;
}
*p->top++=m;
printf("\nok!\n");
return(p);
}
int stackempty(stacklist s)
{
if(s->top<=s->base)
return(0);
else
return(1);
}
int pop(stacklist x)
{
int y;
y=*--(x->top);
return(y);
}
main()
{
stacklist L;
int n,e;
L=initstack(p);
printf("please input a decimal number:");
scanf("%d",&n);
if(n<=0)
printf("error due to the wrong input!\n");
while(n)
{ push(L,n%8);
n=n/8;
}
printf("the changed number is:\n");
while(stackempty(L))
{
e=pop(L);
printf("%d",e);
}
}
正文
将十进制数转化为8进制2005-04-02 20:38:00
【评论】 【打印】 【字体:大 中 小】 本文链接:http://blog.pfan.cn/jay0518/448.html
阅读(3261) | 评论(0)
版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!
评论