利用栈实现十进制数与其他进制数进行转换

源代码

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

#define STACK_INIT_SIZE  100
#define STACK_INCREMENT  10

#define N 128

typedef unsigned StackElement;
typedef enum{NO=0,YES=1}Bool;
typedef enum{ERROR=0,OK=1}Status;

typedef struct Stack{
  StackElement* pBase;
  StackElement* pTop;
  int stackSize;
}Stack;

Status ConstructStack(Stack* pStack);
Status DestructStack(Stack* pStack);
Status ClearStack(Stack* pStack);
Bool   IsStackEmpty(Stack* pStack);
Bool   IsStackFull(Stack* pStack);
int    LengthOfStack(Stack* pStack);
Status GetTopOfStack(Stack* pStack,StackElement* pElement);
Status PushIntoStack(Stack* pStack,StackElement* pElement);
Status PopFromStack(Stack* pStack, StackElement* pElement);
Status TraverseStack(Stack* pStack,Status (*pVisitFunction)());

Status PrintStackElement(StackElement);
Status ConvertNumber(unsigned decimal,unsigned base,char digitString[],int maxLength);

Status ConvertNumber(unsigned decimal, unsigned base, char digitString[],int maxLength){
  unsigned n,e;
  Stack stack;
  int i;

  if(base<2||base>36){
    return ERROR;
  }
  if(maxLength<=1){
    return ERROR;
  }
  n=decimal;
  if(n==0){
     digitString[0]='0';
     digitString[1]='\0';
     return OK;
  }
  if(ConstructStack(&stack)==ERROR){
     return ERROR;
  }
  while(n!=0){
    e=n%base;
    if(PushIntoStack(&stack,&e)==ERROR){
       return ERROR;
    }
    n/=base;
  }
  i=0;
  while(IsStackEmpty(&stack)==NO){
    if(PopFromStack(&stack,&e)==ERROR){
       return ERROR;
    }
    if(e>=0 && e<=9){
       digitString[i]=e+'0';
    }
    else if(e>=10 && e<=36){
      digitString[i]=e-10+'A';
    }
    else{
       return ERROR;
    }
    i++;
    if(i>=maxLength){
       return ERROR;
    }
  }
  DestructStack(&stack);
  digitString[i]='\0';
  return OK;
}

Status PrintStackElement(StackElement e){
  if(sizeof(e)!=sizeof(unsigned)){
    return ERROR;
  }
  printf("%d\t",e);
  return OK;
}

Status ConstructStack(Stack* pStack){
   pStack->pBase=(StackElement*)malloc(STACK_INIT_SIZE*sizeof(StackElement));
   if(pStack->pBase==NULL){
      return ERROR;
   }
   pStack->pTop=pStack->pBase;
   pStack->stackSize=STACK_INIT_SIZE;
   return OK;
}

Status DestructStack(Stack* pStack){
  free(pStack->pBase);
  pStack->pBase=NULL;
  pStack->pTop=NULL;
  pStack->stackSize=0;
  return OK;
}

Status ClearStack(Stack* pStack){
  pStack->pTop=pStack->pBase;
  return OK;
}

Bool IsStackEmpty(Stack* pStack){
  if(pStack->pTop==pStack->pBase){
     return YES;
  }
  else{
     return NO;
  }
}

Bool IsStackFull(Stack* pStack){
  if(pStack->pTop-pStack->pBase>=pStack->stackSize){
    return YES;
  }
  else{
    return NO;
  }
}

int LengthOfStack(Stack* pStack){
   return pStack->pTop-pStack->pBase;
}

Status PushIntoStack(Stack* pStack,StackElement* pElement){
  if(IsStackFull(pStack)==YES){
     pStack->pBase=(StackElement*)
       realloc(pStack->pBase,(pStack->stackSize+STACK_INCREMENT)*sizeof(StackElement));
     if(pStack->pBase==NULL){
        return ERROR;
     }
     else{
        pStack->pTop=pStack->pBase+pStack->stackSize;
        pStack->stackSize+=STACK_INCREMENT;
     }
   }
   *(pStack->pTop)=*pElement;
   pStack->pTop++;
   return OK;
}

Status PopFromStack(Stack* pStack,StackElement* pElement){
  if(IsStackEmpty(pStack)==YES){
     return ERROR;
  }
  else{
    pStack->pTop--;
    *pElement=*(pStack->pTop);
    return OK;
  }
}

Status GetTopOfStack(Stack* pStack,StackElement* pElement){
  if(IsStackEmpty(pStack)){
    return ERROR;
  }
  *pElement=*(pStack->pTop-1);
  return OK;
}

int main(void){
   char a[N];
   ConvertNumber(153,2,a,N);
   puts(a);
   return 0;
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容