| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 315 人关注过本帖
标题:进制转换小程序___xyz
只看楼主 加入收藏
admin_xyz
Rank: 2
等 级:论坛游民
帖 子:195
专家分:77
注 册:2012-6-24
结帖率:97.3%
收藏
已结贴  问题点数:20 回复次数:4 
进制转换小程序___xyz
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "function.h"

#define N 100

                                            //问题1:程序实现的太啰嗦了。。。求优化或给个优化的思路。
                                            //问题2:16转n进制那块,一个函数里怎么实现输出 转成2进制,转成10进制  (一个函数里实现2个功能)
int main()
{
    void  display();
    void  bin_n(char *bin_str);

    void  decimal_n (int integer);
    void  hex_n (char *hex);

    int  judge = 0;     //judge input
    int  decimal = 0;
    char  hex[N];
    char  binary[N];

/**************** DEFINE Variable *******************/

while (1)

    {

    display();

    scanf ("%d",&judge);
    system("clear");

    switch (judge)

        {
            case 1:
                printf ("Please Input Decimal: ");
                scanf ("%d", &decimal);

                decimal_n(decimal);
                break;
            case 2:
                printf ("Please Input Binary: ");
                scanf ("%s", binary);

                bin_n(binary);
                break;
            case 3:
                printf ("Please Input Hex: ");
                scanf ("%s", hex);

                hex_n (hex);
                break;
            case 0:
                break;

            default:
                printf ("\n\nError , Please Input Again !");
                break;

        }

        if (judge == 0)
            break;

    }


    return 0;
}

/************************** 上面是Main.c **************************************/
/************************** 下面是function.h************************************/
#ifndef FUNCTION_H_INCLUDED
#define FUNCTION_H_INCLUDED
#define N 100
void display ()
{
    printf ("\n\n\n\n");
    printf ("               ****************************                \n");
    printf ("               *                                           \n");
    printf ("               *  1: Input Decimal To All                  \n");
    printf ("               *  2: Input Binary  To All                  \n");
    printf ("               *  3: Input Hex     To All                  \n");
    printf ("               *                                           \n");
    printf ("               *  0: Input 0        Leave                  \n");
    printf ("               *                                           \n");
    printf ("               ***************************                 \n");

    printf ("\n\n");
    printf ("               Please Input: ");
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
int  square(int u, int n)
    {
        int  sum = 1;
        while (n)
            {
                sum *= u;
                n--;
            }
        return sum;
    }
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void  decimal_n (int integer)         //将十进制数转换成2,16进制,并输出
{
    int  *bin = calloc(N,sizeof(int));
    int  count = 0;
    printf ("\nHex == %x\n", integer);
    while (integer)        //1/2=0,1%2 = 1
        {
            *(bin++) = integer % 2;
            integer /= 2;
            count++;
       }
    printf ("Binary == ");
    while (count-- != 0)
        {
            printf ("%d ", *--bin);
        }
    free (bin);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void  bin_n (char *bin_str)
{
    int  square (int u,int n);
    int  d_sum = 0;
    int  len_bin = strlen(bin_str);
    while (*bin_str != '\0')
        {
            if (*bin_str == '1')
                {
                    d_sum += square (2, len_bin-1);
                    bin_str++;
                    len_bin--;
                }
            else
                {
                    len_bin--;
                    bin_str++;
                }
        }
    printf ("\nDecimal == %d\n", d_sum);
    printf ("Hex == %x\n", d_sum);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

void hex_n (char *hex)
{
    char  hex_table[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
    char  hex_bin_table[][5] = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
    int len_hex = strlen(hex);
    int i = 0;
    printf ("\nBinary == ");
    while (len_hex--)
        {
            for (i = 0;i < 16;i++)
                {
                    if (*hex == hex_table[i])
                        {
                            printf ("%s ",hex_bin_table[i]);
                            break;
                        }
                }
            hex++;
        }
}


#endif // FUNCTION_H_INCLUDED

2012-09-14 19:21
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:10 
膜拜大作 膜拜楼主
2012-09-14 19:23
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:10 
有个叫 strtol 的库函数,楼主不妨学学它的常规实现。应该很容易从网上找到的。
2012-09-14 20:35
admin_xyz
Rank: 2
等 级:论坛游民
帖 子:195
专家分:77
注 册:2012-6-24
收藏
得分:0 
回复 3楼 pangding
多谢版主,以前没发现有这个库。。。

总有人比你努力,可怕的是比你牛的人比你还努力。
2012-09-14 21:01
LittleCer
Rank: 2
来 自:一个小村庄
等 级:论坛游民
帖 子:19
专家分:15
注 册:2012-9-1
收藏
得分:0 
cool~
2012-09-14 21:26
快速回复:进制转换小程序___xyz
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020147 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved