1553 simple complex class
来源:互联网 发布:精品电玩手游刷分软件 编辑:程序博客网 时间:2024/06/15 18:18
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_HPPclass 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_HPPclass 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;}
阅读全文
0 0
- 1553 simple complex class
- A simple Complex class
- A simple C++ complex class
- Class Complex
- class Complex
- The Simple Complex
- Simple vs Complex
- The complex class
- 复数类:class Complex
- 复数类模板(Complex Class)
- Favor the Simple Over the Complex
- A simple perl class
- simple string class
- Simple C# FTP Class
- servlet-class :cvc-complex-type.2.4.a
- 【Deep learning vs BPL】思考:complex => simple => rich
- complex
- Complex
- Codeforces 459E. Pashmak and Graph (DP)
- linux gcc
- windows和Ubuntu14.04间通过vmware tools实现文件共享的问题
- 1019. 数字黑洞 (20)
- Shader学习笔记(2)- 基本概念与基础效果(1)
- 1553 simple complex class
- Java常用的八种排序算法与代码实现
- 机器笨猫的日记
- Java的特点
- 灰度图像的一阶和二阶导数代码实现
- bzoj2620[Usaco2012 Mar]Haybale Restacking
- 探索关系抽取中的多变知识
- 模拟——洛谷P1185 绘制二叉树
- SCU-4396 麦野沉利与御坂美琴的战斗