#include <stdlib.h>
#include<stdio.h>
#include <malloc.h>
#define stack_init_size 100
#define stackincrement 10
typedef struct sqstack
{
int *base;
int *top;
int STACKSIZE;
} sqstack;
int StackInit(sqstack *s)
{
s->base=(int *)malloc(stack_init_size *sizeof(int));
if(!s->base)
return 0;
s->top=s->base;
s->STACKSIZE=stack_init_size;
return 1;
}
int Push(sqstack *s,int e)
{
if(s->top-s->base>=s->STACKSIZE)
{
s->base=(int *)realloc(s->base,(s->STACKSIZE+stackincrement)*sizeof(int)); if(!s->base)
return 0;
s->top=s->base+s->STACKSIZE;
s->STACKSIZE+=stackincrement;
}
*(s->top++)=e;
return e;
}
int Pop(sqstack *s,int e)
{
if(s->top==s->base)
return 0;
e=*--s->top;
return e;
}
int stackempty(sqstack *s)
{
if(s->top==s->base)
{
return 1;
}
else
{
return 0;
}
}
int conversion(sqstack *s)
{
int n,e=0,flag=0;
printf("输入要转化的十进制数:\n");
scanf("%d",&n);
printf("要转化为多少进制:\n"); scanf("%d",&flag);
printf("将十进制数%d 转化为%d 进制是:\n",n,flag);
while(n)
{
Push(s,n%flag);
n=n/flag;
}
while(!stackempty(s))
{
e=Pop(s,e);
switch(e)
{
case 10: printf("A");
break;
case 11: printf("B");
break;
case 12: printf("C"); break;
case 13: printf("D"); break;
case 14: printf("E"); break;
case 15: printf("F"); break;
default: printf("%d",e); }
}
printf("\n");
return 0;
}
int main()
{
sqstack s;
StackInit(&s);
conversion(&s);
return 0;
}
标签:
整数
栈
基本操作
十进制
转化
进制
上传时间:
2016-12-08
上传用户:爱你198