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 -35 2
Sample Output
-3i5+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; }
相关文章推荐
- A simple Complex class
- A simple C++ complex class
- 面向对象复数类(class Complex) 的重载运算符 一元二元的实现与使用
- Tomcat6环境JBPM4.4报错:java.lang.ClassNotFoundException: de.odysseus.el.util.SimpleResolver
- A simple class to play sound on netcf
- Asterisk::AGI - Simple Asterisk Gateway Interface Class
- class.getName()和class.getSimpleName()的区别
- generate a simple class for all fields of a table .
- Class.getSimpleName()的作用
- The Simple Complex
- A simple IOCP Server/Client Class
- c++ simple class template example: Stack
- Tomcat6环境JBPM4.4报错:java.lang.ClassNotFoundException: de.odysseus.el.util.SimpleResolver
- A simple class to play sound on netcf (part 2)
- java后端解决跨域问题 新建一个filter 复制代码 public class SimpleCORSFilter implements Filter{ @Override
- Error inflating class com.facebook.drawee.view.SimpleDraweeView问题的解决
- Simple Worker Thread Class
- getClass()和getSimpleName()的区别和作用
- Java Class 介绍 canonical name, simple name and class name
- using class in c#(simple)