2012. 7. 13. 14:40

#include <stdio.h>

#define MAX 10

void init_stack( int* top )
{
        *top = -1;
}

void push( char data, char stack[], int* top )
{
        if( *top >= MAX - 1 )
        {
                puts( "Stack Overflow" );
                return;
        }

        stack[ ++(*top) ] = data;
}

char pop( char stack[], int* top )
{
        if( *top < 0 )
        {
                puts( "Stack Underflow" );
                return 0;
        }

        return ( stack[ (*top)-- ] );
}

void convert( int num, int dec)
{
        
        int  top;
        char stack[MAX];

        char mod[17] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','\0'};

        init_stack( &top );

        while( num >= dec )
        {
                push( mod[ num % dec ], stack, &top );
                num      = num / dec;
        }

        push( mod[num], stack, &top );
        
        printf("%2d진수 : ", dec);
        
        while(top >= 0)
                printf("%c ", pop( stack, &top ));

        printf("\n");        
}

int main( void )
{
        int  num;

        printf("정수를 입력하세요 .. : ");
        scanf("%d", &num);

        convert( num, 2);
        convert( num, 8);
        convert( num, 16);

        return 0;
}

Posted by 몰라욧