您的位置:首页 > 其它

1553 simple complex class

2017-05-28 14:59 274 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: