那里错了,帮忙改下

来源:百度知道 编辑:UC知道 时间:2024/05/10 11:07:17
给一个不多于5位的正整数,要求:1.求它是几位数,
2.逆序打印出各位数字
#include <stdio.h>
#include <math.h>

int main()
{
long int num;
int indiv,ten,hundred,thousand,ten_thousand,place;
printf("请输入一个整数(0 ~ 99999): ");
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);
switch(place)
{
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 4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);
printf("\n反序数字为: ");
printf("%d%d%d%d\n",indiv,ten,hundred,thousand);
bre

你的变量indiv,ten,hundred,thousand,ten_thousand根本就没赋值,也就是说你的程序根本就没计算这些这个数的各个位是什么数字就输出了......
修改后:
#include <stdio.h>
#include <math.h>

int main()
{
long int num;
int indiv,ten,hundred,thousand,ten_thousand,place;
printf("请输入一个整数(0 ~ 99999): ");
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=num/10000;
thousand=num/1000%10;
hundred=num/100%10;
ten=num/10%10;
indiv=num%10;
//-------------------------------------------------
switch(place)
{
case 5:printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundred,ten,indiv);
printf("\n反序数字为: ");
printf(&quo