# include <stdio.h>
# include <malloc.h>
# define STACK_INIT_SIZE 100
# define STACLINCREMENT 10
typedef struct
{
int *base;
int *top;
int stacksize;
}sqstack;
void creatstack( sqstack *s)
{
s->top=s->base=(int *) malloc (STACK_INIT_SIZE *sizeof(int));
s->stacksize=STACK_INIT_SIZE;
}
void push (int e,sqstack *s)
{
if ( (s->top-s->base )==s->stacksize)
{
s->base=(int *) realloc (s->base,(s->stacksize +
STACLINCREMENT )*sizeof(int));
s->top=s->base +s->stacksize;
s->stacksize+=
STACLINCREMENT;
}
*(s->top)=e;
++s->top;
}
int pop
(sqstack
*s)
{
int e;
--s->top;
e=*s->top;
return (e);
}
int stackempty (sqstack *s)
{
if (s->top==s->base )return (0);
else
return (1);
}
void main()
{
int N;
int R;
int t;
sqstack s;
creatstack (&s);
printf("请输入待转换的整数N,欲转进制R\n");
scanf("%d,%d",&N,&R);
printf("%d的%d进制形式为:",N,R);
while (N)
{
push (N%R,&s);
N=N/R;
}
while(stackempty(&s))
{
t=pop(&s);
if ( t <10) printf("%d",t);
else printf("%c",
t+55);
}
printf("\n");
}
[[it] 本帖最后由 石头大 于 2008-5-22 11:10 编辑 [/it]]