二进制,八进制,十六进制和十进制的转换程序

              最近用数据结构中栈的知识弄了个进制转换的程序。

              大家看看。

/*
*这个程序实现了十进制与二进制,八进制,十六进制之间的转换。
*Dec stand for decimal
*Oct stand for octonary
*Bin stand for binary
*/
/*
*author:xufeiyang
*version: v1.0
*/
#include <stdio.h>
#include <math.h>

#define MAXSIZE 16

typedef struct stack                       /*栈结构声明*/
{
    short int pool[MAXSIZE];
    short int top;
}Stack;

void InitStack(Stack *bin)        /*初始化栈,将其中值都赋值0,方便以后操作*/
{
    int i;
    for(i = 0; i < MAXSIZE; i++)
        bin->pool[i] = 0;

    bin->top = -1;
}
int IsEmpty(Stack *bin)        /*判断栈是否是空栈;若是,返回*/
{
    if(-1 == bin->top)
        return 1;
    else
        return 0;
}
int GetTopElem(Stack *sta)        /*取得栈顶元素*/
{
    return sta->pool[sta->top];
}
int Push(Stack *bin, short int x)        /*元素x入栈*/
{
    if(bin->top == MAXSIZE-1)
        return 0;
    bin->top++;
    bin->pool[bin->top] = x;
    return 1;
}
int Pop(Stack *bin, short int *x)                  /*出栈,元素存入x中返回*/
{
    if(-1 == bin->top)
        return 0;
    else
    {
        *x = bin->pool[bin->top];
        bin->top--;
        return 1;
    }
}
void DecToBin(int decnum, Stack *bin)        /*将十进制转换成二进制*/
{
    short int numinstack;            /*用来接收栈中弹出的元素*/
    Stack temp;            /*定义一个栈,用来最为二进制转换的中间栈,临时存储*/
    InitStack(&temp);

    if(0 == decnum)            /*输入的十进制数是0,则直接将0压入栈,继续往栈中压入0直至栈满*/
    {
        Push(bin, 0);
        bin->top = 15;
        return;
    }

    if(decnum > 0)            /*输入的十进制数是正数*/
    {
        while(decnum != 0)
        {
            Push(bin, decnum % 2);
            decnum /= 2;
        }
        bin->top = 15;
    }

    if(decnum < 0)            /*输入的十进制数是负数,先按正数操作,最后压1入栈,表示负号*/
    {
        decnum = -decnum;
        while(decnum != 0)
        {
            Push(bin ,decnum % 2);
            decnum /= 2;
        }
        bin->top = 14;

        while(!IsEmpty(bin))    /*转换后,temp中的数是十进制负数的反码,此时二进制正序排列*/
        {
            Pop(bin, &numinstack);
            if(1 == numinstack)
                Push(&temp, 0);
            else
                Push(&temp, 1);
        }
        temp.pool[temp.top]++;    /*栈顶元素加1,其实是为反码转换成补码做准备*/
        while(!IsEmpty(&temp))    /*将反码转换成补码*/
        {
            Pop(&temp, &numinstack);
            if(2 == numinstack)
            {
                Push(bin, 0);
                temp.pool[temp.top]++;
            }
            else
            {
                Push(bin, numinstack);
            }
        }
        Push(bin, 1);   /*最后压1入栈,表示该数是负数*/
    }
}
void DecToHex(int decnum, Stack *hex)    /*十进制转换成十六进制*/
{
    Stack bin;                /*定义一个二进制栈,十进制先转换成二进制*/
    Stack temp;
    int i = 0;
    short int tempnum;
    int sum = 0;
    InitStack(&bin);
    InitStack(&temp);
    DecToBin(decnum, &bin);

    while(!IsEmpty(&bin))        /*将bin中二进制码倒置在temp中*/
    {
        Pop(&bin, &tempnum);
        Push(&temp, tempnum);
    }

    while(!IsEmpty(&temp))
    {
        Pop(&temp, &tempnum);
        sum += tempnum * pow(2, i);
        i++;
        if(4 == i)
        {
            Push(hex, sum);
            sum = 0;
            i = 0;
        }
    }
/*    while(decnum != 0)
    {
        Push(hex, decnum % 16);
        decnum /= 16;
    }
*/
}
void DecToOct(int decnum, Stack *oct)        /*十进制转换成八进制*/
{
    Stack bin;
    Stack temp;
    int i = 0, sum = 0;
    short int tempnum;
    InitStack(&bin);
    InitStack(&temp);
    DecToBin(decnum, &bin);

    while(!IsEmpty(&bin))
    {
        Pop(&bin, &tempnum);
        Push(&temp , tempnum);
    }

    while(!IsEmpty(&temp))
    {
        Pop(&temp, &tempnum);
        sum += tempnum * pow(2, i);
        i++;
        if(3 == i)
        {
            Push(oct, sum);
            sum = 0;
            i = 0;
        }
    }
    if(decnum >= 0)
        Push(oct, 0);
    else
        Push(oct, 1);

}
int BinToDec(Stack *bin)    /*二进制转换成十进制*/
{
    int DecNum = 0;
    int i = MAXSIZE - 2;
    short int  popnum;
    Stack temp;
    InitStack(&temp);

    if(1 == GetTopElem(bin))    /*如果符号位是1*/
    {
        while(!IsEmpty(bin))    /*Exchange*/
        {
            Pop(bin, &popnum);
            Push(&temp, popnum);
        }

        temp.pool[temp.top]--;

        while(!IsEmpty(&temp))     /*fushu buma zhuanghuancheng fanma*/
        {
            Pop(&temp, &popnum);
            if(-1 == popnum)
            {
                Push(bin, 1);
                temp.pool[temp.top]--;
            }
            else
            {
                Push(bin, popnum);
            }
        }

        while(!IsEmpty(bin))      /*fanma zhuanhuancheng yuanma*/
        {
            Pop(bin, &popnum);
            if(1 == popnum)
                Push(&temp, 0);
            else
                Push(&temp, 1);
        }

        i = 0;
        while(!IsEmpty(&temp))
        {
            Pop(&temp, &popnum);
            DecNum += popnum * pow(2, i);
            i++;
        }
        return -DecNum;
    }

    Pop(bin, &popnum);    /*弹出符号位0*/
    while(!IsEmpty(bin))    /*二进制是正数*/
    {
        Pop(bin, &popnum);
        DecNum += popnum * pow(2, i);
        i--;
    }
    return DecNum;
}
void OctToBin(Stack *oct, Stack *bin)        /*八进制转换成二进制*/
{
    short int popnum;
    Stack temp;
    InitStack(&temp);

    Pop(oct, &popnum);
    Push(&temp, popnum);

    while(!IsEmpty(oct))
    {
        Pop(oct, &popnum);
        switch(popnum)
        {
            case 0:
                Push(&temp, 0);
                Push(&temp, 0);
                Push(&temp, 0);
                break;
            case 1:
                Push(&temp, 0);
                Push(&temp, 0);
                Push(&temp, 1);
                break;
            case 2:
                Push(&temp, 0);
                Push(&temp, 1);
                Push(&temp, 0);
                break;
            case 3:
                Push(&temp, 1);
                Push(&temp, 1);
                Push(&temp, 0);
                break;
            case 4:
                Push(&temp, 1);
                Push(&temp, 0);
                Push(&temp, 0);
                break;
            case 5:
                Push(&temp, 1);
                Push(&temp, 0);
                Push(&temp, 1);
                break;
            case 6:
                Push(&temp, 1);
                Push(&temp, 1);
                Push(&temp, 0);
                break;
            case 7:
                Push(&temp, 1);
                Push(&temp, 1);
                Push(&temp, 1);
                break;
        }
    }
    while(!IsEmpty(&temp))    /*将temp中的二进制码倒置压入bin栈中*/
    {
        Pop(&temp, &popnum);
        Push(bin, popnum);
    }
}

