| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 762 人关注过本帖, 1 人收藏
标题:c语言进制转换问题,感觉没啥问题为什么结果不对啊!
取消只看楼主 加入收藏
小杨姓张
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-12-4
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:1 
c语言进制转换问题,感觉没啥问题为什么结果不对啊!
#include<stdio.h>                                        //头文件

void TtoT(char num1);                                    //10 to 2 函数声明
void TtoE(char num2);                                    //10 to 8 函数声明
void TtoS(char num3);                                    //10 to 16 函数声明
                                    

void main()                                                //主函数
{   
    char cs,num;                                    //定义变量cs num
    while(1)                                            //死循环
    {
        printf("10 TO 2     请按1\n");                    //输出10 TO 2  请按1
        printf("10 TO 8     请按2\n");                    //输出10 TO 8  请按2
        printf("10 TO 16    请按3\n");                    //输出10 TO 16  请按3
        scanf("%d",&cs);                                //输入cs的值
        switch(cs)                                        //switch语句
        {
            case 1:
                printf("请输入要转换的十进制数:");        //输出请输入要转换的十进制数:
                scanf("%d",&num);                        //输入数字
                TtoT(num);                                //调用函数TtoT
                break;                                    //跳出switch语句
            case 2:
                printf("请输入要转换的十进制数:");     //输出请输入要转换的十进制数:
                scanf("%d",&num);                        //输入数字
                TtoE(num);                                //调用函数TtoE
                break;                                    //跳出switch语句
            case 3:
                printf("请输入要转换的十进制数:");     //输出请输入要转换的十进制数:
                scanf("%d",&num);                        //输入数字
                TtoS(num);                                //调用函数TtoS
                break;                                    //跳出switch语句
        }
        printf("\n");
        printf("\n");                                    //换行   目的是第二次进入switch之前换行避免与上一次重行
    }
}

void TtoT(char num1)                                        //函数TtoT
{
    int    n;
    char a;                                            //定义变量
    char b[100];                                            //定义数组
    for(n=0;num1!=0;n++)                                //for循环
    {
        a = num1/2;                                        //取商
        b[n] = num1%2;                                    //取余
        num1 = a;                                        //将a的值给num1
    }
    printf("10 TO 2 值:");                                //输出10 TO 2 值:
    for(;n+1>0;n--)                                        //for循环
    {
        printf("%d",b[n]);                              //输出结果
    }
}

void TtoE(char num2)                                    //函数TtoE
{
    int  n;                                            //定义变量
    char a,b[100];                                        //定义数组
    for(n=0;num2!=0;n++)                                //for循环
    {
        a = num2/8;                                        //取商
        b[n] = num2%8;                                    //取余
        num2 = a;                                        //将a的值给num2
    }
    printf("10 TO 8 值:");                                //输出10 TO 8 值:
    for(;n+1>0;n--)                                        //for循环
    {
        printf("%d",b[n]);                                //输出结果   
    }
}

void TtoS(char num3)                                    //函数TtoS
{
    int    n,d;                                            //定义变量
    char a,b[100],c[]={'A','B','C','D','E','F'};        //定义数组
    for(n=0;num3!=0;n++)                                //for循环
    {
        a = num3/16;                                    //取商
        b[n] = num3%16;                                    //取余
        if(b[n]>9)                                        //判断b[n]是否大于9
        {
            d=b[n]-10;                                    //取差值
            b[n]=c[d];                                    //将c对应的值给b
        }
        num3 = a;                                        //将a的值给num3
    }
    printf("10 TO 16 值:0x");                            //输出10 TO 16 值:0x
    for(;n+1>0;n--)                                        //for循环
    {
        if(b[n]>9)                                        //判断b[n]是否大于9
        {
            printf("%c",b[n]);                            //输出结果
        }
        else
        {
            printf("%d",b[n]);                            //输出结果
        }
    }
}
搜索更多相关主题的帖子: include c语言 
2016-12-04 23:06
小杨姓张
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-12-4
收藏
得分:0 
回复 2楼 九转星河
感谢大神!!!改好了!!!改了好久原来是n出了问题!祝大神好运!
2016-12-05 12:26
快速回复:c语言进制转换问题,感觉没啥问题为什么结果不对啊!
数据加载中...
 
   



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

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