#include "Stdio.h"
main()
{
long int num;
int indiv,ten,hundred,thousand,ten_thousand,place;
printf("Please input the number:");
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\n",place);
ten_thousand=(int)(num/10000);
thousand=(int)(num-ten_thousand*10000)/1000;
hundred=(int)(num-thousand*1000-ten_thousand*10000)/100;
ten=(int)(num-hundred*100-thousand*1000-ten_thousand*10000)/10;
indiv=(int)(num-ten*10-hundred*100-thousand*1000-ten_thousand*10000);
switch(place)
{
case 5:printf("每一位数字为:%3d%3d%3d%3d%3d\n",ten_thousand,thousand,hundred,ten,indiv);
printf("反序的数字为:");
printf("%3d%3d%3d%3d%3d\n",indiv,ten,hundred,thousand,ten_thousand);
break;
case 4:printf("每一位数字为:%3d%3d%3d%3d\n",thousand,hundred,ten,indiv);
printf("反序的数字为:");
printf("%3d%3d%3d%3d\n",indiv,ten,hundred,thousand);
break;
case 3:printf("每一位数字为:%3d%3d%3d\n",hundred,ten,indiv);
printf("反序的数字为:");
printf("%3d%3d%3d\n",indiv,ten,hundred);
break;
case 2:printf("每一位数字为:%3d%3d\n",ten,indiv);
printf("反序的数字为:");
printf("%3d%3d\n",indiv,ten);
break;
case 1:printf("每一位数字为:%3d\n",indiv);
printf("反序的数字为:");
printf("%3d\n",indiv);
break;
}
getch();
}
这是谭浩强课本后的一个习题的源程序。这个程序的调试结果是对。
考虑到优先级的问题,类型转换运算符要比除法运算符高,因此,我加了括号。如下,蓝色的地方:
thousand=(int)((num-ten_thousand*10000)/1000);
hundred=(int)((num-thousand*1000-ten_thousand*10000)/100);
ten=(int)((num-hundred*100-thousand*1000-ten_thousand*10000)/10);
在重新调试的时候,有时候就会出现问题。比如,输入12345就没有问题,输入45678就出现问题。
(win-tc 1.8调试)
不知道是怎么回事,请大侠解释一下。
[此贴子已经被作者于2007-4-12 17:52:40编辑过]