|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 栗粒粒 于 2020-5-6 17:51 编辑
在重写输入运算符时,如果用户需要输入一个虚数(3 + 8i),那么用户可能直接输入3 或者8i这样的实数或者纯虚数,(如果是纯虚数就把real设置为0,实数就把imaginary设置为0)这个时候要怎么判断呢
- class Complex
- {
- public:
- friend ostream & operator<<(ostream &os, const Complex &c);
- friend istream & operator>>(istream &is, Complex &c);
-
- private:
- int real,imaginary;
- char a,b;
-
- };
- ostream &operator<<(ostream &os, const Complex &c)
- {
- if(c.a == '+')
- os<<c.real<<" + "<<c.imaginary<<"i";
- else
- os<<c.real<<" - "<<c.imaginary<<"i";
- return os;
- }
- istream & operator>>(istream &is , Complex &c)
- {
- is>>c.real;
- //不知道接下去怎么写
- return is;
- }
- #endif // COMPLEX_H_INCLUDED
复制代码 |
|