关于输入运算符重载
程序代码:
#ifndef COMPLEX_H #define COMPLEX_H #include <iostream> #include <string> using namespace std; class Complex { public: friend ostream &operator<<( ostream &,const Complex &); friend istream &operator>>( istream &, Complex & ); Complex operator+(const Complex ); Complex operator-( const Complex ); Complex operator*( const Complex ); Complex operator/( const Complex ); bool operator==(const Complex ); bool operator!=( const Complex ); private: double realpart; double imaginarypart; }; #endif
这是头文件,然后是输入输出(其他就没有贴了,因为编译通过但是输出输入出现了问题)
程序代码:
ostream &operator<<( ostream &output , const Complex &c) { output << "(" << c.realpart << ", " << c.imaginarypart << ")"; return output; } istream &operator>>( istream &input , Complex &c) { input.ignore(); cin >> c.realpart ; input.ignore(2); cin >> c.imaginarypart; input.ignore(); return cin; }
这是cpp。
我的希望输入样式
(0, 0)
(4.3, 8.2)
(3.2 9.4)
(2.1, 9.2)
但是输到第二个结束后就直接停止输入了,输出一堆东西来
Enter a complex number in the form: (a, b)
? (0, 0)
x: (0, 0)
y: (9.2, 8.2)
z: k:
x=y+z:
(-1.53603e-41, 9.72077e-270) = (0, 4.85575e-270) + (-1.53603e-41, 4.86502e-270)
x=y-z:
(1.53603e-41, -9.27509e-273) = (0, 4.85575e-270) - (-1.53603e-41, 4.86502e-270)
x=y*z:
(-0, 0) = (0, 4.85575e-270) * (-1.53603e-41, 4.86502e-270)
x=y/z:
(-0, 0.998094) = (0, 4.85575e-270) / (-1.53603e-41, 4.86502e-270)
(-0, 0.998094) != (-0.611574, -2.87136e-42)
(-0.611574, -2.87136e-42) == (-0.611574, -2.87136e-42)
问题如上图? (0, 0)
x: (0, 0)
y: (9.2, 8.2)
z: k:
x=y+z:
(-1.53603e-41, 9.72077e-270) = (0, 4.85575e-270) + (-1.53603e-41, 4.86502e-270)
x=y-z:
(1.53603e-41, -9.27509e-273) = (0, 4.85575e-270) - (-1.53603e-41, 4.86502e-270)
x=y*z:
(-0, 0) = (0, 4.85575e-270) * (-1.53603e-41, 4.86502e-270)
x=y/z:
(-0, 0.998094) = (0, 4.85575e-270) / (-1.53603e-41, 4.86502e-270)
(-0, 0.998094) != (-0.611574, -2.87136e-42)
(-0.611574, -2.87136e-42) == (-0.611574, -2.87136e-42)
求大神搭救啊