美文网首页
利用栈实现十进制数与其他进制数进行转换

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

作者: crabor | 来源:发表于2018-06-20 20:41 被阅读0次

    源代码

    #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;
    }
    
    

    相关文章

      网友评论

          本文标题:利用栈实现十进制数与其他进制数进行转换

          本文链接:https://www.haomeiwen.com/subject/gyxfyftx.html