Complex Again



题目描述:

I know you have learned operator overloading. Your task is to finish this easy class Complex according to the function main.

class Complex {

      //A friend function to print Complex numbers like a+bi where a is the real part and b is the imaginary part
      
public:
   Complex(double = 0.0, double = 0.0);
   
   //Some function about operator overloading.

   void SetReal(double re){real = re;}
   void SetImag(double im){imag = im;}
private:
   double real; 
   double imag; 
};

 

tip:

You should first enter the real part and then the imaginary part.

That is , cin >> x1 >> y1; Complex C(x1,y1);  x1 is the real part and y1 is the imaginary part.

Sample Input

4 6 1 1

2 3

Sample Output

2+3i
4+6i
1+1i
5+7i
3+5i
-2+10i
5+1i
5+7i
4+6i
-2+10i
4+6i
1
0
代码:

 

#include <iostream>
#include "Complex.h"
using namespace std;
int main(int argc, const char *argv[]) {
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    // x1 and x2 are the real part, y1 and y2 are the imaginary part
    Complex a(x1, y1), b(x2, y2);
 
    Complex c = a;
    double c1, c2;
    // c1 is the real part and c2 is the imaginary part
    cin >> c1 >> c2;
 
    c.SetReal(c1);
    c.SetImag(c2);
 
    cout << c << endl;
    cout << a << endl;
 
    c = b;
    cout << c << endl;
 
    // a and b are not changed.
    cout << (a + b) << endl;
    cout << (a - b) << endl;
    cout << (a * b) << endl;
    cout << (a / b) << endl;
 
    a += b;
    cout << a << endl;
    a -= b;
    cout << a << endl;
    a *= b;
    cout << a << endl;
    a /= b;
    cout << a << endl;
 
    cout << (a == a) << endl;
    cout << (a != a) << endl;
 
    return 0;
}
#include <iostream>
#include "Complex.h"
using namespace std;
Complex::Complex(double real, double imag) {
  this->real = real;
  this->imag = imag;
}
 
Complex::Complex(const Complex& otherComplex) {
  real = otherComplex.real;
  imag = otherComplex.imag;
}
 
ostream& operator <<(ostream& out, const Complex& c) {
  out << c.real << "+" << c.imag << "i";
  return out;
}
 
Complex Complex:: operator +(const Complex& c) {
  double tmpreal = real + c.real;
  double tmpimag = imag + c.imag;
 
  Complex tmp(tmpreal, tmpimag);
 
  return tmp;
}
 
Complex Complex:: operator -(const Complex& c) {
  double tmpreal = real - c.real;
  double tmpimag = imag - c.imag;
 
  Complex tmp(tmpreal, tmpimag);
 
  return tmp;
}
 
Complex Complex:: operator *(const Complex& c) {
  double tmpreal = real * c.real - imag * c.imag;
  double tmpimag = imag * c.real + real * c.imag;
 
  Complex tmp(tmpreal, tmpimag);
 
  return tmp;
}
 
Complex Complex:: operator /(const Complex& c) {
  double t = c.real * c.real + c.imag * c.imag;
  double tmpreal = (real * c.real + imag * c.imag) / t;
  double tmpimag = (imag * c.real - real * c.imag) / t;
 
  Complex tmp(tmpreal, tmpimag);
 
  return tmp;
}
 
Complex& Complex:: operator = (const Complex& otherComplex) {
  real = otherComplex.real;
  imag = otherComplex.imag;
  return *this;
}
 
Complex& Complex:: operator += (const Complex& otherComplex) {
  *this = *this + otherComplex;
  return *this;
}
 
Complex& Complex:: operator -= (const Complex& otherComplex) {
  *this = *this - otherComplex;
  return *this;
}
 
Complex& Complex:: operator *= (const Complex& otherComplex) {
  *this = *this * otherComplex;
  return *this;
}
 
// a / b, b != 0
Complex& Complex:: operator /= (const Complex& otherComplex) {
  *this = *this / otherComplex;
  return *this;
}
 
bool Complex:: operator == (const Complex& otherComplex) {
  if (real == otherComplex.real && imag == otherComplex.imag)
    return true;
  else
    return false;
}
 
bool Complex:: operator != (const Complex& otherComplex) {
  return !(*this == otherComplex);
}

#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
class Complex {
  friend ostream& operator <<(ostream&, const Complex&);
 
 public:
  Complex(double = 0, double = 0);
  Complex(const Complex& otherComplex);
 
  Complex& operator = (const Complex& otherComplex);
  Complex& operator += (const Complex& otherComplex);
  Complex& operator -= (const Complex& otherComplex);
  Complex& operator *= (const Complex& otherComplex);
  Complex& operator /= (const Complex& otherComplex);
 
  Complex operator +(const Complex&);
  Complex operator -(const Complex&);
  Complex operator *(const Complex&);
  Complex operator /(const Complex&);
 
  bool operator == (const Complex& otherComplex);
  bool operator != (const Complex& otherComplex);
 
  void SetReal(double re) {
    real = re;
  }
  void SetImag(double im) {
    imag = im;
  }
 
 private:
  double real;
  double imag;
};
#endif



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值