我正在使用运算符重载实现复数。在程序中,用户以以下形式输入复数ALWAYS:
a + bi
所以,在例子......
25.0 + 3.6i
假设用户将始终输入复数的实部和虚部,例如,用户将输入“5 + 0i”(而不是“5”)或“0 - 6.2i”(而不是“-6.2i”)。
我的问题是在main()中我有以下代码:
ComplexNumber c1;
cin >> c1;
cout << c1;
并打印代码:
0 + 0i
...当我在运行时输入“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;
}
以下是operator<<
类的实现:
ostream & operator<<(ostream & out, const ComplexNumber & n) {
n.print(out);
return out;
}
这是我对ComplexNumber成员类print()的实现:
void ComplexNumber::print(ostream & out) const {
if (imag >= 0)
out << real << " + " << imag << "i";
else
out << real << " - " << (-1 * imag) << "i";
}
这是我的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
对我实施的任何帮助都会很棒。
答案 0 :(得分: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;
}
(我使用的是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;
}
这是否更好取决于观众。就个人而言,我确实发现它比第一个版本更可读(!)。一个更有条理的替代方案(对于这样一个简单的情况来说会有点过分)是Boost.Qi,它允许非常优雅的解析器构造。
答案 1 :(得分:1)
这部分:
string num;
in >> num;
它只从输入中读取一个单词。您需要多次调用它来阅读4.2 + 8.3i
之类的内容,其中包含三个单词。