用C语言编程用英语显示输入的整数 [英] Programming in C- Display inputted integers in english

查看:153
本文介绍了用C语言编程用英语显示输入的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写Kochan的C编程问题时遇到了问题,第6章,练习6.问题是获取用户输入(整数)并用英语显示。防爆。用户输入123输出应为一二三。在本书的这一点上,我应该只具备C的基本知识(没有数组,没有奇特的功能)所以我不能使用包含除基础知识以外的任何建议。以下是我到目前为止用英语显示大于9的整数:



I am having trouble coding a problem from Kochan''s "Programming in C," Chapter 6, Exercise 6. The problem is to take user input (an integer) and display it in english. Ex. User inputs "123" output should be "one two three." At this point in the book, I should only have basic knowledge of C (no arrays, no fancy functions) so I cannot use suggestions that include anything other than the basics. Here is what I have so far to display integers greater than 9 in english:

if (number > 9) {
            while (temp >= 1 ) {
                temp = temp / 10;
                ++counter;
            }
            for (i = 1; i <= (counter - 1); i++) {
                digit = number / pow(10, (counter - i));
                if (digit == 9) {
                    printf ("nine ");
                }
                else if (digit == 8) {
                    printf ("eight ");
                }
                else if (digit == 7) {
                    printf ("seven ");
                }
                else if (digit == 6) {
                    printf ("six ");
                }
                else if (digit == 5) {
                    printf ("five ");
                }
                else if (digit == 4) {
                    printf ("four ");
                }
                else if (digit == 3) {
                    printf ("three ");
                }
                else if (digit == 2) {
                    printf ("two ");
                }
                else if (digit == 1) {
                    printf ("one ");
                }
                x = 1;
                for (x = 1; x <= counter - i; x++) {
                    ten = ten * 10;
                }
                    number = number % ten;
        }
        }
                digit = number;
                if (digit > 0 && digit < 10) {
                if (digit == 9) {
                    printf ("nine ");
                }
                else if (digit == 8) {
                    printf ("eight ");
                }
                else if (digit == 7) {
                    printf ("seven ");
                }
                else if (digit == 6) {
                    printf ("six ");
                }
                else if (digit == 5) {
                    printf ("five ");
                }
                else if (digit == 4) {
                    printf ("four ");
                }
                else if (digit == 3) {
                    printf ("three ");
                }
                else if (digit == 2) {
                    printf ("two ");
                }
                else if (digit == 1) {
                    printf ("one ");
                }
        }





注意:我知道我没有使用switch语句,我稍后会更改。 int temp等于int number。 int number是用户输入的值。声明时,int digit和int counter = 0。声明时,int 10 = 1。



我现在得到的输出,无论输入的整数是多长,都是用英语显示的前两位数。我的代码中的逻辑出了什么问题?



NOTES: I know I am not using switch statements, I will change that later. int temp is equal to int number. int number is the value of the user input. int digit and int counter = 0 at declaration. int ten = 1 at declaration.

The output I am getting right now, no matter how long the inputted integer is, is the first two digits displayed in english. What is wrong with the logic in my code?

推荐答案

这段代码太天真了,不值得回顾。这甚至不是编程。



这是你可以做的第一步:把你所有的英文数字放在一些字符串数组中。比如果你有一个数字0..9,它的英文表示将在一行代码中找到;这是由该数字索引的数组的元素。



除此之外,人们很容易开发单位将多位置数字翻译成英文单词的组合,例如五百三十二,在所有情况下控制复数/单数切换(数百或数千对数百或数千等)。虽然这需要更多的努力,但这仍然很容易,因为在这方面,英语非常非常简单。



-SA
This code is so naive that it does not worth reviewing. This is not even programming.

This is the first step you could do: put all your English numeral in some array of strings. Than, if you have a number 0..9, its English representation of it will be found in one line of code; this is the element of the array indexed by that number.

On top of that, people easily developed units "translating" multi-positional numbers into combination of English words, such as "five hundreds thirty two", controlling plural/singular switching in all the cases ("hundreds" or "thousands" vs. "hundred" or "thousand", and so on). Even though it would require more considerable effort, this is still easy enough, as English language is really, really simple, in this respect.

—SA


我修复了一些错误(未初始化的变量)。请注意:您也应该处理数字 0 (零)。



I fixed some bugs (uninitialized variables). Please note: you should handle the digit 0 (zero) too.

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

void show_number(int number)
{
    int counter = 0;
    int i, temp, digit, x, ten;
    if (number > 9) {
        temp = number;
        while (temp >= 1 ) {
            temp = temp / 10;
            ++counter;
        }
        for (i = 1; i <= (counter - 1); i++) {
            digit = (int)number / pow(10.0, (counter - i));
            if (digit == 9) {
                printf ("nine ");
            }
            else if (digit == 8) {
                printf ("eight ");
            }
            else if (digit == 7) {
                printf ("seven ");
            }
            else if (digit == 6) {
                printf ("six ");
            }
            else if (digit == 5) {
                printf ("five ");
            }
            else if (digit == 4) {
                printf ("four ");
            }
            else if (digit == 3) {
                printf ("three ");
            }
            else if (digit == 2) {
                printf ("two ");
            }
            else if (digit == 1) {
                printf ("one ");
            }
            x = 1;
            ten =1;
            for (x = 1; x <= counter - i; x++) {
                ten = ten * 10;
            }
            number = number % ten;
        }
    }
    digit = number;
    if (digit > 0 && digit < 10) {
        if (digit == 9) {
            printf ("nine ");
        }
        else if (digit == 8) {
            printf ("eight ");
        }
        else if (digit == 7) {
            printf ("seven ");
        }
        else if (digit == 6) {
            printf ("six ");
        }
        else if (digit == 5) {
            printf ("five ");
        }
        else if (digit == 4) {
            printf ("four ");
        }
        else if (digit == 3) {
            printf ("three ");
        }
        else if (digit == 2) {
            printf ("two ");
        }
        else if (digit == 1) {
            printf ("one ");
        }
    }
}


这篇关于用C语言编程用英语显示输入的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