求教为什么加(int)
输入一个不多于5位的正整数,求它是几位数,分别输出每一位数,按逆序输出各位数字。代码如下:#include <stdio.h>
main()
{
long x;
int n;
int wan,qian,bai,shi,ge;
scanf("%ld",&x);
while(x>99999||x<=0) {printf("error.\n");scanf("%ld",&x);}
if(x>=10000) n=5;
else if(x>=1000) n=4;
else if(x>=100) n=3;
else if(x>=10) n=2;
else n=1;
printf("n=%d\n",n);
/*求几位数完成*/
wan=x/10000;
qian=(int)(x-wan*10000)/1000;
bai=(int)(x-wan*10000-qian*1000)/100;
shi=(int)(x-wan*10000-qian*1000-bai*100)/10;
ge=(int)(x-wan*10000-qian*1000-bai*100-shi*10); /* 这几句为什么前面要加int,不加就不对了?*/
switch(n)
{
case 5:
printf("every: %d %d %d %d %d\n",wan,qian,bai,shi,ge);
printf("fan:%d%d%d%d%d",ge,shi,bai,qian,wan);break;
case 4:
printf("every: %d %d %d %d\n",qian,bai,shi,ge);
printf("fan:%d%d%d%d",ge,shi,bai,qian);break;
case 3:
printf("every: %d %d %d\n",bai,shi,ge);
printf("fan:%d%d%d\n",ge,shi,bai);break;
case 2:
printf("every: %d %d\n",shi,ge);
printf("fan:%d%d\n",ge,shi);break;
case 1:
printf("every: %d\n",ge);
printf("fan: %d\n",ge);
}
getch();
}