Printing an integer each digit at a time in english form recursively
How to get the digits of a number without converting it to a string/ char array?
这是用C编码而不是C ++编码。我对C的了解非常有限,因为我在入门级课程中,我们刚刚过了中期。尽量保持这一点尽可能简单,因为我不能包含我们在课堂上没有涉及的关键字或运算符。我不认为这是必要的,因为我认为这只是我的逻辑需要帮助,而不是我的代码。
参考上面两个例子来编写一个类的代码,我很难过如何完成我的最后一小部分拼图。我在SO上找到了几个问题 - 答案似乎是相关的,但他们使用代码来解决我不知道的问题。希望有人可以帮助我理解我的逻辑。
我的任务目标是:
获取用户定义的整数,并以英语显示数字。例如:
请输入一个整数:123
您已输入:One Two Three
然后,我需要将数字之和相加(如果数字<10,则以英文显示)。在这种情况下:
各个数字的总和为:六
最后,我需要使用2位小数来平均数字。在这种情况下:
平均值为:2.00
我完成了所有这些。除外:我的第一步向后列出数字!它读取10个地方,100个地方,1000个地方等。例如:
请输入一个整数:123
您输入了:Three Two One
我对这部分赋值的条件是,我可能只使用一个switch语句,而且我必须使用switch语句(意味着需要一个循环(我去做))。我也可能不会使用数组。但最后,最重要的是,我可能不会反转输入数字(这是该任务的第一个版本的解决方案)。如果我能做到这一点,我就不会在这里。
以下是相关代码的摘录。
#include <stdio.h>
int main(void)
{
int userinput, digit
printf("Please input a number:");
scanf("%d", &userinput);
printf("You have entered: ");
if (userinput < 0)
{
printf("Negative ");
userinput = -userinput;
}
do
{
digit = userinput%10;
switch (digit)
{
case 0:
{
printf("Zero ");
break;
}
case 1:
{
printf("One ");
break;
}
case 2:
{
printf("Two ");
break;
}
case 3:
{
printf("Three ");
break;
}
case 4:
{
printf("Four ");
break;
}
case 5:
{
printf("Five ");
break;
}
case 6:
{
printf("Six ");
break;
}
case 7:
{
printf("Seven ");
break;
}
case 8:
{
printf("Eight ");
break;
}
case 9:
{
printf("Nine ");
break;
}
default:
{
break;
}
}
userinput = userinput/10;
} while (userinput > 0);
printf("\n");
答案 0 :(得分:1)
如果无法使用数组,请使用递归:
void print_textual(int n)
{
if (n > 9) {
print_textual(n / 10);
}
switch (n % 10) {
case 0: printf("zero "); break;
case 1: printf("one "); break;
case 2: printf("two "); break;
case 3: printf("three "); break;
case 4: printf("four "); break;
case 5: printf("five "); break;
case 6: printf("six "); break;
case 7: printf("seven "); break;
case 8: printf("eight "); break;
case 9: printf("nine "); break;
}
}
顺便说一句,如果你可以使用一个数组至少数字名称,那么这将好得多:
void print_textual(int n)
{
if (n > 9) {
print_textual(n / 10);
}
static const char *names[] = {
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine"
};
printf("%s ", names[n % 10]);
}
答案 1 :(得分:0)
为避免使用递归,请将10的幂乘数缩放为n
。然后使用此乘数来确定最重要到最低有序的数字。
使用相同的digits_in_english()
打印数字金额。
void digits_in_english(const char *prompt, int n, int *Sum, int *Count) {
fputs(prompt, stdout);
*Sum = 0;
*Count = 0;
if (n < 0) {
printf(" Negative");
n = -n;
}
int m = n;
int pow10 = 1;
while (m > 9) {
m /= 10;
pow10 *= 10;
}
do {
static const char *Edigit[] = { "Zero", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine" };
int digit = n / pow10;
*Sum += digit;
(*Count)++;
// OP knows how to put a switch statement here instead of printf()
printf(" %s", Edigit[digit]);
n -= digit * pow10;
pow10 /= 10;
} while (pow10 > 0);
fputs("\n", stdout);
}
void Etest(int n) {
int Count, Sum;
// Change this to a printf() and scanf()
printf("Please enter an integer: %d\n", n);
digits_in_english("You have entered:", n, &Sum, &Count);
double Average = (double) Sum / Count;
// Do no care about the resultant Sum, Count
digits_in_english("The sum of the individual digits is:", Sum, &Sum, &Count);
printf("The average is: %.2f\n\n", Average);
}
样品
Please enter an integer: -2147483647
You have entered: Negative Two One Four Seven Four Eight Three Six Four Seven
The sum of the individual digits is: Four Six
The average is: 4.60
对INT_MIN不起作用。以便携方式这样做有点棘手。
看来OP不允许使用数组。希望不包括字符串。
答案 2 :(得分:-1)
所以,通过很多绊脚石,我设法让我的代码按照预期的方式工作,只使用我允许使用的知识。这是成品(除非有人看到任何巨大的错误):
//initializing variables, of course
int userinput, number1, number2, numbersum;
int div = 1;
float number3;
//displaying instructions to the user
printf("Please input a number: ");
scanf("%d", &userinput);
printf("You have entered: ");
if (userinput < 0)
{
printf("Negative ");
userinput = -userinput;
}
//the variables number1-3 are for the data analysis at the end
//I am preserving the original input in them so I can mutilate it in the following step
number1 = userinput;
number2 = userinput;
while (div <= userinput)
{
div = div*10;
}
do
{
if (userinput != 0)
{
div = div/10;
switch (userinput/div)
{
case 0:
{
printf("Zero ");
break;
}
case 1:
{
printf("One ");
break;
}
case 2:
{
printf("Two ");
break;
}
case 3:
{
printf("Three ");
break;
}
case 4:
{
printf("Four ");
break;
}
case 5:
{
printf("Five ");
break;
}
case 6:
{
printf("Six ");
break;
}
case 7:
{
printf("Seven ");
break;
}
case 8:
{
printf("Eight ");
break;
}
case 9:
{
printf("Nine ");
break;
}
default:
{
break;
}
}
userinput = userinput%div;
}
else
{
printf("Zero");
}
} while (userinput > 0);
//line break to make it look pretty
printf("\n");
//boring math to determine the sum of the digits
//assuming all are positive due to know contrary instructions
//set equal to zero since this variable refers to itself in the following function
numbersum = 0;
while (number1 > 0)
{
numbersum = numbersum + (number1 % 10);
number1 = number1 / 10;
}
//nested switch in if statement to print english if digits less than or equal to 10
if (numbersum <= 10)
{
switch (numbersum)
{
case 0:
{
printf("The sum of the individual integers is: Zero");
break;
}
case 1:
{
printf("The sum of the individual integers is: One");
break;
}
case 2:
{
printf("The sum of the individual integers is: Two");
break;
}
case 3:
{
printf("The sum of the individual integers is: Three");
break;
}
case 4:
{
printf("The sum of the individual integers is: Four");
break;
}
case 5:
{
printf("The sum of the individual integers is: Five");
break;
}
case 6:
{
printf("The sum of the individual integers is: Six");
break;
}
case 7:
{
printf("The sum of the individual integers is: Seven");
break;
}
case 8:
{
printf("The sum of the individual integers is: Eight");
break;
}
case 9:
{
printf("The sum of the individual integers is: Nine");
break;
}
case 10:
{
printf("The sum of the individual integers is: Ten");
}
default:
{
break;
}
}
printf("\n");
}
//else if greater than 10, just print the decimal number
else
{
printf("The sum of the individual digits in the integer is: %d\n", numbersum);
}
if (numbersum == 0)
{
printf("The average is of zero is not a number.\n");
}
else
{
//initializing a variable here because it's totally irrelevant to the above functions
//and this feels cleaner because of it. I'm not sure if this is bad etiquette
int i;
//picks out the number of digits in the input and effectively sets i to that number
for (i = 0; number2 > 0; i++)
{
number2 = number2/10;
}
//this is necessary for turning number3 into an actual floating point, not an int stored as float
number3 = numbersum;
//math to determine average (sum of digits divided by number of digits)
number3 = number3 / i;
printf("The average is: %.2f\n", number3);
}
return 0;
它很大,可能有点草率(由于我有多新),但它确实有效,这对我来说真的很重要。
谢谢你的帮助,伙计们。