switch

本文介绍了一个使用C语言编写的程序,该程序能够接收用户输入的一个六位以内的整数,并输出每个位上的数字及其反序排列。通过一系列算术操作确定每一位的数字,并根据数字位数使用switch语句实现不同长度数字的正确格式输出。

/* 表达式和一个整数,或一个字符常量进行比较。表达式必须对整数求值,可以使用字符或整数值。 */ #include<stdio.h> int main() { 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\n" , place ); printf ("每位数字为: "); //求出num在各个位上的值 hundred_thousand = num / 100000; ten_thousand = ( num - hundred_thousand*100000) / 10000 ; thousand = ( num - hundred_thousand*10000 - 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 ); //判断num的位数,并根据位数做出输出 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; } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值