一到c程序问题(所有分数了)

来源:百度知道 编辑:UC知道 时间:2024/05/12 07:10:58
题目是输入一个0到99999之间的数,1:求出它有几位;2:打印出每一位
3:按逆序打印出各位数字,例如原数为321,应输出123.
整个程序如下,出现了一个问题当输入的数大于40000时就会出现错误,在0到39999之间都正常,请高手指点下。
main()
{ long int num;
int place,ten_thousand,thousand,huandred,ten,indiv;
printf("please input the number(0~99999):\n");

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("place=%d \n",place);

ten_thousand=num/10000;
thousand=(num-ten_thousand*10000)/1000;
huandred=(num-ten_thousand*10000-thousand*1000)/100;
ten=(num-ten_thousand*10000-thousand*1000-huandred*100)/10;
indiv=num-ten_thousand*10000-thousand*1000-huandred*100-ten*10;

switch(place)
{
case 5:printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,huandred,ten,indiv);
printf("%d%d%d%d%d",indiv,te

编译器的问题,你用的是turbo C吧。早期的turbo C编译器int类型是16位的,有符号int可表示范围是-32768~32767,所以40000以上的数据采用int是不对的。可以采用unsigned long,这样到其他的编译器上一样能运行.你的改法也行。

这是你编译器的问题。
dev c++ 没有问题。
你应该是TC ?

可能真是编译器的问题了 我用VC也是正确的 调试过了 建议你换一个??


int place,ten_thousand,thousand,huandred,ten,indiv;
改为
long int place,ten_thousand,thousand,huandred,ten,indiv;

同时将后面的打印格式全部改为%ld