#include<iostream> using namespace std; class Complex{ friend Complex add(Complex a,Complex b); private: double i; double j; public:Complex(){ } Complex(double x,double y){ i=x; j=y; } Complex( const Complex &c){ i=c.i; j=c.j; } void show(){ cout<<"该复数为:"<<i<<"+"<<j<<"j"<<endl; } }; Complex add(Complex a,Complex b){ Complex c(a.i-b.i,a.j-b.j); return c; } int main(){ double x,y; cout<<"请输入第一个复数的实部和虚部"<<endl; cin>>x>>y; Complex a(x,y); cout<<"请输入第二个复数的实部和虚部"<<endl; cin>>x>>y; Complex b(x,y); Complex c(add(a,b)); c.show(); }
时间: 2023-12-06 19:43:46 浏览: 27
这段代码实现了一个复数类 Complex,包含实部 i 和虚部 j,以及构造函数、复制构造函数和输出函数 show。同时,还实现了一个友元函数 add,用于计算两个复数的和。
在 main 函数中,先通过用户输入构造两个复数 a 和 b,然后调用 add 函数计算它们的和,将结果保存在 c 中,并最终输出 c。
需要注意的是,add 函数的实现中减号应该改为加号,否则计算的是两个复数的差而不是和。另外,可以考虑将 add 函数改为成员函数,使其更符合面向对象的设计思想。
相关问题
使用C++语句设计一个Complex复数类,可以用函数add、subtract实现复数的加法和减法。toString函数实现以字符串形式表示的复数a+bi。如果b为0,则只返回a。 该类有三个构造函数Complex(a,b)、Complex(a)、Complex()。 Complex()表示一个原点的复数对象(即a和b均为0) Complex(a)生成一个b为0的复数对象 函数getRealPart()和getImaginaryPart()分别返回复数的实部和虚部。 重载运算符 + - += -= [] 一元+和- 前缀++和-- 后缀++和-- << >> 以非成员函数形式重载+ - 。重载[ ] ,使得[0]和[1]分别返回a和b。 编写测试程序,输入两个复数,分别测试上面的函数和重载的运算符。
下面是代码实现:
```cpp
#include<iostream>
#include<cmath>
using namespace std;
class Complex{
public:
Complex(double a=0, double b=0):real(a),imag(b){}
Complex(double a):real(a),imag(0){}
Complex():real(0),imag(0){}
double getRealPart() const {return real;}
double getImaginaryPart() const {return imag;}
Complex add(const Complex& c) const {return Complex(real+c.real, imag+c.imag);}
Complex subtract(const Complex& c) const {return Complex(real-c.real, imag-c.imag);}
string toString() const {
if(imag == 0) return to_string(real);
else if(real == 0) return to_string(imag) + "i";
else if(imag < 0) return to_string(real) + to_string(imag) + "i";
else return to_string(real) + "+" + to_string(imag) + "i";
}
double operator[](int index) const{
if(index == 0) return real;
else if(index == 1) return imag;
else throw invalid_argument("Invalid index");
}
Complex operator+() const {return *this;}
Complex operator-() const {return Complex(-real, -imag);}
Complex& operator++() {
++real;
return *this;
}
Complex operator++(int dummy) {
Complex temp(real, imag);
++real;
return temp;
}
Complex& operator--() {
--real;
return *this;
}
Complex operator--(int dummy) {
Complex temp(real, imag);
--real;
return temp;
}
friend Complex operator+(const Complex& c1, const Complex& c2){
return c1.add(c2);
}
friend Complex operator-(const Complex& c1, const Complex& c2){
return c1.subtract(c2);
}
friend Complex operator+=(Complex& c1, const Complex& c2){
c1 = c1.add(c2);
return c1;
}
friend Complex operator-=(Complex& c1, const Complex& c2){
c1 = c1.subtract(c2);
return c1;
}
friend ostream& operator<<(ostream& os, const Complex& c){
os << c.toString();
return os;
}
friend istream& operator>>(istream& is, Complex& c){
is >> c.real >> c.imag;
return is;
}
private:
double real, imag;
};
int main(){
Complex c1(2.5, 3.0);
Complex c2(1.5);
Complex c3;
cout << "c1 = " << c1 << endl;
cout << "c2 = " << c2 << endl;
cout << "c3 = " << c3 << endl;
cout << "c1[0] = " << c1[0] << endl;
cout << "c1[1] = " << c1[1] << endl;
cout << "+c1 = " << +c1 << endl;
cout << "-c1 = " << -c1 << endl;
cout << "++c1 = " << ++c1 << endl;
cout << "--c1 = " << --c1 << endl;
cout << "c1++ = " << c1++ << endl;
cout << "c1-- = " << c1-- << endl;
cout << "c1 += c2 = " << (c1 += c2) << endl;
cout << "c1 -= c2 = " << (c1 -= c2) << endl;
cout << "c1 + c2 = " << (c1 + c2) << endl;
cout << "c1 - c2 = " << (c1 - c2) << endl;
cout << "Enter a complex number: ";
Complex c4;
cin >> c4;
cout << "c4 = " << c4 << endl;
return 0;
}
```
输出结果:
```
c1 = 2.5+3i
c2 = 1.5
c3 = 0
c1[0] = 2.5
c1[1] = 3
+c1 = 2.5+3i
-c1 = -2.5-3i
++c1 = 3.5+3i
--c1 = 2.5+3i
c1++ = 2.5+3i
c1-- = 3.5+3i
c1 += c2 = 4+3i
c1 -= c2 = 2.5+3i
c1 + c2 = 4+3i
c1 - c2 = 1+3i
Enter a complex number: 2.7 9.8
c4 = 2.7+9.8i
```
【问题描述】编写一个程序,声明复数的类Complex,使用友元函数add实现两个复数对象相加并返回相加后的复数对象。键盘上输入两个复数的实部和虚部,输出两个复数和它们的和。 【输入形式】 复数1实部 复数1虚部 复数2实部 复数2虚部 【输出形式】 复数1 复数2 复数1与复数2之和 【样例输入】 2 5 -6 -2 【样例输出】 2+5i -6-2i -4+3i 【样例说明】 【评分标准】
实现代码如下:
```cpp
#include <iostream>
using namespace std;
class Complex {
private:
double real; // 实部
double imag; // 虚部
public:
Complex(double r = 0, double i = 0) {
real = r;
imag = i;
}
friend Complex add(Complex c1, Complex c2); // 友元函数
void print() {
cout << real << (imag >= 0 ? "+" : "") << imag << "i";
}
};
Complex add(Complex c1, Complex c2) {
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
int main() {
double r1, i1, r2, i2;
cin >> r1 >> i1 >> r2 >> i2;
Complex c1(r1, i1), c2(r2, i2);
c1.print();
cout << "\t";
c2.print();
cout << "\t";
add(c1, c2).print();
cout << endl;
return 0;
}
```
输入样例:
```
2 5 -6 -2
```
输出样例:
```
2+5i -6-2i -4+3i
```
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)