void HexToBin(Stack *hex, Stack *bin)        /*十六进制转换成二进制*/
{
    short int popnum;
    Stack temp;
    InitStack(&temp);

    while(!IsEmpty(hex))
    {
        Pop(hex, &popnum);
        switch(popnum)
        {
            case 0:
                Push(&temp, 0);
                Push(&temp, 0);
                Push(&temp, 0);
                Push(&temp, 0);
                break;
            case 1:
                Push(&temp, 0);
                Push(&temp, 0);
                Push(&temp, 0);
                Push(&temp, 1);
                break;
            case 2:
                Push(&temp, 0);
                Push(&temp, 0);
                Push(&temp, 1);
                Push(&temp, 0);
                break;
            case 3:
                Push(&temp, 0);
                Push(&temp, 0);
                Push(&temp, 1);
                Push(&temp, 1);
                break;
            case 4:
                Push(&temp, 0);
                Push(&temp, 1);
                Push(&temp, 0);
                Push(&temp, 0);
                break;
            case 5:
                Push(&temp, 0);
                Push(&temp, 1);
                Push(&temp, 0);
                Push(&temp, 1);
                break;
            case 6:
                Push(&temp, 0);
                Push(&temp, 1);
                Push(&temp, 1);
                Push(&temp, 0);
                break;
            case 7:
                Push(&temp, 0);
                Push(&temp, 1);
                Push(&temp, 1);
                Push(&temp, 1);
                break;
            case 8:
                Push(&temp, 1);
                Push(&temp, 0);
                Push(&temp, 0);
                Push(&temp, 0);
                break;
            case 9:
                Push(&temp, 1);
                Push(&temp, 0);
                Push(&temp, 0);
                Push(&temp, 1);
                break;
            case 10:
                Push(&temp, 1);
                Push(&temp, 0);
                Push(&temp, 1);
                Push(&temp, 0);
                break;
            case 11:
                Push(&temp, 1);
                Push(&temp, 0);
                Push(&temp, 1);
                Push(&temp, 1);
                break;
            case 12:
                Push(&temp, 1);
                Push(&temp, 1);
                Push(&temp, 0);
                Push(&temp, 0);
                break;
            case 13:
                Push(&temp, 1);
                Push(&temp, 1);
                Push(&temp, 0);
                Push(&temp, 1);
                break;
            case 14:
                Push(&temp, 1);
                Push(&temp, 1);
                Push(&temp, 1);
                Push(&temp, 0);
                break;
            case 15:
                Push(&temp, 1);
                Push(&temp, 1);
                Push(&temp, 1);
                Push(&temp, 1);
                break;
        }
    }
    while(!IsEmpty(&temp))        /*将temp中的二进制码倒置压入bin中*/
    {
        Pop(&temp, &popnum);
        Push(bin, popnum);
    }
}
int OctToDec(Stack *oct)        /*八进制通过二进制转换成十进制*/
{
    Stack bin;
    int DecNum;
    InitStack(&bin);
    OctToBin(oct, &bin);
    DecNum = BinToDec(&bin);
    return DecNum;
}
int HexToDec(Stack *hex)        /*十六进制通过二进制转换成十进制*/
{
    Stack bin;
    int DecNum;
    InitStack(&bin);
    HexToBin(hex, &bin);
    DecNum = BinToDec(&bin);
    return DecNum;
}
int TranslateToHex(Stack *hex, char hexpool[], int size)    /*将输入的十六进制字符,转换成十六进制数字,并存入栈hex中*/
{
    int i;
    for(i = size - 1; i >= 0; i--)
    {
        switch(hexpool[i])
        {
            case 'A':
            case 'a':
                Push(hex, 10);
                break;
            case 'B':
            case 'b':
                Push(hex, 11);
                break;
            case 'C':
            case 'c':
                Push(hex, 12);
                break;
            case 'D':
            case 'd':
                Push(hex, 13);
                break;
            case 'E':
            case 'e':
                Push(hex, 14);
                break;
            case 'F':
            case 'f':
                Push(hex, 15);
                break;
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                Push(hex, hexpool[i] - 48);
                break;
            default:
                printf("Hex number is Wrong!/n");
                return -1;
        }
    }
}
int TranslateToBin(Stack *bin, char binpool[], int size)    /*将输入的二进制字符转换成二进制数字,并存入栈bin中*/
{
    int i;
    for(i = size - 1; i >= 0; i--)
    {
        if('1' == binpool[i])
        {
            Push(bin, 1);
        }
        else if('0' == binpool[i])
        {
            Push(bin, 0);
        }
        else
        {
            printf("Bin Number is Wrong! /n");
            return -1;
        }
    }
}
int TranslateToOct(Stack *oct, char octpool[], int size)    /*将输入的八进制字符转换成八进制数字,并存入栈oct中*/
{
    int i;
    int popnum;
    if('0' != octpool[0] && '1' != octpool[0])
    {
        printf("Oct number is Wrong!/n");
        return -1;
    }
    for(i = size - 1; i >= 0; i--)
    {
        if(octpool[i] > '7' || octpool[i] < '0')
        {
            printf("Oct number is Wrong!/n");
            return -1;
        }

        Push(oct, octpool[i] - 48);
    }

}
void PrintHex(Stack *hex)        /*按十六进制形式打印*/
{
    short int tempnum;
    while(!IsEmpty(hex))
    {
        Pop(hex, &tempnum);
        switch(tempnum)
        {
        case 10:
            putchar('A');
            break;
        case 11:
            putchar('B');
            break;
        case 12:
            putchar('C');
            break;
        case 13:
            putchar('D');
            break;
        case 14:
            putchar('E');
            break;
        case 15:
            putchar('F');
            break;
        default:
            printf("%d", tempnum);
            break;
        }
    }
}
int main()
{
    Stack bin;        /*二进制栈*/
    Stack hex;    /*十六进制栈*/
    Stack oct;    /*八进制栈*/
    int DecNum;    /*输入和输出的十进制*/
    short int tempnum;    /*接收bin中弹出的数字*/
    int choice;    /*选项*/
    char binpool[MAXSIZE];    /*接收16个二进制字符*/
    char octpool[6];        /*接收6个八进制字符*/
    char hexpool[4];        /*接收4个十六进制字符*/

    clrscr();

    memset(binpool, '0', MAXSIZE);    /*将binpool内存块所有空间赋值为‘0’*/
    memset(octpool, '0', 6);
    memset(hexpool, '0', 4);

    while(1)
    {
        InitStack(&bin);
        InitStack(&hex);
        InitStack(&oct);

        printf("/n========== Menu ===========/n");
        printf("  1.---- Dec to Bin ----/n");
        printf("  2.---- Dec to Oct ----/n");
        printf("  3.---- Dec to Hex ----/n");
        printf("  4.---- Bin to Dec ----/n");
        printf("  5.---- Oct to Dec ----/n");
        printf("  6.---- Hex to Dec ----/n");
        printf("  7.---- Exit ----/n");
        putchar('/n');
        printf("Choose : ");
        scanf("%d", &choice);
        getchar();

        switch(choice)
        {
            case 1:
                clrscr();
                printf("Please input a dec Number : ");
                scanf("%d", &DecNum);
                getchar();
                DecToBin(DecNum, &bin);
                printf("Dec to Bin is : ");
                while(!IsEmpty(&bin))
                {
                   Pop(&bin, &tempnum);
                   printf("%d", tempnum);
                }
                putchar('/n');
                break;
            case 2:
                clrscr();
                printf("Enter a Dec number : ");
                scanf("%d", &DecNum);
                getchar();
                DecToOct(DecNum, &oct);
                printf("Dec to Oct is : 0o");
                while(!IsEmpty(&oct))
                {
                    Pop(&oct, &tempnum);
                    printf("%d", tempnum);
                }
                putchar('/n');
                break;
            case 3:
                clrscr();
                printf("Enter a Dec number : ");
                scanf("%d", &DecNum);
                getchar();
                DecToHex(DecNum, &hex);
                printf("Dec to Hex is : 0x");
                PrintHex(&hex);
                putchar('/n');
                break;
            case 4:
                clrscr();
                printf("Enter a 16 bit Bin number : ");
                scanf("%s", binpool);
                getchar();

                if(-1 == TranslateToBin(&bin, binpool, MAXSIZE))
                {
                    break;
                }
                DecNum = BinToDec(&bin);
                printf("Answer (Dec) is : %d/n", DecNum);
                break;
            case 5:
                clrscr();
                printf("Enter a 6 bit Oct number : ");
                scanf("%s", octpool);
                getchar();
                if(-1 == TranslateToOct(&oct, octpool, 6))
                {
                    break;
                }
                DecNum = OctToDec(&oct);
                printf("Answer (Dec) is : %d/n", DecNum);
                break;
            case 6:
                clrscr();
                printf("Enter a 4 bit Hex number : 0x");
                scanf("%s", hexpool);
                getchar();
                if(-1 == TranslateToHex(&hex, hexpool, 4))
                {
                    break;
                }
                DecNum = HexToDec(&hex);
                printf("Answer (Dec) is : %d/n", DecNum);
                break;
            case 7:
                system("PAUSE");
                return 0;
            default:
                printf("Error , Choose again !/n");
                break;
        }
    }
    putchar('/n');
}

写得比较长,主要原因是我写完以后没有对代码和算法进行优化,有兴趣的话,可以和我讨论下如何优化,呵呵

哦,忘了说用的IDE是Turbo C。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值