在循环中打印变量时的无限输出

时间:2018-05-08 14:49:14

标签: c++

我试图将数字输入转换为单词。我是初学者,所以我的代码看起来很笨重。我也为张贴这么大的代码而道歉。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
    int i = 0, number = 0;
    string num_name = "";
    cout << "Enter a number (maximum value is 1,000,000,000)" << endl;
    cin >> number;

    //To find the maximum magnitude
    while ((number / pow(10, i)) >= 1)
    {
        i++;
    }
    i--;
    int j = i;
    cout << j << endl; //Check

                       //To find which multiple of 3 i is
    int n = 0;
    while (j >= 3)
    {
        if (j % 3 == 0)
            n++;
        j--;
    }
    j = i;
    cout << j << endl; //Check
    cout << n << endl; //Check

                       //String construction
    int k = n;
    int q = 0;

    while (k >= 1)
    {
        while (j >= (3 * k))
        {
            q = (int)(number / pow(10, j));
            if (j == 2 + (3 * k))
            {
                if (q == 1) num_name += "one";
                else if (q == 2) num_name += "two";
                else if (q == 3) num_name += "three";
                else if (q == 4) num_name += "four";
                else if (q == 5) num_name += "five";
                else if (q == 6) num_name += "six";
                else if (q == 7) num_name += "seven";
                else if (q == 8) num_name += "eight";
                else if (q == 9) num_name += "nine";

                num_name += "hundred";
            }
            if (j == 1 + (3 * k))
                continue;
            if (j == 3 * k)
            {
                if (q == 1) num_name += "one";
                else if (q == 2) num_name += "two";
                else if (q == 3) num_name += "three";
                else if (q == 4) num_name += "four";
                else if (q == 5) num_name += "five";
                else if (q == 6) num_name += "six";
                else if (q == 7) num_name += "seven";
                else if (q == 8) num_name += "eight";
                else if (q == 9) num_name += "nine";
                else if (q == 10) num_name += "ten";
                else if (q == 11) num_name += "eleven";
                else if (q == 12) num_name += "twelve";
                else if (q == 13) num_name += "thirteen";
                else if (q == 14) num_name += "fourteen";
                else if (q == 15) num_name += "fifteen";
                else if (q == 16) num_name += "sixteen";
                else if (q == 17) num_name += "seventeen";
                else if (q == 18) num_name += "eighteen";
                else if (q == 19) num_name += "nineteen";
                if (q >= 20 && q <= 99)
                {
                    int r = 0;
                    r = q % 10;
                    (int)(q /= 10);
                    if (q == 2) num_name += "twenty";
                    else if (q == 3) num_name += "thirty";
                    else if (q == 4) num_name += "forty";
                    else if (q == 5) num_name += "fifty";
                    else if (q == 6) num_name += "sixty";
                    else if (q == 7) num_name += "seventy";
                    else if (q == 8) num_name += "eighty";
                    else if (q == 9) num_name += "ninety";

                    if (r == 1) num_name += "one";
                    else if (r == 2) num_name += "two";
                    else if (r == 3) num_name += "three";
                    else if (r == 4) num_name += "four";
                    else if (r == 5) num_name += "five";
                    else if (r == 6) num_name += "six";
                    else if (r == 7) num_name += "seven";
                    else if (r == 8) num_name += "eight";
                    else if (r == 9) num_name += "nine";
                }


                if (k == 3) num_name += "billion";
                else if (k == 2) num_name += "million";
                else if (k == 1) num_name += "thousand";
            }
            number = number % (int)pow(10, j);
            j--;
        }
        k--;
    }
    cout << num_name << endl;
    cin.ignore();
    cin.get();
    return 0;
}

&#34; String Construction&#34;部分崩溃并打印变量q(商)的无限值。我也尝试在q之前打印一个像flag = 1这样的简单变量,这也是无限的1。我无法弄清楚造成这种情况的原因。任何输入都会对我的学习有很大帮助!

