• 数制转换


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

  • 相关阅读:
    获取网页数据
    追踪公式引用的单元格
    loadRunner函数之lr_set_debug_message
    Python爬虫之抓取豆瓣影评数据
    Python爬虫之抓图
    loadRunner函数之web_add_header
    JVM是如何处理异常的
    1. JVM内存区块
    JVM-JVM是如何执行方法调用的
    JVM-内部类分析
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2035721.html
Copyright © 2020-2023  润新知