运算符>>重载

Pat*_*ray 3 c++ operator-overloading

我正在使用运算符重载实现复数.在程序中,用户以以下形式输入复数:ALWAYS:

a + bi
Run Code Online (Sandbox Code Playgroud)

所以,在例子中......

25.0 + 3.6i
Run Code Online (Sandbox Code Playgroud)

假设用户将始终输入复数的实部和虚部,例如,用户将输入"5 + 0i"(而不是"5")或"0 - 6.2i"(而不是"-6.2")一世").

我的问题是在main()中我有以下代码:

ComplexNumber c1;
cin >> c1;
cout << c1;
Run Code Online (Sandbox Code Playgroud)

并打印代码:

0 + 0i
Run Code Online (Sandbox Code Playgroud)

...当我在运行时输入"4.2 + 8.3i"时.

这是我的operator>>班级实施:

istream & operator>>(istream & in, ComplexNumber & n) {
    string real;
    string imag;
    bool done = false;
    int sign = 1;

    string num;
    in >> num;

    int length;
    for (int i = 0; i < num.length(); i++) {
        if (num.at(i) == 'i') {
            imag = num.substr((i - length), i);
        }
        else if (num.at(i) == '-') {
            sign = -1;
        }
        else if (num.at(i) == ' ') {
            if (!done) {
                real = num.substr(i);
                done = true;
            }
            length = 0;
        }
        length++;
    }

    n = ComplexNumber(atof(real.c_str()), atof(imag.c_str()) * sign);
    return in;
}
Run Code Online (Sandbox Code Playgroud)

这是我的operator<<类实现:

ostream & operator<<(ostream & out, const ComplexNumber & n) {
    n.print(out);
    return out;
}
Run Code Online (Sandbox Code Playgroud)

这是我对ComplexNumber成员类print()的实现:

void ComplexNumber::print(ostream & out) const {
    if (imag >= 0)
        out << real << " + " << imag << "i";
    else 
        out << real << " - " << (-1 * imag) << "i";
}
Run Code Online (Sandbox Code Playgroud)

这是我的ComplexNumber头文件,以获取更多详细信息:

#ifndef COMPLEXNUMBER_H
#define COMPLEXNUMBER_H

#include <iostream>
using namespace std;

class ComplexNumber {

  public:

    // constructors
    ComplexNumber();
    ComplexNumber(double real_part, double imaginary_part);
    ComplexNumber(const ComplexNumber & rhs);

    // named member functions
    void print(ostream & out = cout) const;
    bool equals(const ComplexNumber & rhs) const;

    // assignment operators
    const ComplexNumber & operator=(const ComplexNumber & rhs);
    const ComplexNumber & operator+=(const ComplexNumber & rhs);
    const ComplexNumber & operator-=(const ComplexNumber & rhs);
    const ComplexNumber & operator*=(const ComplexNumber & rhs);

  private:
    double real;      
    double imag; 
};

// arithmetic operators
ComplexNumber operator+(const ComplexNumber & lhs, const ComplexNumber & rhs);
ComplexNumber operator-(const ComplexNumber & lhs, const ComplexNumber & rhs);
ComplexNumber operator*(const ComplexNumber & lhs, const ComplexNumber & rhs);

// relational operators
bool operator==(const ComplexNumber & lhs, const ComplexNumber & rhs);
bool operator!=(const ComplexNumber & lhs, const ComplexNumber & rhs);

// I/O operators
ostream & operator<<(ostream & out, const ComplexNumber & n);
istream & operator>>(istream & in, ComplexNumber & n);

#endif
Run Code Online (Sandbox Code Playgroud)

对我的实现的任何帮助都会很棒.

Kon*_*lph 5

基本上你operator >>的方式过于复杂,甚至不能正确处理错误.您不应该将值读入字符串中 - 直接将其读入数字.此外,在每次读取操作之后,您需要检查(并可能设置)流的状态.

istream& operator >>(istream& in, ComplexNumber& value) {
    int re;
    if (not (in >> re)) {
        return in;

    char pm;
    if (not (in >> pm) or (pm != '+' and pm != '-') {
        in.setstate(ios::failbit);
        return in;
    }

    int im;
    if (not (in >> im))
        return in;

    char i;
    if (not (in >> i) or i != 'i') {
        in.setstate(ios::failbit);
        return in;
    }

    value = ComplexNumber {re, (pm == '-' ? -im : im)};
    return in;
}
Run Code Online (Sandbox Code Playgroud)

(我使用C++ 11初始化程序,因为我很懒...)

并且,是的,通过将整个读数拉成单个链式表达式,可以写得更短:

istream& operator >>(istream& in, ComplexNumber& value) {
    int re;
    int im;
    char pm;
    char i;

    if (not (in >> re >> pm) or
        (pm != '+' and pm != '-') or
        not (in >> im >> i) or
        i != 'i')
    {
        in.setstate(ios::failbit);
        return in;
    }

    value = ComplexNumber {re, (pm == '-' ? -im : im)};
    return in;
}
Run Code Online (Sandbox Code Playgroud)

这是否更好取决于观众.就个人而言,我确实发现它比第一个版本更可读(!).一个更有条理的替代方案(对于这样一个简单的情况来说是过度杀戮)是Boost.Qi,它允许非常优雅的解析器构造.