C_使用switch语句
2009-11-05 20:42
148 查看
源码:
# include <stdio.h>
int main()
{
int num;
/* 下面定义的各变量,分别代表个位,十位,百位,千位,万位,十万位以及位数 */
int indiv, ten, hundred, thousand;
int ten_thousand, hundred_thousand, place;
printf("请输入一个整数(0~999999):");
scanf("%d", &num);
/* 判断变量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*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;
/* 判断变量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;
}
return 0;
}
其中,打算用以下函数处理每一位的值的返回过程:(待整理)
# include <stdio.h>
int main()
{
int num;
/* 下面定义的各变量,分别代表个位,十位,百位,千位,万位,十万位以及位数 */
int indiv, ten, hundred, thousand;
int ten_thousand, hundred_thousand, place;
printf("请输入一个整数(0~999999):");
scanf("%d", &num);
/* 判断变量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*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;
/* 判断变量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;
}
return 0;
}
其中,打算用以下函数处理每一位的值的返回过程:(待整理)
int Break(unsigned m, int i) { for (; i>1; --i) m /= 10; return m%10; }
相关文章推荐
- 在Android library中不能使用switch-case语句报错及解决方案
- 用户从键盘输入月份,使用switch语句输出该月份的天数
- C语言选择语句的使用---if()-else和switch--case
- if,continue,break,while,do-while, switch,return,foreach,for等条件语句的使用
- 除了switch,PHP就不能像Python一样使用Map来代替多分枝条件语句吗? (只讨论技术)
- Java SE7新特性之switch语句中使用字符串
- 不使用if、?:、switch及其他判断语句如何找出两个int型变量中的最大值和最小值
- 求 1+2+...+n, 要求不能使用乘除法、for、while、if、else、switch、case 等关键字以及条件判断语句 (A?B:C)。
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
- 关于在for循环的switch语句使用break和continue问题
- switch语句的使用
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)
- switch分支语句注意事项及注册界面的使用思路
- 作业3-5 输入五级制成绩(A-E),输出相应的百分制成绩(0-100)区间,要求使用 switch语句
- switch语句的简单使用
- C语言 switch语句的使用总结
- C# 使用枚举数 避免使用switch语句
- switch语句的使用
- Swift中if与switch语句使用一例
- switch语句使用总结