PTA---6-1 运算符重载

本题考虑对运算符进行重载。分别重载复数运算的+,-,*,/,=(赋值)运算符,以及比较大小的<=(复数1的模是否小于等于复数2的模)运算符,其中,比较运算符按复数的模进行比较。测试程序根据输入的mode值分别测试各个函数是否编写正确。

函数接口定义:

在这里描述函数接口:
i#include <iostream>
using namespace std;

class Complex {
    double real;
    double imag;
public:
    //构造函数
    Complex(double real=0, double imag=0);

    //operator+-*/=操作符函数
    Complex operator+(const Complex& c) const;
    Complex operator-(const Complex& c) const;
    Complex operator*(const Complex& c) const;
    Complex operator/(const Complex& c) const;
    Complex operator=(const Complex& c);

    //operator <=操作符函数
    bool operator<=(const Complex& c) const;

    //友元函数声明以帮助operator<<()函数访问Complex类的私有成员
    friend ostream& operator<<(ostream& out, const Complex& c);
};

//n个复数,按模从小到达排序
void bubble_sort(Complex[],int n);

bubble_sort函数按冒泡排序的算法对n个复数按模从小到大的顺序排序。

裁判测试程序样例

在这里给出函数被调用进行测试的例子:
int main() {
    double dReal1, dImag1, dReal2, dImag2;

    int mode;
    cin>>mode;
    cin>>dReal1>>dImag1>>dReal2>>dImag2;
    Complex c1(dReal1, dImag1);
    Complex c2(dReal2, dImag2);
    Complex c[6] = {c1,c2,c1+c2,c1-c2,c1*c2,c1/c2};
    switch(mode)
    {
        case 1: cout << c[0]<<" "<<c[1];break;
        case 2: cout << c[2];break;
        case 3: cout << c[3];break;
        case 4: cout << c[4];break;
        case 5: cout << c[5];break;
        case 6: bubble_sort(c,6);
                for(int i=0;i<6;i++)
                    cout<<c[i]<<" ";

    }

    return 0;
}
/* 请在这里填写答案 */

输入样例1:

在这里给出一组输入。例如:

1
1 2 3 4

输出样例1:

在这里给出相应的输出。例如:

1+2i 3+4i

注意:复数的输出,实部和虚部即使为0也输出。

输入样例2:

在这里给出一组输入。例如:

2
4 5 6 7

输出样例2:

在这里给出相应的输出。例如:

10+12i

注意:复数的输出,实部和虚部即使为0也输出。

这道题就是考察了运算符的重载和复数相关计算,要熟悉复数的运算规则。

//operator+-*/=操作符函数重载
Complex::Complex(double real1, double imag1) //类名前缀!!!注意构造函数和成员函数的位置不一样的
{
        real=real1;
        imag=imag1;
}
Complex Complex:: operator+(const Complex& c) const  //常函数,只能读不能写
    {
        Complex  temp;
        temp.real=c.real+this->real;    //this指针指向使用函数的对象
        temp.imag=c.imag+this->imag;
        return temp;
    }
    Complex Complex::operator-(const Complex& c) const
    {
        Complex temp;
        temp.real=this->real-c.real;    //注意this指针和形参c的位置,谁先谁后,结果是不一样的
        temp.imag=this->imag-c.imag;
        return temp;
    }
    Complex Complex::operator*(const Complex& c) const
    {
        Complex temp;
        temp.real=c.real*this->real-c.imag*this->imag; //要熟悉复数的乘法运算,如果看不明白可以先自己举个例子手算一下
        temp.imag=c.real*this->imag+c.imag*this->real;
        return temp;
    }
    Complex Complex::operator/(const Complex& c) const
    {
        Complex temp,temp0,temp1,temp2;   //除法运算也是一样的,因为要去分母,我先多定义两个临时对象放分子分母
        temp0.real=c.real;
        temp0.imag=-c.imag;    //这是为了化解分母而加的临时对象,实部不变,虚部为相反数,相乘后就去掉虚部了
        temp1=(*this)*temp0;   //分子的值,实部加虚部
        temp2=c*temp0;    //分母的值,只剩实部
        temp.real=temp1.real/temp2.real;  //最后得出结果
        temp.imag=temp1.imag/temp2.real;
        return temp;
    }
    Complex Complex::operator=(const Complex& c)
    {
         this->real=c.real;
         this->imag=c.imag;
    }

    //operator <=操作符函数
    bool Complex::operator<=(const Complex& c) const
    {
              if((this->real*this->real+this->imag*this->imag)>(c.real*c.real+c.imag*c.imag))
              {
                  return false;
              }
              else return true;
    }

    //友元函数声明以帮助operator<<()函数访问Complex类的私有成员
     ostream& operator<<(ostream& out, const Complex& c)
    {
        if(c.imag>=0)   {out<<c.real<<"+"<<c.imag<<"i";}   //注意判断虚部的正负
        else if(c.imag<0)   {out<<c.real<<c.imag<<"i";}
    }
void bubble_sort(Complex  c[],int n)  //bubble_sort函数按冒泡排序的算法对n个复数按模从小到大的顺序排序。
{
      for(int i=0;i<n;i++)                //简单的冒泡排序,双层for循环遍历数组
      {
          for(int j=0;j<n-1-i;j++)
          {
              if(c[j+1]<=c[j])
              {
                  Complex temp;
                  temp=c[j];
                  c[j]=c[j+1];
                  c[j+1]=temp;
              }
          }
      }
}

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值