シーケンススタックは10進数からR進数への変換を実現する

7664 ワード

    2016 7 31 13:53:37
                     r  
    1N   r    ,            , N=3456,r=8N         N / 8 (  )        N  % 8(  )
    3456          432                    0                   
    432           54                     0
    54            6                      6
    6            0                       6                   
      :(345610  =(66008
            8               ,              ,         ,            8        ,                 。
          : N>0   12N0N % r    s  ,  2; N=0,  s       ,    。
    ② N / r    N
#include
#define MAXSIZE 100
#define X 2

//          
typedef struct  
{
    int data[MAXSIZE];
    int top;
}SeqStack;

//      
void initSeqStack(SeqStack * S);
int  isEmpty(SeqStack * S);
void pushStack(SeqStack * S,int element);
int  popStack(SeqStack * S,int element);
void conversion(SeqStack * S,int N);
void traverseStack(SeqStack * S);

//      
void initSeqStack(SeqStack * S)
{
    S->top = -1;
    return ;
}

//       
int isEmpty(SeqStack * S)
{
    if(-1 == S->top)
        return 1;
    else
        return 0;
}

//       
int  isFull(SeqStack * S)
{
    if(MAXSIZE == S->top)
        return 1;
    else
        return 0;
}

//        
void pushStack(SeqStack * S,int element)
{
    if( isFull(S) )
    {
        printf("   ,      !
"
); return ; } else { S->top++; S->data[S->top] = element; /* S->data[++S->top] = element; */ return; } } // void popStack(SeqStack * S,int * element) { if( isEmpty(S) ) { printf(" , !
"
); return ; } else { *element = S->data[S->top]; S->top--; return ; } } // void conversion(SeqStack * S,int number) { int restNumber; /* while(restNumber) { restNumber = number%X; pushStack(S,restNumber); restNumber = restNumber/X; } */ do { restNumber = number%X; pushStack(S,restNumber); number = number/X; } while (number); return; } // void traverseStack(SeqStack * S) { int i; int j = 0; if(isEmpty(S)) { printf(" !
"
); return; } for(i = S->top;i>=0;i--) { if(!(j%4)) printf(" "); printf("%d",S->data[i]); j++; } return ; } // int main(void) { SeqStack S; int number; initSeqStack(&S); printf(" :
"
); scanf("%d",&number); conversion(&S,number); printf(" %d :",X); traverseStack(&S); printf("
"
); return 0; }