20140409 Complex again(for lab)

Description

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



main.h

#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;
}



Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H
using namespace std;
class Complex;
ostream& operator << (ostream& out, const Complex& comp);
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);
   friend ostream& operator << (ostream& out, const Complex& comp);
   
   Complex operator = (const Complex& other);
   Complex operator + (const Complex& other);
   Complex operator - (const Complex& other);
   Complex operator * (const Complex& other);
   Complex operator / (const Complex& other);
   void operator += (const Complex& other);
   void operator -= (const Complex& other);
   void operator *= (const Complex& other);
   void operator /= (const Complex& other);
   bool operator == (const Complex& other);
   bool operator != (const Complex& other);
   //Some function about operator overloading.

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



Complex.cpp

#include <iostream>
#include "Complex.h"
using namespace std;



Complex::Complex(double a, double b) : real(a), imag(b) {}



ostream& operator << (ostream& out, const Complex& comp) {
      if (comp.imag > 0)
      out << comp.real << "+" << comp.imag << "i";
      else if (comp.imag < 0)
      out << comp.real << "-" << (0-comp.imag) << "i";

}
Complex Complex::operator + (const Complex& other) {
       Complex tmp;
       tmp.real = real + other.real;
       tmp.imag = imag + other.imag;
       return tmp;
       }
Complex Complex::operator - (const Complex& other) {
       Complex tmp;
       tmp.real = real - other.real;
       tmp.imag = imag - other.imag;
       return tmp;
       }
Complex Complex::operator * (const Complex& other) {
       Complex tmp;
       tmp.real = real*other.real - imag*other.imag;
       tmp.imag = real*other.imag + imag*other.real;
       return tmp;
       }
Complex Complex::operator / (const Complex& other) {
       Complex tmp;
       tmp.real = (real*other.real + imag*other.imag);
       tmp.real = tmp.real/(other.real*other.real+other.imag*other.imag);
       tmp.imag = imag*other.real - real*other.imag;
       tmp.imag = tmp.imag/(other.real*other.real+other.imag*other.imag);
       return tmp;
       }
void Complex::operator += (const Complex& other) {
     real = real + other.real;
     imag = imag + other.imag;
}
void Complex::operator -= (const Complex& other) {
     real = real - other.real;
     imag = imag - other.imag;
       }
void Complex::operator *= (const Complex& other) {
       double a, b;
     a = real * other.real - imag * other.imag;
     b = real * other.imag + imag * other.real;
     real = a;
     imag = b;
       }
void Complex::operator /= (const Complex& other) {
    double a, b;
    a = (real*other.real + imag*other.imag);
    a = a/(other.real*other.real+other.imag*other.imag);
    b = imag*other.real - real*other.imag;
    b = b/(other.real*other.real+other.imag*other.imag);
    real = a;
    imag = b;
}
bool Complex::operator == (const Complex& other) {
       if (real == other.real && imag == other.imag)
       return true;
       else
       return false;
       }
bool Complex::operator != (const Complex& other) {
       if (real != other.real || imag != other.imag)
       return true;
       else
       return false;
       }
       // Some function about operator overloading.


这道题比较简单,主要是运算符重载和友元

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值