/*从键盘输入一个不多于5位的整数,能显示出它是几位数,并按正反两种顺序显示出各位数字*/
#include<stdio.h>
void main()
{long a;
int place,one,ten,hundred,thousand,ten_thousand;
printf("intput a number(0~99999):");
scanf("%ld",&a);
if(a<0||a>100000)
{printf("input again\n");
scanf("%ld",&a);}
if(a>9999)
place=5;
else if(a>999)
place=4;
else if(a>99)
place=3;
else if(a>9)
place=2;
else place=1;
printf("\nweishu:");
printf("%d\n",place);
ten_thousand=a/10000;
/*thousand = (int)(a-ten_thousand*10000)/10,在求除万位以外的其他位时得加这个强制转换符,要不得不到
正确的结果。编译器是turbo c。其他 编译器就不会有这个问题。原因因该是跟turbo c 里int 型是16位,而long型是32位有关
具体原因谁能帮我解释下呢?*/
thousand=(a-ten_thousand*10000)/1000;
hundred=(a-ten_thousand*10000-thousand*1000)/100;
ten=(a-ten_thousand*10000-thousand*1000-hundred*100)/10;
one=(a-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
printf("wangwei=");
printf("%d\n",ten_thousand);
printf("qianwei=");
printf("%d\n",thousand);
printf("baiwei=");
printf("%d\n",hundred);
printf("shiwei=");
printf("%d\n",ten);
printf("gewei=");
printf("%d\n",one);
switch(place)
{case(1):printf("the changed number is:");printf("%d\n",one);break;
case(2):printf("the changed number is:");printf("%d%d\n",one,ten);break;
case(3):printf("the changed number is:");printf("%d%d%d\n",one,ten,hundred);break;
case(4):printf("the changed number is:");printf("%d%d%d%d\n",one,ten,hundred,thousand);break;
case(5):printf("the changed number is:");printf("%d%d%d%d%d\n",one,ten,hundred,thousand,ten_thousand);break;
}
getchar();
getchar();
}
我想问的是加不加这个(int)有什么不同? 有区别啊。在turbo c里没加强制转换就不正确了啊。