12_switch语句

//_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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值