1. 程式人生 > >任意進位制轉換(佇列實現儲存和輸出)

任意進位制轉換(佇列實現儲存和輸出)

#include <stdio.h>
#include <stdlib.h>
#define maxsize 1000000
**//定義佇列**
typedef struct
{
    char c[maxsize];
    int top;
}seqstack;

**//判斷佇列是否為空**
int empty_seqstack(seqstack *s)
{
    if(s->top==-1)
        return 1;
    else
        return 0;
}
**//入隊**
void push_seqstack(seqstack *s,char x)
{

    if(s->top==maxsize-1)
        printf("stack is full\n");
    else
    {

        s->top++;
        s->c[s->top]=x;
    }
}
**//出隊**
void pop_seqstack(seqstack*s)
{
    char x;
    while(s->top!=-1)
    {
        x=s->c[s->top];
        printf("%c",x);
        s->top--;
    }


}
**///進位制轉換(以字元形式儲存為核心)**
void conversion(int a,int r,seqstack *s)
{
    char x;
    while(a!=0)
    {
        if(a%r<=9)
        {
            x=a%r+'0';
            push_seqstack(s,x);
        }
        else
        {
            x=a%r+'A'-10;
            push_seqstack(s,x);
        }
        a=a/r;
    }


}
**//主程式**
int main()
{
    int a,r;
    seqstack *s;
    s=(seqstack*)malloc(sizeof(seqstack));
    s->top=-1;
    printf("請輸入要轉換的數和進位制\n");
    scanf("%d %d",&a,&r);

    conversion(a,r,s);
    pop_seqstack(s);




    return 0;
}