zoukankan      html  css  js  c++  java
  • 数制转换

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

  • 相关阅读:
    Bandicam班迪录屏 高清录制视频软件
    理解WebKit和Chromium: 浏览器综述
    GDAL对于raw数据支持的一个bug
    关于GDAL计算图像坐标的几个问题
    理解WebKit和Chromium: WebKit资源加载机制
    关于web服务器架构的思考
    使用PROJ4库将地心直角坐标(XYZ)转为地心大地坐标(BLH)
    Java数据类型和MySql数据类型对应表
    理解WebKit和Chromium: 基于Chromium内核的Android WebView
    【Unity探究】物理碰撞实验
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2035721.html
Copyright © 2011-2022 走看看