class Complex{ double real, imag; public: Complex(double r=0, double i=0): real(r), imag(i){ } friend istream& operator>>(istream& in, Complex& c); //已实现 friend ostream& operator<<(ostream& out, const Complex& c);//已实现 friend Complex operator+(const Complex& c1, const Complex& c2); friend Complex operator-(const Complex& c1, const Complex& c2); Complex& operator+=(const Complex& c); Complex& operator-=(const Complex& c); };#include <iostream> using namespace std; int main() { Complex c1, c2; cin >> c1 >> c2; cout << c1 + c2 << endl; cout << c1 - c2 << endl; c1 += c2; cout << c1 << endl; c1 -= c2; cout << c1 << endl; return 0; } /* 请在这里填写答案,类的详细定义已由系统给出,并重载了输入输出运算符,你只需要写出需要重载的算术运算符函数即可 */
时间: 2023-06-17 11:05:44 浏览: 203
重载的算术运算符函数如下所示:
```c++
Complex operator+(const Complex& c1, const Complex& c2){
return Complex(c1.real+c2.real, c1.imag+c2.imag);
}
Complex operator-(const Complex& c1, const Complex& c2){
return Complex(c1.real-c2.real, c1.imag-c2.imag);
}
Complex& Complex::operator+=(const Complex& c){
real += c.real;
imag += c.imag;
return *this;
}
Complex& Complex::operator-=(const Complex& c){
real -= c.real;
imag -= c.imag;
return *this;
}
```
其中,`operator+` 重载了加法运算符,返回两个复数相加的结果;`operator-` 重载了减法运算符,返回两个复数相减的结果;`operator+=` 重载了复合赋值加法运算符,将一个复数加上另一个复数并返回结果;`operator-=` 重载了复合赋值减法运算符,将一个复数减去另一个复数并返回结果。
相关问题
用C++给下面的代码写运行部分。class COMPLEX {private: double real; double imag;public: COMPLEX(double r = 0, double i = 0) : real(r), imag(i) {} COMPLEX operator+(const COMPLEX& rhs) const { return COMPLEX(real + rhs.real, imag + rhs.imag); } COMPLEX operator-(const COMPLEX& rhs) const { return COMPLEX(real - rhs.real, imag - rhs.imag); } COMPLEX& operator=(const COMPLEX& rhs) { real = rhs.real; imag = rhs.imag; return *this; }};
以下是用C++编写的运行部分:
```c++
#include <iostream>
using namespace std;
class COMPLEX {
private:
double real;
double imag;
public:
COMPLEX(double r = 0, double i = 0) : real(r), imag(i) {}
COMPLEX operator+(const COMPLEX& rhs) const {
return COMPLEX(real + rhs.real, imag + rhs.imag);
}
COMPLEX operator-(const COMPLEX& rhs) const {
return COMPLEX(real - rhs.real, imag - rhs.imag);
}
COMPLEX& operator=(const COMPLEX& rhs) {
real = rhs.real;
imag = rhs.imag;
return *this;
}
};
int main() {
COMPLEX a(1, 2), b(3, 4), c;
c = a + b;
cout << c.real << " + " << c.imag << "i" << endl;
c = a - b;
cout << c.real << " + " << c.imag << "i" << endl;
return 0;
}
```
在上述代码中,我们定义了一个名为`COMPLEX`的类,它表示一个复数,具有实部和虚部两个成员变量。我们使用构造函数初始化这两个成员变量,并且使用运算符重载函数`operator+`和`operator-`分别实现了复数的加法和减法。同时,我们也定义了一个赋值运算符`operator=`,用于将一个复数赋值给另一个复数。
在`main()`函数中,我们创建了两个复数对象`a`和`b`,并将它们相加和相减,将结果分别赋值给复数对象`c`。最后,我们输出了`c`的实部和虚部,以验证我们的代码是否正确。
描述 请重载复数类的流插入和流提取运算符,实现复数类的输出和输入。 #include<iostream> #include <string> using namespace std; //--------------------- class Complex { private: double real,imag; public: Complex(){ real=imag=0; } Complex(double r){ real=r;imag=0; } Complex(double r,double i){ real=r;imag=i; } // 在此处补充你的代码 int main() { Complex c; cin>>c; cout<<c; } 输入 实部和虚部以英文逗号分隔 输出 实部+虚部j 样例输入 样例一:5.4,-9.0 样例二:5.4,0 样例输出 样例一:5.4-9j 样例二:5.4
-9j
可以使用友元函数来重载流插入和流提取运算符,如下所示:
```cpp
#include<iostream>
#include <string>
using namespace std;
class Complex {
private:
double real,imag;
public:
Complex(){real=imag=0;}
Complex(double r){real=r;imag=0;}
Complex(double r,double i){real=r;imag=i;}
friend ostream & operator<<(ostream & os,const Complex & c) {
if(c.imag == 0) {
os << c.real;
} else if(c.real == 0) {
os << c.imag << "j";
} else {
os << c.real << (c.imag > 0 ? "+" : "") << c.imag << "j";
}
return os;
}
friend istream & operator>>(istream & is,Complex & c) {
string str;
is >> str;
int pos = str.find(",");
if(pos == string::npos) {
c.real = stod(str);
c.imag = 0;
} else {
c.real = stod(str.substr(0,pos));
c.imag = stod(str.substr(pos+1));
}
return is;
}
};
int main() {
Complex c;
cin >> c;
cout << c;
return 0;
}
```
在流插入运算符中,我们需要判断实部和虚部的值,如果其中一个为0,则只输出另一个;如果两者都不为0,则需要输出加号或减号以及j。
在流提取运算符中,我们可以先读入一个字符串,然后用find函数查找逗号的位置,如果没有逗号则说明只有实部,否则就有实部和虚部。最后使用substr函数提取出实部和虚部的值,并将其转换为double类型。
阅读全文