#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <malloc.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#ifndef ERROR
#define ERROR -1
#endif
#define OVERFLOW -2
typedef int ElemType;
typedef int status;
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
typedef struct _SqSTACK{
ElemType *base;
ElemType *top;
int stackSize;
}SqSTACK;
status StackInit(SqSTACK* stack)
{
stack->base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(int));
if(!stack->base)
{
return ERROR;
}
stack->top = stack->base;
stack->stackSize = STACK_INIT_SIZE;
return OK;
}
status StackPush(SqSTACK* stack,ElemType e)
{
if(stack->top - stack->base >= stack->stackSize)
{
ElemType* preBase = stack->base;
printf("over flow! top = %p memory reallocing ...\n",stack->top);
stack->base = (ElemType*)realloc(stack->base,(stack->stackSize + STACK_INCREMENT)*sizeof(ElemType));
if(!stack->base)
{
perror("realloc");
return ERROR;
}
stack->stackSize += STACK_INCREMENT;
stack->top = stack->base + (stack->top - preBase);
}
*(stack->top) = e;
stack->top +=1;
return OK;
}
ElemType StackPop(SqSTACK* stack)
{
ElemType pop;
if(stack->base == stack->top)
{
printf("error:stack empty!\n");
return (ERROR);
}
pop = * -- stack->top;
return pop;
}
status StackDestroy(SqSTACK* stack)
{
free(stack->base);
return 0;
}
status StackIsEmpty(SqSTACK* stack)
{
if(stack->base == stack->top)
{
return TRUE;
}
return FALSE;
}
int DecNotationExchange(int dec,int N)
{
int ox = 1;
SqSTACK stack;
if(dec <1)
{
return 0;
}
StackInit(&stack);
while(dec)
{
StackPush(&stack,dec%N);
dec = dec/N;
}
if (!StackIsEmpty(&stack))
{
ox = StackPop(&stack);
}
while(!StackIsEmpty(&stack))
{
ox = ox *10 + StackPop(&stack);
}
StackDestroy(&stack);
return ox;
}
进制转换
最新推荐文章于 2020-11-06 17:21:35 发布

1万+

被折叠的 条评论
为什么被折叠?



