//_12_switch语句
//_12_main.cpp
/*给出一个不多于6位的正整数,要求:
1、求它是几位数
2、分别打印每一位数字
3、按逆序打印出各位数字,例如,原数为489,应输出984
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;//要输入的数据
//下面定义的变量分别代表个位十位百位千位万位十万位以及位数
int indiv,ten,hundred,thousand;
int ten_thousand,hundred_thousand;
int place_number;
printf("输入一个整数(0~999999):");
scanf("%d",&number);
while(number<0 || number>999999)//保证输入的为非负整数
{
int num;
printf("请重新输入整数(0~999999):");
scanf("%d",&num);
number = num;
}
if(number > 99999)
place_number = 6;
else if(number > 9999)
place_number = 5;
else if(number > 999)
place_number = 4;
else if(number > 99)
place_number = 3;
else if(number > 9)
place_number = 2;
else
place_number = 1;
//求出number在各个位上的值
hundred_thousand = number/100000;
ten_thousand = (number - hundred_thousand*100000)/10000;
thousand = (number - hundred_thousand*100000
- ten_thousand*10000)/1000;
hundred = (number - hundred_thousand*100000
- ten_thousand*10000 - thousand*1000)/100;
ten = (number - hundred_thousand*100000 - ten_thousand*10000
- thousand*1000 - hundred*100)/10;
indiv = number - hundred_thousand*100000 - ten_thousand*10000
- thousand*1000 - hundred*100 - ten*10;
//判断变量number的位数,并根据位数做出相应的输出
switch(place_number)
{
case 1:
printf("%d\n",indiv);
printf("反序数字为:");
printf("%d\n",indiv);
break;
case 2:
printf("%d%d\n",ten,indiv);
printf("反序数字为:");
printf("%d\n",indiv,ten);
break;
case 3:
printf("%d%d%d\n",hundred,ten,indiv);
printf("反序数字为:");
printf("%d%d%d\n",indiv,ten,hundred);
break;
case 4:
printf("%d%d%d%d\n",thousand,hundred,ten,indiv);
printf("反序数字为:");
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("反序数字为:");
printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand);
break;
case 6:
printf("%d%d%d%d%d%d\n",hundred_thousand,ten_thousand,thousand,hundred,ten,indiv);
printf("反序数字为:");
printf("%d%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand,hundred_thousand);
break;
default:
printf("Not find.\n");
}
system("pause");
return 0;
}
12_switch语句
最新推荐文章于 2022-04-23 20:55:28 发布