1 个答案:

答案 0 :(得分:0)

嗯,你能做的最好的事情之一就是不要弄乱主要功能。你弄乱了主要功能:c

创建一些功能可以让您用更少的代码解决问题。要创建的函数的数量是主观的,imho它可以很好地创建2个函数:toDigit和convert,都返回字符串。

另外我会使用一个函数用于logaritm和电源而不使用库(主要是因为power返回一个double,你必须将它转换为int,数学库是重量级和其他原因)

让我们开始:(所有功能必须高于主要功能,而数字必须高于转换)

你可以通过基本的logaritmic函数得到数字的大小,这会给你一个不好的约束,因为我称之为sillyLog:

int sillyLog(int n, int b){
    return !(n/b < 1)? 1 + sillyLog(n/b, b) : 1;
}

然后我们为权力创建我们的功能:

int power(int b, int e){
    return (e < 1)? 1 : b * power(b, e-1);
}

现在让我们看看主要的:

int main(){
    int number = 0, magnitude = 0, counter = 1;
    string num_name = "";
    cout << "Enter a number (maximum value is 2,147,483,647)" << endl;
    cin >> number;
    magnitude = sillyLog(number, 10);
    while (magnitude - 3 > 0){
        magnitude = magnitude - 3;
        counter++;
    }
    while (number){
        int tmp;
        tmp = (number - (number % power(1000,--counter)))/ power(1000, counter);//remove all digit after the first three and decrease counter
        num_name += convert(tmp, counter + 1);
        number = number - tmp * power(1000, counter);                           // we cut the first 3 digit 
    }
    cout << num_name << endl;
    return 0;
}

计数器是计算三元组的变量,

同样2,147,483,647是整数类型的最大长度,如果你想要一个更大的数字,你必须使用long / long long / unsigned / other类型。 注意:负数不是一个问题,因为你只是忽略了它们但是它们会吃掉#34;记忆的一半用于数字。

如何看到我们生成字符串只需调用函数convert

string convert(int n, int m){
    string s = "";
    s = ((n-n%100)/100)? toDigit((n-n%100)/100) + " hundred " : "";
    s += toDigit(n - (n - n%100));
    switch (m){
        case 4: return s + " billion ";
        case 3: return s + " million ";
        case 2: return s + " thousand "; 
        default: return s;
    }
}

我使用了条件运算符(三元组)和switch case而不是if只是为了让你知道它们的存在

现在让我们看看最简单的功能:c

string toDigit(int n){
    if (n < 20)
        switch (n){
            case 1: return "one ";
            case 2: return "two ";
            case 3: return "three ";
            case 4: return "four ";
            case 5: return "five ";
            case 6: return "six ";
            case 7: return "seven ";
            case 8: return "eight ";
            case 9: return "nine ";
            case 10: return "ten ";
            case 11: return "eleven ";
            case 12: return "twelve ";
            case 13: return "thirteen ";
            case 14: return "fourteen ";
            case 15: return "fifteen ";
            case 16: return "sixteen ";
            case 17: return "seventeen ";
            case 18: return "eighteen ";
            case 19: return "nineteen ";
            default: return "";
        }
    switch ((n - n%10) / 10){
        case 2: return "twenty " + toDigit(n - (n - n % 10));
        case 3: return "thirty " + toDigit(n - (n - n % 10));
        case 4: return "forty " + toDigit(n - (n - n % 10));
        case 5: return "fifty " + toDigit(n - (n - n % 10));
        case 6: return "sixty " + toDigit(n - (n - n % 10));
        case 7: return "seventy " + toDigit(n - (n - n % 10));
        case 8: return "eighty " + toDigit(n - (n - n % 10));
        case 9: return "ninety " + toDigit(n - (n - n % 10));
    }
}

这是必须使用开关盒使你的代码可读

如果您有任何疑问,我会很乐意回答您