+、-、*、/复数与整数运算符重载

4 篇文章 1 订阅

7-8 +、-、*、/运算符重载 (10 分)

编写程序实现+ - * / 运算符重载,主要功能如下: 1、实现两复数(c1与c2)的加减乘除运算 2、实现复数c1与整数num的加减乘除运算 3、实现整数num与复数c1的加减乘除运算

输入格式:

c1实部 c1虚部 c2实部 c2虚部 整数num 具体格式见样例

输出格式:

c1+c2结果 c1-c2结果 c1c2结果 c1/c2结果 c1+num结果 c1-num结果 c1num结果 c1/num结果 num+c1结果 num-c1结果 num*c1结果 num/c1结果 具体格式见输出样例

样例">样例">样例">样例">样例">输入样例:

1 2
3 4
5

输出样例:

c1+c2=(4.00,6.00i)
c1-c2=(-2.00,-2.00i)
c1*c2=(-5.00,10.00i)
c1/c2=(0.44,0.08i)
c1+num=(6.00,2.00i)
c1-num=(-4.00,2.00i)
c1*num=(5.00,2.00i)
c1/num=(0.20,2.00i)
num+c1=(6.00,2.00i)
num-c1=(4.00,2.00i)
num*c1=(5.00,2.00i)
num/c1=(5.00,2.00i)

本以为就是普通的复数运算,列出数学公式编程提交答案错误后才发现,整数与复数间的运算是定义新运算!也就是要再重载两遍,复数操作整数轻而易举。紧接着,问题来了!之前的运算符重载都是由复数调用的,也就是类的对象直接调用,此处需要整数调用复数的加减乘除就需要友元重载,可传入两个参数。


参考答案:

好繁琐,期待简洁答案!!

#include <iostream>
using namespace std;
class complex
{
    float real;
    float image;
public:
    complex() {}
    complex(float r, float i)
    {
        real = r;
        image = i;
    }
    float getr()
    {
        return real;
    }
    float geti()
    {
        return image;
    }
    complex operator+(const complex& c2)
    {
        complex c3;
        c3.real = real + c2.real;
        c3.image = image + c2.image;
        return c3;
    }
    complex operator-(const complex& c2)
    {
        complex c3;
        c3.real = real - c2.real;
        c3.image = image - c2.image;
        return c3;
    }
    complex operator*(const complex& c2)
    {
        complex c3;
        c3.real = real * c2.real - image * c2.image;
        c3.image = image * c2.real + real * c2.image;
        return c3;
    }
    complex operator/(const complex& c2)
    {
        complex c3;
        c3.real = (real * c2.real + image * c2.image) / (c2.real * c2.real + c2.image * c2.image);
        c3.image = (image * c2.real - real * c2.image) / (c2.real * c2.real + c2.image * c2.image);
        return c3;
    }

    complex operator+(int n)
    {
        complex c3;
        c3.real = real+n;
        c3.image = image;
        return c3;
    }
    complex operator-(int n)
    {
        complex c3;
        c3.real = real-n;
        c3.image = image;
        return c3;
    }
    complex operator*(int n)
    {
        complex c3;
        c3.real = real*n;
        c3.image = image;
        return c3;
    }
    complex operator/(int n)
    {
        complex c3;
        c3.real = real/n;
        c3.image = image;
        return c3;
    }

    friend complex operator+(int& n, const complex& c2)
    {
        complex c3;
        c3.real = n + c2.real;
        c3.image = c2.image;
        return c3;
    }
    friend complex operator-(int& n, const complex& c2)
    {
        complex c3;
        c3.real = n - c2.real ;
        c3.image=c2.image;
        return c3;
    }
    friend complex operator*(int& n, const complex& c2)
    {
        complex c3;
        c3.real = n * c2.real;
        c3.image = c2.image;
        return c3;
    }
    friend complex operator/(int&n,const complex& c2)
    {
        complex c3;
        c3.real =n/c2.real;
        c3.image =c2.image;
        return c3;
    }
};
int main()
{
    int r1, i1, r2, i2, r3;
    cin >> r1 >> i1 >> r2 >> i2 >> r3;
    complex c11(r1, i1), c22(r2, i2), c;
    cout.setf(ios::fixed);
    cout.precision(2);
    c = c11 + c22;
    cout << "c1+c2=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c11 - c22;
    cout << "c1-c2=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c11 * c22;
    cout << "c1*c2=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c11 / c22;
    cout << "c1/c2=(" << c.getr() << "," << c.geti() << "i)" << endl;

    c = c11 + r3;
    cout << "c1+num=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c11 -r3;
    cout << "c1-num=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c11 * r3;
    cout << "c1*num=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c11 /r3;
    cout << "c1/num=(" << c.getr() << "," << c.geti() << "i)" << endl;

    c = r3 + c11;
    cout << "num+c1=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = r3 - c11;
    cout << "num-c1=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = r3 * c11;
    cout << "num*c1=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = r3 / c11;
    cout << "num/c1=(" << c.getr() << "," << c.geti() << "i)" << endl;
}

 其实这不是正确的复数运算,只是为了这道“定义新运算”题的正解,如下给出一般意义上的复数运算代码:

#include <iostream>
using namespace std;
class complex
{
    float real;
    float image;
public:
    complex(){}
    complex(float r, float i)
    {
        real = r;
        image = i;
    }
    complex(float ri)
    {
        real = ri;
        image = 0;
    }
    float getr()
    {
        return real;
    }
    float geti()
    {
        return image;
    }
    complex operator+(const complex& c2)
    {
        complex c3;
        c3.real = real + c2.real;
        c3.image =image + c2.image;
        return c3;
    }
    complex operator-(const complex& c2)
    {
        complex c3;
        c3.real = real - c2.real;
        c3.image = image - c2.image;
        return c3;
    }
    complex operator*(const complex& c2)
    {
        complex c3;
        c3.real = real*c2.real-image*c2.image;
        c3.image = image*c2.real + real*c2.image;
        return c3;
    }
    complex operator/(const complex& c2)
    {
        complex c3;
        c3.real = (real * c2.real+image * c2.image)/(c2.real* c2.real+ c2.image* c2.image);
        c3.image =(image * c2.real - real * c2.image)/(c2.real * c2.real + c2.image * c2.image);
        return c3;
    }
};
int main()
{
    int r1, i1, r2, i2, r3;
    cin >> r1 >> i1 >> r2 >> i2 >> r3;
    complex c11(r1,i1), c22(r2,i2), c33(r3),c;
    cout.setf(ios::fixed);
    cout.precision(2);
    c = c11 + c22;
    cout << "c1+c2=(" << c.getr() << "," << c.geti() << "i)"<<endl;
    c = c11 - c22;
    cout << "c1-c2=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c11 * c22;
    cout << "c1*c2=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c11 / c22;
    cout << "c1/c2=(" << c.getr() << "," << c.geti() << "i)" << endl;

    c = c11 + c33;
    cout << "c1+num=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c11 - c33;
    cout << "c1-num=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c11 * c33;
    cout << "c1*num=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c11 / c33;
    cout << "c1/num=(" << c.getr() << "," << c.geti() << "i)" << endl;

    c = c33 + c11;
    cout << "num+c1=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c33 - c11;
    cout << "num-c1=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c33 * c11;
    cout << "num*c1=(" << c.getr() << "," << c.geti() << "i)" << endl;
    c = c33 / c11;
    cout << "num/c1=(" << c.getr() << "," << c.geti() << "i)" << endl;
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值