1553 simple complex class

Descripe

You need to define a class named complex which has private two members, one is real which represents the real part of the complex, and another is imag which represents the imaginary part of the complex, both of them are integer;
The class has some member functions:
void display() const;
//print the complex with this form like 3 + 4i, if the real part and the imaginary part both are 0, you need to print 0;
double getModuli() const;
//like the function’s name, you need to return the moduli of the complex;
What’s more, you need to finish two friend function, overload “+” and “-“, the rule follow the rules of the complex.

Sample Input

0 -3
5 2

Sample Output

-3i
5+2i
5.09902 7.07107
5-1i
-5-5i

Provided Codes

main.cpp

#include <iostream>
#include "complex.hpp"

using namespace std;

int main() {
  int real, imag;
  cin >> real >> imag;
  complex a(real, imag);
  a.display();
  cout << endl;
  cin >> real >> imag;
  complex b(real, imag);
  b.display();
  cout << endl;
  complex c = a + b;
  complex d = a - b;
  cout << c.getModuli() << " " << d.getModuli() << endl;
  c.display();
  cout << endl;
  d.display();
}

Submission

complex.hpp

#ifndef COMPLEX_HPP
#define COMPLEX_HPP

class complex{
    public:
        complex(int ,int );
        void display() const;
        friend complex operator+(complex& ,complex& );
        friend complex operator-(complex& ,complex& );
        double getModuli() const; 
    //private:
        int real;
        int imag;
};

#endif

complex.cpp

#include <iostream>
#include <cmath>
#include "complex.hpp"

using namespace std;

complex operator+(complex& a,complex& b){
    return complex(a.real+b.real,a.imag+b.imag);
}

complex operator-(complex& a,complex& b){
    return complex(a.real-b.real,a.imag-b.imag);
}

complex::complex(int r,int i):
    real(r),imag(i){

} 

void complex::display() const{
    if(real!=0){
        cout<<real;
        if(imag>0){
            cout<<"+";
        }
    }   
    if(imag!=0){
        cout<<imag<<"i";
    }
    if(real==0&&imag==0){
        cout<<"0";
    }
}

double complex::getModuli() const{
    return sqrt(real*real+imag*imag);
}

Standard Answer

complex.hpp

#ifndef COMPLEX_HPP
#define COMPLEX_HPP

class complex {
public:
  complex();
  ~complex();
  complex(int real, int imag);
  double getModuli() const;
  void display() const;
  friend complex operator +(const complex& a, const complex& b);
  friend complex operator -(const complex& a, const complex& b);
private:
  int real;
  int imag;
};

#endif

complex.cpp

#include "complex.hpp"
#include <iostream>
#include <sstream>
#include <cmath>

using namespace std;

complex::complex() {}

complex::complex(int real, int imag) {
  this->real = real;
  this->imag = imag;
}

complex::~complex() {}

double complex::getModuli() const {
  return sqrt(pow(double(this->real), 2.0) + pow(double(this->imag), 2.0));
}

void complex::display() const {
  if (!(this->real == 0 && this->imag != 0)) {
    cout << this->real;
  }
  cout << ((this->imag > 0 && this->real != 0)? "+" : "");
  if (this->imag != 0) {
    cout << this->imag;
  }
  cout << (this->imag == 0? "" : "i");
}

complex operator + (const complex& a, const complex& b) {
  complex temp(a.real + b.real, a.imag + b.imag);
  return temp;
}

complex operator - (const complex& a, const complex& b) {
  complex temp(a.real - b.real, a.imag - b.imag);
  return temp;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值