c语言数据类型异常,请牛人解答

下面的代码,改变了数据类型,但运行的结果还有问题,请高人帮忙运行一下,并查看原因给予解答,谢谢
# include <stdio.h>

void main()
{
int num;
/* 下面定义的各变量,分别代表个位,十位,百位,千位,万位,十万位以及位数 */
int indiv, ten, hundred, thousand;
int ten_thousand, hundred_thousand, place;

printf("请输入一个整数(0~999999):");
scanf("%d", &num);

/* 判断变量num的位数 */
if(num > 99999)
place = 6;
else 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);

printf("每位数字为:");

/* 求出num在各位上的值 */
hundred_thousand = num/100000;
ten_thousand = (num - hundred_thousand*100000)/10000;
thousand = (num - hundred_thousand*100000 - ten_thousand*10000)/1000;
hundred = (num - hundred_thousand*100000 - ten_thousand*10000
- thousand*1000)/100;
ten = (num - hundred_thousand*100000 - ten_thousand*10000
- thousand*1000 - hundred*100)/10;
indiv = num - hundred_thousand*100000 - ten_thousand*10000
- thousand*1000 - hundred*100 - ten*10;

/* 判断变量num的位数,并根据位数做出相应的输出 */
switch(place)
{
case 1: printf("%d", indiv);
printf("\n反序数字为:");
printf("%d\n", indiv);
break;
case 2: printf("%d, %d", ten, indiv);
printf("\n反序数字为:");
printf("%d%d\n", indiv, ten);
break;
case 3: printf("%d, %d, %d", hundred, ten, indiv);
printf("\n反序数字为:");
printf("%d%d%d\n", indiv, ten, hundred);
break;
case 4: printf("%d, %d, %d, %d", thousand, hundred, ten, indiv);
printf("\n反序数字为:");
printf("%d%d%d%d\n", indiv, ten, hundred, thousand);
break;
case 5: printf("%d, %d, %d, %d, %d", ten_thousand, thousand,
hundred, ten, indiv);
printf("\n反序数字为:");
printf("%d%d%d%d%d\n", indiv, ten, hundred,
thousand, ten_thousand);
break;
case 6: printf("%d, %d, %d, %d, %d, %d", hundred_thousand,
ten_thousand, thousand, hundred, ten, indiv);
printf("\n反序数字为:");
printf("%d%d%d%d%d%d\n", indiv, ten, hundred, thousand,
ten_thousand, hundred_thousand);
break;
default: printf("Not find.\n");
break;
}
getch();
}

我看了下(我现在不方便编译…没条件…),除了num应该换成long以为…似乎没得错…但是你这个程序写得复杂了…因为你只用反序输出每位的内容…所以…(我只写了个函数…没编译的…如果有错,应该好改)
void func(long num)
{
if(num==0)
printf("0");
else
while(num>0)
printf("%ld ", num%10), num/=10;
printf("\n");
}
这样可以反序输出了…如果你还想指明哪一位,可用数组将英文单词装好,然后3用脚标输出便OK
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-10-21
int 在不同编译系统中长度不一样,如在 TURBO C 中就是 2B,最大数为32768,在 Visual C++ 中为 4B ,最大值为 2 的 32 次方减1。所以你的错误应该是在 Turbo C 环境中数值越界。故可修改为 long 型。
第2个回答  2008-10-21
int num; 改为long num;

scanf("%d", &num); 改为scanf("%ld", &num);
因为你要求的输入的数范围是0~999999,你定义一个int 型的num它最大为32767啊!当你输入的数大于32767不就发生错误了吗?
第3个回答  2008-10-21
恶 num改成 long int试试
第4个回答  2008-10-21
bu会
相似回答