任何进制转换

作者在 2010-12-08 17:21:35 发布以下内容
#include <stdio.h>
#include <stdlib.h>

#define MAXS 8//
#define ADD  8//

typedef struct
{
    char *base;
    char *top;
    int stacksize;
}Sqstack;

void InitStack( Sqstack &s)
{
    s.base = (char *) malloc (MAXS*sizeof(char));
    if( !s.base )
        exit(0);
    s.top = s.base;
    s.stacksize = MAXS;
}

void Push( Sqstack &s, char temp )
{
    if( s.top-s.base>=MAXS )
    {
        s.base =(char *) realloc (s.base, (MAXS+ADD)*sizeof(char));
        if( !s.base )
            exit(0);
        s.top = s.base + s.stacksize;
        s.stacksize += ADD;
    }
    *s.top++ = temp;
}

void Pop( Sqstack &s, char &temp )
{
    if( s.top==s.base )
        return;
    temp = *--s.top;
}

int main()
{
    Sqstack s;
    InitStack(s);
    char c;
    int n, exchange;
    int temp;

    printf("input the number:");
    scanf("%d", &n);
    printf("input the number you exchange scale:");
    scanf("%d", &exchange);
    while( n )
    {
        temp= n%exchange;
        if( temp>=10 )
            Push( s, 'A'+temp-10 );
        else
            Push( s, 48+temp );
        n = n/exchange;
    }
    while( s.base != s.top )
    {
        Pop( s, c );
        printf("%c",c);
    }
    printf("\n");

    return 0;
}
程序 | 阅读 773 次
文章评论,共0条
游客请输入验证码
浏览31545次