栈的应用——十进制数转其他进制

/**************************************
*		Number Converion(From decimal to others)
* Author: Yannis Zhao
* Date: 2014-8-15
*
***************************************/

#include <stdio.h>
#include <malloc.h>

/**********************Data Object Structure********************/
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
#define ElemType int
typedef struct
{
	ElemType *base;//Stack bottom pointer
	ElemType *top;//Stack top pointer
	int capacity;//Stack capacity
}SqStack;

#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define null -65535

/***********************Operations******************************/
int InitStack(SqStack* stack);
void DestroyStack(SqStack* stack);
void ClearStack(SqStack* stack);
int Empty(SqStack stack);
int Size(SqStack stack);
ElemType PeekTop(SqStack stack);
int Push(SqStack* stack,ElemType e);
ElemType Pop(SqStack* stack);
void PrintStack(SqStack stack);
void PrintlnStack(SqStack stack);

int InitStack(SqStack* stack)
{
	stack->base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));//Default size
	
	if(!stack->base)//Malloc failed
		return OVERFLOW;
	
	stack->top=stack->base;
	stack->capacity=STACK_INIT_SIZE;
	
	return TRUE;
}

void DestroyStack(SqStack* stack)
{
	free(stack->base);
	stack->base=NULL;
	stack->top=NULL;
	stack->capacity=0;
}

void ClearStack(SqStack* stack)
{
	stack->top=stack->base;
}

int Empty(SqStack stack)
{
	if(stack.top==stack.base)
		return TRUE;
	
	return FALSE;
}

int Size(SqStack stack)
{
	return stack.top-stack.base;
}

ElemType PeekTop(SqStack stack)
{
	if(stack.top==stack.base)//Empty
		return null;
	
	return *(stack.top-1);
}

int Push(SqStack* stack,ElemType e)
{
	if(stack->top-stack->base >= stack->capacity)//Stack is full
	{
		stack->base=(ElemType*)realloc(stack->base, (STACK_INIT_SIZE+STACK_INCREMENT)*sizeof(ElemType) );
		if(!stack->base)
			return OVERFLOW;
		
		stack->top=stack->base+stack->capacity;
		stack->capacity+=STACK_INIT_SIZE;
	}
	
	*stack->top++=e;//Set value and move to next position
	
	return TRUE;
}

ElemType Pop(SqStack* stack)
{
	if(stack->top==stack->base)//Empty
		return null;
	
	return *--stack->top;
}

void PrintStack(SqStack stack)
{
	ElemType* p=NULL;
	
	if(!stack.base)//Not Exist
	{
		printf("null\n");
		return;
	}
	
	if(stack.base==stack.top)//Empty
	{
		printf("null");
		return;
	}
	
	p=stack.base;
	while(p<stack.top-1)
	{
		printf("%d ",*p++);
	}
	printf("%d",*p);
}

void PrintlnStack(SqStack stack)
{
	ElemType* p=NULL;
	
	if(!stack.base)//Not Exist
	{
		printf("null\n");
		return;
	}
	
	if(stack.base==stack.top)//Empty
	{
		printf("null\n");
		return;
	}
	
	p=stack.base;
	while(p<stack.top-1)
	{
		printf("%d ",*p++);
	}
	printf("%d\n",*p);
}

int main(int argc , char** argv)
{
	int n;
	int r;//radix
	SqStack stack;
	
	InitStack(&stack);
	
	printf("Enter radix:");
	scanf("%d",&r);
	while(r!=2 && r!=8 &&r!=10 && r!=16)
	{
		printf("Input not valid.\n");
		printf("Enter radix:");
		scanf("%d",&r);
	}
	
	printf("Enter a positive decimal interger:");
	scanf("%d",&n);
	while(n<0)
	{
		printf("You must enter a positive number.\n");
		printf("Enter a positive decimal interger:");
		scanf("%d",&n);
	}
	
	while(n)//Claculate and push
	{
		Push(&stack,n%r);
		n=n/r;
	}
	
	if(16==r)//hexadecimal
	{
		while(!Empty(stack))//Pop
		{		
			if(PeekTop(stack)>9)
			{
				printf("%c",Pop(&stack)+55);
			}
			else
				printf("%d",Pop(&stack));
		}
	}
	
	while(!Empty(stack))//Pop
	{		
		printf("%d",Pop(&stack));
	}
	printf("\n");
	
	return 0;
}
参考:《数据结构》清华大学出版社 严蔚敏 吴伟民
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值