重载运算符<<以数字和英文返回数字

时间:2015-10-14 00:41:48

标签: c++ class operator-overloading ostream

我现在已经在一个项目上工作了几个小时,这是我无法弄清楚如何修复的最后一个错误。课程是:

class WholeNumber
{
    private:
        int number;
    public:
        WholeNumber();
        WholeNumber(int n);
        void display(); //Display the English description
        WholeNumber operator+(const WholeNumber &n1);
        WholeNumber operator-(const WholeNumber &n1);
        WholeNumber operator++();
        WholeNumber operator--();
        friend ostream& operator<<(ostream& output, const WholeNumber& N);
        int getNum();
};

我试图让运营商&lt;&lt;以数字形式和英文形式显示数字,如示例编号452所示:

452, four hundred fifty two

我设法编译的是:

ostream& operator<<(ostream& output, const WholeNumber& N)
{
    return output << N.number;
}

哪个会以数字形式返回数字,即452.我尝试使用

ostream& operator<<(ostream& output, const WholeNumber& N)
{
    return output << N.number << display(); //doesn't work
    return output << N.number << N.display(); //also doesn't work
}

display()函数很长,但它开始了:

void WholeNumber::display()

我可以复制display()的整个代码,但我不知道这是否有帮助。任何帮助将不胜感激。

完整代码:

#include<iostream>
#include<string>
#include<sstream>
#include<cstring>

using namespace std;

class WholeNumber
{
    private:
        int number;
    public:
        WholeNumber();
        WholeNumber(int n);
        void display(); //Display the English description
        WholeNumber operator+(const WholeNumber &n1);
        WholeNumber operator-(const WholeNumber &n1);
        WholeNumber operator++();
        WholeNumber operator--();
        int getNum();
        friend ostream& operator<<(ostream& output, const WholeNumber& N);
        const static string lessThan20[];
        const static string tens[];
        const static string hundreds[];
};

WholeNumber::WholeNumber()
{
    number = 0;
}

WholeNumber::WholeNumber(int n)
{
    if (n > 1000)
        number = 1000;
    else if (n < -1000)
        number = -1000;
    else
        number = n;
}

const string WholeNumber::lessThan20[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
const string WholeNumber::tens[] = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
const string WholeNumber::hundreds[] = { "one hundred", "two hundred", "three hundred", "four hundred", "five hundred", "six hundred", "seven hundred", "eight hundred", "nine hundred" };

int WholeNumber::getNum()
{
    return number;
}

WholeNumber WholeNumber::operator+(const WholeNumber &n1)
{
    WholeNumber N = number + n1.number;

    if(N.number > 1000)
        N = 1000;
    return N;
}

WholeNumber WholeNumber::operator-(const WholeNumber &n1)
{
    WholeNumber N = number - n1.number;

    if(N.number < -1000)
        N = -1000;
    return N;
}

WholeNumber WholeNumber::operator++()
{
    ++number;
    return *this;
}

WholeNumber WholeNumber::operator--()
{
    --number;
    return *this;
}

void WholeNumber::display()
{
    int num = number;

    int digit1;
    int digit2;
    int digit3;

    if (num == -1000)
        cout << "negative one thousand" << endl;
    else if (num >= -999 && num <= -100)
    {
        num = num * -1;
        digit1 = num / 100;
        if ((num % 100) > 19)
            digit2 = num % 100 / 10;
        else
            digit2 = num % 100;
        digit3 = (num % 100) % 10;

        if ((num % 100) < 20 && digit3 != 0)
            cout << "negative " << hundreds[digit1 - 1] << " " << lessThan20[digit2] << endl;
        else if (digit2 == 0 && digit3 == 0)
            cout << "negative " << hundreds[digit1 - 1] << endl;
        else
            cout << "negative " << hundreds[digit1 - 1] << " " << tens[digit2 - 2] << " " << lessThan20[digit3] << endl;
    }
    else if (num >= -99 && num < -19)
    {
        num = num * -1;
        digit1 = num / 10;
        digit2 = num % 10;

        if ((num % 10) > -20 && digit2 != 0)
            cout << "negative " << tens[digit1 - 2] << " " << lessThan20[digit2] << endl;
        else if (digit2 == 0)
            cout << "negative " << tens[digit1 - 2] << endl;
        else
            cout << "negative " << tens[digit1 - 2] << " " << lessThan20[digit2] << endl;
    }
    else if (num >= -19 && num <= -1)
    {
        num = num * -1;
        cout << "negative " << lessThan20[num] << endl;
    }
    else if (num == 0)
        cout << "zero" << endl;
    else if (num >= 1 && num <= 19)
        cout << lessThan20[num] << endl;
    else if (num > 19 && num <= 99)
    {
        digit1 = num / 10;
        digit2 = num % 10;

        if ((num % 10) < 20 && digit2 != 0)
            cout << tens[digit1 - 2] << " " << lessThan20[digit2] << endl;
        else if (digit2 == 0)
            cout << tens[digit1 - 2] << endl;
        else
            cout << tens[digit1 - 2] << " " << lessThan20[digit2] << endl;
    }
    else if (num >= 100 && num <= 999)
    {
        digit1 = num / 100;
        if ((num % 100) > 19)
            digit2 = num % 100 / 10;
        else
            digit2 = num % 100;
        digit3 = (num % 100) % 10;

        if ((num % 100) < 20 && digit3 != 0)
            cout << hundreds[digit1 - 1] << " " << lessThan20[digit2] << endl;
        else if (digit2 == 0 && digit3 == 0)
            cout << hundreds[digit1 - 1] << endl;
        else
            cout << hundreds[digit1 - 1] << " " << tens[digit2 - 2] << " " << lessThan20[digit3] << endl;
    }
    else
        cout << "one thousand" << endl;
}

ostream& operator<<(ostream& output, const WholeNumber& N)
{
    output << N.number << N.display();
    return output;
}

int main()
{
    WholeNumber n1(777);
    WholeNumber n2(345);
    n1.display();

    ++n1;
    --n2;
    cout << n1 - n2 << endl;
    return 0;
}

0 个答案:

没有答案