Chinaunix首页 | 论坛 | 博客
  • 博客访问: 845497
  • 博文数量: 321
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 936
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-23 11:25
文章分类

全部博文(321)

文章存档

2017年(1)

2016年(10)

2015年(61)

2014年(187)

2013年(62)

分类: C/C++

2014-06-23 17:11:30

原文地址:C语言实例12:SWITCH语句 作者:hnrainll


/***********
给出一个不多于5位的正整数,要求,1 求它是几位数,2 分别打印出每一位数字;3逆向打印出个数字
*************/

#include <stdio.h>

int main(int argc, char *argv[])
{
    int num;
    int indiv, ten, hundred, thousand;
    int ten_thousand, hundred_thousand,place;

    printf("请输入一个整数(0~999999):");
    scanf("%d",&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("place = %d", place);

    printf("每位数字:");

    hundred_thousand = num/100000;
    ten_thousand = (num - hundred_thousand*100000)/10000;
    thousand = (num - hundred_thousand*100000-ten_thousand*10000)/1000;
    hundred = (num - hundred_thousand*100000-ten_thousand*10000-thousand*1000)/100;
    ten = (num - hundred_thousand*100000-ten_thousand*10000-thousand*1000-hundred*100)/10;
    indiv = num - hundred_thousand*100000-ten_thousand*10000-thousand*1000-hundred*100-ten*10;

    switch(place)
    {    case 1:printf("%d", indiv);
             printf("\n反序数字为:");
             printf("%d\n",indiv);
             break;

        case 2:printf("%d,%d", ten, indiv);
             printf("\n反序数字为:");
             printf("%d%d\n",indiv, ten);
             break;
        case 3:printf("%d,%d,%d",hundred, ten, indiv);
             printf("\n反序数字为:");
             printf("%d%d%d\n",indiv, ten, hundred);
             break;
        case 4:printf("%d,%d,%d,%d", thousand, hundred,ten,indiv);
             printf("\n反序数字为:");
             printf("%d%d%d%d\n",indiv,ten,hundred,thousand);
             break;
        case 5:printf("%d,%d,%d,%d,%d", ten_thousand,thousand,hundred,ten,indiv);
             printf("\n反序数字为:");
             printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand);
             break;
        case 6:printf("%d,%d,%d,%d,%d,%d",hundred_thousand,ten_thousand,thousand,hundred,ten,indiv);
             printf("\n反序数字为:");
             printf("%d%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand,hundred_thousand);
             break;

        default:printf("NOT FIND.\n");
        break;
    }

        while(1);
    return 0;
}


阅读(636) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~