| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 376 人关注过本帖
标题:switch语句问题
只看楼主 加入收藏
JimmyZeng21
Rank: 2
来 自:广东广州
等 级:论坛游民
帖 子:15
专家分:10
注 册:2011-11-1
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:3 
switch语句问题
#include<stdio.h>
void main()
{
    int num;
    int indiv,ten,hundred,thousand;
    int ten_thousand,hundred_thousand,place;
    printf("请输入一个整数1-99999:");
    scanf("%d\n",&num);
    if(num>99999)
    place=6;
    else if (num>9999)
    place=5;
    else if(num>999)
    place=4;
    else if(num>99)
    place=3;
    else if(num>9)
    place=2;
    else
    place=1;
    printf("每位数字为:%d\n",place);
    hundred_thousand=num/100000;
    ten_thousand=(num-hundred_thousand*10000)/10000;
    thousand=(num-num*hundred_thousand-num*ten_thousand)/1000;
    hundred=(num-num*hundred_thousand-num*ten_thousand-thousand*1000)/100;
    ten=(num-num*hundred_thousand-num*ten_thousand-thousand*1000-hundred*100)/10;
    indiv=num-num*hundred_thousand-num*ten_thousand-thousand*1000-hundred*100-ten*10;
    switch(place)
    {
        case 1:printf("%d",indiv);
               printf("反序数字为:%d\n",indiv);
               break;
        case 2:printf("%d,%d\n",indiv,ten);
               printf("反序数字为:%d,%d\n",ten,indiv);
               break;
        case 3:printf("%d,%d,%d\n",indiv,ten,hundred);
               printf("反序数字为:%d,%d,%d\n",hundred,ten,indiv);
               break;
        case 4:printf("%d,%d,%d,%d\n",indiv,ten,hundred,thousand);
               printf("反序数字为:%d,%d,%d,%d\n",thousand,hundred,ten,indiv);
               break;
        case 5:printf("%d,%d,%d,%d,%d\n",indiv,ten,hundred,thousand,ten_thousand);
               printf("反序数字为:%d,%d,%d.%d,%d\n",ten_thousand,thousand,hundred,ten,indiv);
               break;
        case 6:printf("%d,%d,%d,%d,%d,%d\n",indiv,ten,hundred,thousand,ten_thousand,hundred_thousand);
               printf("反序数字为:%d,%d,%d,%d,%d,%d\n",hundred_thousand,ten_thousand,thousand,hundred,ten,indiv);
               break;
        default:printf("not find .\n");
    }
}
   
        
        这是一个:给出一个不多于5位的正整数,要求它是几位数,分别打出每一位数字,按倒序打出各位数字,例如984变成489.上面是我写的,但是不知道错在哪了,莱鸟的我第一打这么长的,辛苦。
搜索更多相关主题的帖子: include 
2011-11-17 22:10
zxc1989
Rank: 2
等 级:论坛游民
帖 子:15
专家分:45
注 册:2011-11-15
收藏
得分:10 
#include<stdio.h>
#include<math.h>
void main()
{
    int num;
    int indiv,ten,hundred,thousand;
    int ten_thousand,hundred_thousand,place;
    printf("请输入一个整数0-99999:");
    scanf("%d\n",&num);
    if(num>9999)
    place=5;
    else if (num>999)
    place=4;
    else if(num>99)
    place=3;
    else if(num>9)
    place=2;
    else place=1;
    printf("位数:%d\n",place);
    printf("每位数字为:");
    hundred_thousand=num/100000;
    ten_thousand=(num-hundred_thousand*10000)/10000;
    thousand=(num-num*hundred_thousand-num*ten_thousand)/1000;
    hundred=(num-num*hundred_thousand-num*ten_thousand-thousand*1000)/100;
    ten=(num-num*hundred_thousand-num*ten_thousand-thousand*1000-hundred*100)/10;
    indiv=num-num*hundred_thousand-num*ten_thousand-thousand*1000-hundred*100-ten*10;
    switch(place)
    {
        case 1:printf("%d",indiv);
               printf("反序数字为:%d\n",indiv);
               break;
        case 2:printf("%d,%d\n",ten,indiv);
               printf("反序数字为:%d,%d\n",indiv,ten);
               break;
        case 3:printf("%d,%d,%d\n",hundred,ten,indiv);
               printf("反序数字为:%d,%d,%d\n",indiv,ten,hundred);
               break;
        case 4:printf("%d,%d,%d,%d\n",thousand,hundred,ten,indiv);
               printf("反序数字为:%d,%d,%d,%d\n",indiv,ten,hundred,thousand);
               break;
        case 5:printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,hundred,ten,indiv);
               printf("反序数字为:%d,%d,%d.%d,%d\n",indiv,ten,hundred,thousand,ten_thousand);
               break;
        default:printf("not find .\n");
    }
}
用语言描述太麻烦了,而且还不好描述,写出正确程序供你参考一下,仔细的与你的程序对照一下,看看那些地方不对,研究一下。不会在追问ok?
2011-11-17 23:09
北魂狼
Rank: 2
等 级:论坛游民
帖 子:4
专家分:15
注 册:2011-4-29
收藏
得分:10 
#include<stdio.h>
 
main(void)
{
    int num,sum=0;
    printf("请输入一个整数1-99999:\n");
   
    scanf("%d",&num);
    while(num)
    {
        printf("逆序后是:%d",num%10);
        num /=10;
        sum ++;
       printf("\n");
    }
    printf("整数的位数 :%d\n",sum);
}
可以修改如上,可以实现上述功能
   
2011-11-17 23:12
北魂狼
Rank: 2
等 级:论坛游民
帖 子:4
专家分:15
注 册:2011-4-29
收藏
得分:0 
#include<stdio.h>

main(void)
{
    int num,sum=0;
    printf("请输入一个整数1-99999:\n");
   
    scanf("%d",&num);
    printf("逆序后是:\n");

    while(num)
    {
        printf("%d",num%10);
        num /=10;
        sum ++;
       printf("\n");
    }
    printf("整数的位数 :%d\n",sum);
}
可以修改如上,可以实现上述功能
   
2011-11-17 23:13
快速回复:switch语句问题
数据加载中...
 
   



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

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