資料結構–進位制(任意)轉換

酷行僧發表於2019-05-13
**

#include< stdio.h>
#include< stdlib.h>
#include < string.h>
#define N 100

typedef struct nima{
    char a; 
}data;

typedef struct hehe{
data *c;    
int u;
int max;
}arry;
void frist(arry*fist)
{
    fist->c= (data*)malloc(sizeof(data)*N);
    fist->max = N;
    fist->u = 0;
}
void push(arry*fist,char m)
{

    fist->c[fist->u].a = m;
    if(fist->u == fist->max){
        printf("full");
        exit(0);
    }
       printf("%c   ",fist->c[fist->u].a);
       fist->u++;
}
void pop(arry*fist,char *m)
{   
                   if((fist->u-1)<0){
    printf("error");
    exit(0);
    }
    fist->u--;
    *m=fist->c[fist->u].a;
}
void suan(arry*fist,char a[],int m,int d)
{   
    int j = strlen(a);
    int sum = 0;
    int dd;
    for(int i =0;i<j;i++)
    {
        dd=1;   
        for(int i_1 = j-i-1;i_1>0;i_1--)
        {
        dd=dd*m;        
        }
        if(a[i]>=`0`&&a[i]<=`9`)
        sum= sum+dd*(a[i]-`0`);
        if(a[i]>=`a`&&a[i]<=`f`)
        sum = sum+dd*(a[i]-`a`+10);
        if(a[i]>=`A`&&a[i]<=`F`)
        sum= sum+dd*(a[i]-`A`+10);
    }
    printf("%d
",sum);
    int mm;
    char cc;
    while(sum)
    {
        mm = sum%d;
        if(mm>=10)
        {
        cc = `a`+mm-10; 
        }
        else
        {
        cc = `0`+mm-0;
        }
        push(fist,cc);
        sum = sum/d;
    }
    printf("%d
",fist->u);
    int jjj = fist->u;
    for(int i=0;i<jjj;i++)
    {

        pop(fist,&cc);
        printf("%c",cc);
    }
    printf("
");
}
int main()
{
    arry fist;
    frist(&fist);
    char aa[N];
    int i;
    int d;
    scanf("%s",aa);
    printf("進位制
") ;
    scanf("%d",&i);
    printf("換
");
    scanf("%d",&d);
    suan(&fist,aa,i,d);
    return 0;
}

**

相關文章