好的,这是我的家庭作业。但在每个人都说“弄清楚”之前,我已经让程序运行起来了。我只是无法弄清楚为什么它不适用于整数的完整十个字符。作业要求用户输入一个整数。使用一个开关,我们将打印单个整数字中的数字(26 将是两个六)。我可以让它工作。我遇到的问题是当我输入 1-0 作为它打印的整数时,“一二三四五六七八零零”。如果我输入 1-9,它会打印出“一二三四五六七九九”。如果我输入 1-8,它工作得很好。我不确定这里出了什么问题。这是我的编码。
#include <stdio.h>
int main(void)
{
int num, num2, integer, decimal_place, length, sum;
float multiplier, integer_length, avg;
printf("\nPlease enter an integer: ");
scanf("%d", &num);
printf("\n");
printf("\tYou have entered:\n\n\t");
if (num < 0)
{
printf("negative ");
num *= -1;
}
num2 = num;
length = 1;
sum = 0;
integer_length = 0;
// Get the length of the input
while (num2 > 9)
{
length++;
num2 /= 10;
}
for (integer = length; integer > 0; integer--)
{
multiplier = 10;
for (decimal_place = integer; decimal_place > 0; decimal_place--)
{
multiplier *= 0.1;
}
num2 = num * multiplier;
num2 %= 10;
switch(num2)
{
case 0:
printf("zero");
sum += 0;
break;
case 1:
printf("one");
sum += 1;
break;
case 2:
printf("two");
sum += 2;
break;
case 3:
printf("three");
sum += 3;
break;
case 4:
printf("four");
sum += 4;
break;
case 5:
printf("five");
sum += 5;
break;
case 6:
printf("six");
sum += 6;
break;
case 7:
printf("seven");
sum += 7;
break;
case 8:
printf("eight");
sum += 8;
break;
case 9:
printf("nine");
sum += 9;
break;
}
printf(" ");
}
while ( num > 0 )
{
num /= 10.00;
integer_length++;
}
avg = sum / integer_length;
printf("\n\nThe sum of the individual integers is: %d\n", sum);
printf("The average is: %.2f", avg);
printf("\n\n");
return 0;
}
任何帮助将不胜感激。