C语言 显示整数的数字,而不使用反向数字或数组



以英文递归形式一次打印每个数字的整数

如何在不将其转换为字符串/字符数组的情况下获取数字的数字?

这是针对 C 编码,而不是C++。我对C语言的了解非常有限,因为我正在上入门级课程,而我们刚刚度过期中考试。尽量保持简单,因为我不能包含我们在课堂上没有涉及的关键字或运算符。我认为这是没有必要的,因为我认为需要帮助的只是我的逻辑,而不是我的代码。

在引用了上述两个示例来为一个类编写代码之后,我对如何完成我的最后一小块拼图感到困惑。我在SO上找到了几个问题答案,这些答案似乎是相关的,但是他们使用代码来解决我不了解的问题。希望有人能在这里帮助我解决我的逻辑问题。

我的任务目标是:

取一个用户定义的整数,并用英文显示数字。例如:

请输入一个整数:123
您输入的: 一二三

然后,我需要将数字的总和相加(如果数字<10,则以英文显示)。在这种情况下:

各个数字的总和为:六

最后,我需要使用 2 位小数平均数字。在这种情况下:

平均值为: 2.00

我已经完成了所有这些。除了:我的第一步是向后列出数字!它读取 10s 位置、100s 位置、1000s 位置等。例如:

请输入一个整数:123
您输入: 三二一

我对这部分作业的条件是,我只能使用一个 switch 语句,并且我必须使用 switch 语句(这意味着需要一个循环(我选择了 do))。我也可能不使用数组。但最后,也是最重要的一点,我可能不会反转输入数字(这是此作业第一个版本的解决方案)。如果我能做到这一点,我就不会在这里。

以下是相关代码的摘录。

#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");

如果不能使用数组,请使用递归:

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]);
}

为避免使用递归,请形成一个缩放到 n 的 10 次幂乘数。 然后使用此乘数按最高有效到最低有效顺序确定数字。

使用相同的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: %dn", 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: %.2fnn", 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 使用数组。 希望不包括字符串。

因此,通过很多磕磕绊绊,我设法使我的代码按照预期的方式工作,只使用我被允许使用的知识。这是成品(除非有人看到任何巨大的错误):

    //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: %dn", 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: %.2fn", number3);
    }
    return 0;

它很大,可能有点草率(由于我有多新),但它有效,这就是我现在真正重要的。

谢谢你们的帮助,伙计们。