2007-11-13
这是一个要求 输入一个不多于5位的正整数,要求:1 求它是几位数;2 分别输出每一位数字;3 按逆序输出各位数字。例如原数为321,应输出123...
(indiv代表个位的意思)
程序如下:
#include void main()
{
long int num;
int indiv,ten,hundred,thousand,ten_thousand,place;
printf("请输入一个正整数(0~99999):");
scanf("%ld",&num);
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("位数:%d",place);
printf("每位数字为:");
ten_thousand=num/10000;
thousand=(int)(num-ten_thousand*10000)/1000;
hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;
ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
switch(place)
{
case 5:printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hunsand,ten,indiv);
printf("反序数字为:");
printf("%d,%d,%d,%d,%d",indiv,ten,hundred,thousand,ten-thousand);
break;
case 4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);
printf("反序数字为:");
printf("%d,%d,%d,%d",indiv,ten,hundred,thousand);
break;
case 3:printf("%d,%d,%d",hundred,ten,indiv);
printf("反序数字为:");
printf("%d,%d,%d",indiv,ten,hundred);
break;
case 2:printf("%d,%d",ten,indiv);
printf("反序数字为:");
printf("%d,%d",indiv,ten);
break;
case 1:printf("%d",indiv);
printf("反序数字为:");
printf("%d",indiv);
break;
}
}
问题:
(1)给这个程序加上注释(可选)
(2)ten_thousand=num/10000;
thousand=(int)(num-ten_thousand*10000)/1000;
hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;
ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
在程序中起到什么作用?
(1)给这个程序加上注释(可选)
LZ的代码有粘贴错误,现简单注释如下:
#include /*包含头文件*/
void main() /*主函数*/
{
long int num; /*声明长整型变量,用来保存输入的数值*/
int indiv,ten,hundred,thousand,ten_thousand,place; /*声明整型的数位变量(个位,十位,百位,千位,万位)及数位个数*/
printf("请输入一个正整数(0~99999):"); /*提示输入整数*/
scanf("%ld",&num); /*输入长整数*/
if(num>9...全部
(1)给这个程序加上注释(可选)
LZ的代码有粘贴错误,现简单注释如下:
#include /*包含头文件*/
void main() /*主函数*/
{
long int num; /*声明长整型变量,用来保存输入的数值*/
int indiv,ten,hundred,thousand,ten_thousand,place; /*声明整型的数位变量(个位,十位,百位,千位,万位)及数位个数*/
printf("请输入一个正整数(0~99999):"); /*提示输入整数*/
scanf("%ld",&num); /*输入长整数*/
if(num>9999) /*按照从大到小的顺序进行判断,如果大于9999,说明输入的是5位数*/
place=5; /*位数变量值置为5*/
else if(num>999) /*如果大于999,说明输入的是4位数*/
place=4; /*位数变量值置为4*/
else if(num>99) /*如果大于99,说明输入的是3位数*/
place=3; /*位数变量置为3*/
else if(num>9) /*如果大于9,说明输入的是2两位数*/
place=2; /*位数变量置为2*/
else
place=1; /*否则置为1*/
printf("位数:%d",place); /*输出数值的位数*/
printf("每位数字为:"); /*提示每位数字的输出*/
ten_thousand=num/10000; /*整型数除10000,得到万位*/
thousand=(int)(num-ten_thousand*10000)/1000; /*输入的数值减去万位*10000,剩下的千位数,然后再除以1000,得到千位个数*/
hundred=(int)(num-ten_thousand*10000-thousand*1000)/100; /*余下类推,方法同上*/
ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
switch(place) /*以下是根据数值位数进行判断,以输出不同位数的数值及相应个位数*/
{
case 5:printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundred,ten,indiv); /*输出正常顺序数值*/
printf("反序数字为:");
printf("%d,%d,%d,%d,%d",indiv,ten,hundred,thousand,ten_thousand); /*按反序输出*/
break;
case 4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);
printf("反序数字为:");
printf("%d,%d,%d,%d",indiv,ten,hundred,thousand);
break;
case 3:printf("%d,%d,%d",hundred,ten,indiv);
printf("反序数字为:");
printf("%d,%d,%d",indiv,ten,hundred);
break;
case 2:printf("%d,%d",ten,indiv);
printf("反序数字为:");
printf("%d,%d",indiv,ten);
break;
case 1:printf("%d",indiv);
printf("反序数字为:");
printf("%d",indiv);
break;
}
}
(2)
ten_thousand=num/10000;
thousand=(int)(num-ten_thousand*10000)/1000;
hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;
ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
上面几个语句的作用:是通过简单的整数运算,分别求得数值相应的每个组成位数。
语句中的(int)是强制类型转换的意思,将运算后的数值强制转换为int型。实际上,这些强制转换操作可以省略,整型运算的结果依然是整型。
举个列子:输入12345
1、语句 num/10000 即是 12345/10000 结果为1,即求得万位数是1,也就是变量ten_thousand值为1。
2、语句 (num-ten_thousand*10000)/1000 即是 (12345 - 1*10000)/1000 结果为2,即求得千位数是2,也就是变量thousand值为2。
3、以此类推,可以分别求得百位,十位,个位。
4、然后进行输出,就是容易的事儿了。
。收起