数据结构(C语言版)栈的实现进制的转换

/* Note:Your choice is C IDE */
#include "stdio.h"
#include<malloc.h>
#define ok 1
#define error 0
#define overflow
#define stack_init_size 50
#define stackincrement 5
typedef int status;
typedef int selemtype;
typedef struct{
	selemtype *base;
	selemtype *top;
	int stacksize;
}sqstack;
status initstack(sqstack *s){
	s->base=(selemtype *)malloc(stack_init_size*sizeof(selemtype));
	if(!s->base)exit(overflow);
	s->top=s->base;
	s->stacksize=stack_init_size;
	return ok;
}
status push(sqstack *s,selemtype e){
	if(s->top-s->base==s->stacksize){
		s->base=(selemtype *)realloc(s->base,(s->stacksize+stackincrement)*sizeof(selemtype));
		if(!s->base)exit(overflow);
		s->top=s->base+s->stacksize;
		s->stacksize+=stackincrement;
	}
	*s->top++=e;
	return ok;
}
status pop(sqstack *s,selemtype *e){
	if(s->top==s->base)return error;
	*e=*--s->top;
	return ok;
}
status stackempty_sq(sqstack s)
{
	if(s.top==s.base)
		return ok;
	else
		return error;

}
main()
{
	sqstack sa;
	int n,i,e;
	if(initstack(&sa)==ok){
		printf("init is ok!\n");
		printf("s.stacksize is %d\n",sa.stacksize);
	}
	printf("请输入需要转换的十进制数:\n");
	scanf("%d",&n);
	printf("请输入需要将该十进制数转换成何种进制数:\n");
	scanf("%d",&i);
	while(n){
		e=n%i;
		push(&sa,e);
		n=n/i;
	}
	while(!stackempty_sq(sa)){
		pop(&sa,&e);
		printf("%d",e);
	}
	printf("\n");
	system("pause");
	return 0;   
}


 

没有更多推荐了,返回首页