c语言 进制转换

Modified on: Tue, 06 Aug 2019 18:46:49 +0800 热度: 805 度
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
typedef struct stack{
    int *base;
    int top;
    int max;
}stack;

void InitStack(stack *s,int size)
{
    s->base=(int *)malloc(sizeof(int)*size);
    s->max=size;
    s->top=-1;
}

void pop(stack *s,int elem)
{
    if(s->top>=s->max) return;
    memcpy(&s->base[++(s->top)],&elem,sizeof(char));
}
void push(stack *s,int *elem)
{
    if(s->top<=-1) return;
    memcpy(&*elem,&s->base[(s->top)--],sizeof(char));
}

int empty(stack s)
{
    if(s.top==-1)
        return 1;
    return 0;
}

void traverser(stack s)
{
    int i;
    int len=s.top;
    for(i=0;i<=len;i++)
    {
        printf("%d\n",s.base[i]);
    }
}

int main(int argc, char *argv[]) {
    stack orignal_num;
    int N; 
    char sign[37]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int system_num;
    int temp;
    InitStack(&orignal_num,100);
    printf("请输入一个非负的十进制的数字:");
    scanf("%d",&N);
    printf("请输入小于36的进制数:");
    scanf("%d",&system_num);
    
    while(N)
    {
        pop(&orignal_num,N%system_num);
        if(system_num==1)
        {
            N--;
        }else
            N/=system_num;
    }
    while(!empty(orignal_num))
    {
        push(&orignal_num,&temp);
        printf("%c",sign[temp]);
    }
    return 0;
}

添加新评论