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

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 
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 
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)"<

你可能感兴趣的:(PTA,C++,c++,面向对象编程)