在英文整数上显示每个数字的程序不适用于以“0”开头的整数

时间:2014-03-08 16:54:28

标签: c loops integer

我有一项任务,我必须编写一个程序,该程序从终端键入一个整数,并以英语提取并显示整数的每个数字。我无法使用数组或递归,我们只是从编程开始。

例如: “123”返回“一二三”

我的程序运行良好(大部分时间),但问题是当你在终端输入类似“0123”的东西时,程序返回“八三”...... WTH ??

这是我的代码:

// Program that takes an integer and displays each digit in English

#include <stdio.h>

int main (void)
{
    int num, digit;
    int reversed = 0, backupZero = 0;

    printf("Please enter an integer:\n");
    scanf("%i", &num);

    if (num == 0) // In case the input is just "0"
    {
        printf("zero");
    }

    while (num > 0) // Loop to reverse the integer
    {
        digit = num % 10;
        reversed = (reversed * 10) + digit;

        if ((reversed == 0) && (digit == 0)) // If the integer finishes in zero
        {
            ++backupZero; // Use this to add extra zeroes later
        }

        num /= 10;
    }

    while (reversed > 0)
    {
        digit = reversed % 10;
        reversed /= 10;

        switch (digit)
        {
            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:
                printf("zero ");
                break;
        }

    }

    for (int counter = 0; counter < backupZero; ++counter) // Prints the extra zeroes at the end
    {
        printf("zero ");
        --backupZero;
    }

    printf("\n");

    return 0;
}

可能是关于数学的东西,我承认我不擅长它。

4 个答案:

答案 0 :(得分:3)

当您使用

读取数字时
scanf("%i", &num);

你让scanf推断数字的基数。以0开头,后跟其他数字的数字被解释为八进制。因此0123123不同。事实上,83

0100 = 64
 020 = 16
  03 =  3
---------
0123 = 83

要将数字作为基数10,请使用

scanf("%d", &num);

答案 1 :(得分:0)

如果你想处理以'0'开头的数字,那么我建议你把用户输入读作字符串(字符数组)而不是整数。

除此之外,您可以使用一个简单的数组来代替每个字符的“切换”,以便将正确的单词映射到每个数字。

以下是实现它的一种方法:

#include <stdio.h>

#define MAX_INPUT_LEN 100

const char* digits[] = {"zero","one","two"  ,"three","four",
                        "five","six","seven","eight","nine"};

int main()
{
    int i;
    char format[10];
    char str[MAX_INPUT_LEN+1];
    sprintf(format,"%c%us",'%',MAX_INPUT_LEN); // now format = "%100s"
    scanf(format,str); // will write into str at most 100 characters
    for (i=0; str[i]!=0; i++)
    {
        if ('0' <= str[i] && str[i] <= '9')
            printf("%s ",digits[str[i]-'0']);
        else
            printf("invalid character ");
    }
    return 0;
}

答案 2 :(得分:0)

哦,哇。我花了3到4个小时写下面的代码。我只在第一周进入,所以请体谅一下。 更新:添加工作减去+一些评论。

#include <stdio.h>
#include <math.h>

int main(void)

{
    int num, count, user, out;
    count = 0;

    printf("Type in any int: ");
    scanf("%d", &num);

    // adding minus to the beginning if int is negative
    if (num < 0) 
    {
        num = -num;
        printf("minus ");
    }
    user = num;

    // creating a power to the future number
    while (num != 0)
    {
       num = num / 10;
       count++;
    }

    int i2;
    i2 = count;

    // main calculations: dividing by (10 to the power of counter) and subtracting from the initial number
    for (int i = 0; i < i2; i++)
    {
        out = user / pow(10, count - 1);
        user = user - out * pow(10, count - 1);
        count--;

        switch (out)
        {
            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;
            case 0:
                printf("zero ");
                break;
            default:
                break;
         }
    }
    printf("\n");

    return 0;
}

答案 3 :(得分:0)

有一些错误:

if ((reversed == 0) && (digit == 0)) (incorrect)
if ((reversed == 0) || (digit == 0)) (correct)

在最后一个循环中,您应该删除

--backupZero;

代码会更好地读取数字