我正在使用运算符重载实现一个复数。在程序中,用户总是以以下形式输入一个复数:
a + bi所以,举个例子...
25.0 + 3.6i假设用户总是输入复数的实部和虚部,例如,用户将输入"5 + 0i“(而不是"5")或"0 - 6.2i”(而不是"-6.2i")。
我的问题是,在main()中,我有以下代码:
ComplexNumber c1;
cin >> c1;
cout << c1;然后打印代码:
0 + 0i...when I在运行时在提示符中输入了"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对我的实现有任何帮助都是很好的。
发布于 2012-04-26 06:28:56
从本质上讲,您的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,它允许非常优雅的解析器构造。
发布于 2012-04-26 06:19:17
这部分:
string num;
in >> num;它只从输入中读取一个单词。您需要多次调用它才能读取像4.2 + 8.3i这样的内容,它有三个单词。
https://stackoverflow.com/questions/10324537
复制相似问题