• 数制转换


    #include<stdio.h>
    #include<malloc.h>
    #define ERROR 0
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define EQUAL 1
    #define OVERFLOW -1
    #define STACK_INIT_SIZE 100
    #define STACKINCREMENT 10
    typedef int SElemType;
    typedef int Status;
    struct STACK
    {     
      SElemType num;    
      SElemType *base;
      SElemType *top;
      int stacksize;
    };
    typedef struct STACK SqStack;
    typedef struct STACK *pSqstack;
    Status InitStack(SqStack  **S);  
    Status DestroyStack(SqStack *S);  
    Status ClearStack(SqStack *S);  
    Status StackEmpty(SqStack S);   
    int StackLength(SqStack S);  
    Status GetTop(SqStack S,SElemType *e);  
    Status Push(SqStack *S,SElemType e);   
    Status Pop(SqStack *S,SElemType *e);   
    Status StackTraverse(SqStack S,Status (*visit)());  
    Status InitStack(SqStack **S)
    {
      (*S)=(SqStack *)malloc(sizeof(SqStack));
      (*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 DestroyStack(SqStack *S)
    {
     free(S->base); 
     free(S);
    }
    Status ClearStack(SqStack *S)
    {
      S->top=S->base;
    }
    Status StackEmpty(SqStack S)
    {
      if(S.top==S.base) return TRUE;
      else
        return FALSE;
    }
    int StackLength(SqStack S)
    {
      int i;
      SElemType *p;
      i=0;
      p=S.top;
      while(p!=S.base)
        {p++;
         i++;
        }
    }
    Status GetTop(SqStack S,SElemType *e)
    {
      if(S.top==S.base) return ERROR;
      *e=*(S.top-1);
      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 StackPrintElem(SElemType * e)
    {
      printf("%d\n",e);
    }
    Status StackTraverse(SqStack S,Status (*visit)())
    {
      while(S.top!=S.base)
         visit(--S.top);
    }
    void Conversion(){
     pSqstack S;
     SElemType e;
     int n;
     InitStack(&S);
     printf("Input a number to convert to OCT:\n");
     scanf("%d",&n);
     if(n<0)
       {
         printf("\nThe number must be over 0.");
         return;
       }
     if(!n) Push(S,0);    /*Èç¹ûnΪ0ʱ,Ó¦Êä³ö0*/
     while(n){
       Push(S,n%8);
       n=n/8;
     }
     printf("the result is:        ");
     while(!StackEmpty(*S)){
       Pop(S,&e);
       printf("%d",e);
     }
    }     
    int main()
    {
      SElemType e;
      SqStack *Sa;
      Conversion(Sa);
      getch();
      return 0;
    }</malloc.h></stdio.h>
    

  • 相关阅读:
    面向对象的相关知识
    模块的导入
    正怎表达式在爬虫里的应用
    前端~css
    CSS知识点
    初识Html
    Python之路--协程/IO多路复用
    进程
    锁 和 线程池
    操作系统-并发-线程-进程
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2035721.html
Copyright © 2020-2023  润新知