文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C语言实例12:SWITCH语句

C语言实例12:SWITCH语句

时间:2010-06-19  来源: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;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载