Complex类

BJFU-OJ 程序设计 C++实验题

Complex类

描述

实现以下复数类Complex,通过运算符重截,实现复数的输入输出以及相关运算。

class Complex

{

private:

double x;

double y;

public:

Complex(double x = 0.0, double y = 0.0);

Complex & operator+=(const Complex &);

Complex & operator-=(const Complex &);

Complex & operator*=(const Complex &);

Complex & operator/=(const Complex &);

friend Complex operator+(const Complex &, const Complex &);

friend Complex operator-(const Complex &, const Complex &);

friend Complex operator*(const Complex &, const Complex &);

friend Complex operator/(const Complex &, const Complex &);

friend bool operator==(const Complex &, const Complex &);

friend bool operator!=(const Complex &, const Complex &);

friend ostream & operator<<(ostream &, const Complex &);

friend istream & operator>>(istream &, Complex &);

};

通过以下主函数测试:

int main()

{

Complex c1, c2;

cin >> c1 >> c2;

cout << "c1 = " << c1 << “\n” << "c2 = " << c2 << 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 << (c1 += c2) << endl;

cout << (c1 -= c2) << endl;

cout << (c1 *= c2) << endl;

cout << (c1 /= c2) << endl;

cout << (c1 == c2) << " " << (c1 != c2) << endl;

return 0;

}

输入

输入有两行,每行输入两个表示复数c1和c2的浮点数。

输出

输出一共有11行,分别表示复数之间的各项操作,具体参见主函数和输出样例

输入样例 1

-4 6
2 5

输出样例 1

c1 = -4 + 6i
c2 = 2 + 5i
c1+c2 = -2 + 11i
c1-c2 = -6 + 1i
c1*c2 = -38 + -8i
c1/c2 = 0.758621 + 1.10345i
-2 + 11i
-4 + 6i
-38 + -8i
-4 + 6i
0 1

提示

复数加法公式:(a + bi) + (c + di) = (a + c) + (b + d)i

复数减法公式:(a + bi) - (c + di) = (a - c) + (b - d)i

复数乘法公式:(a + bi) * (c + di) = (ac - bd) + (ad + bc)i

复数除法公式:(a + bi) / (c + di) = [(ac + bd) / (c * c + d * d)] + [(bc - ad) / (c * c + d * d)]i

#include <iostream>
using namespace std;

class Complex
{
   
private:
    double x;
    double y;
public:
    Complex(double x = 0.0, double y = 0.0);
    Complex& operator+=(const Complex&);
    Complex& operator-=(const Complex&);
    Complex& operator*=(const Complex&);
    Complex& operator/=(const Complex&);
    <
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值