展开全部
给你一个我以前写的,分别包含头文件和cpp文件。
.h文件如下:
#ifndef __Complex__
#define __Complex__
#include <iostream>
class Complex{
public://public部分为对外接口,不可增加、修改或删除
Complex();//a=0,b=0
Complex(const double a, const double b);
Complex(const double a);//b=0
~Complex();
const double getRealPart() const;//返回实部
const double getImaginaryPart() const;//返回虚部
Complex add(const Complex sc) const;//加法,sc作为第二操作数
Complex subtract(const Complex sc) const;//减法,sc作为第二操作数
Complex multiply(const Complex sc) const;//乘法,sc作为第二操作数
Complex divide(const Complex sc) const;//除法,sc作为除数
double abs() const;//返回绝对值
std::string toString() const;//转为字符串a+bi,如果虚部为0,只显示a
Complex& operator+=(const Complex& sc);
Complex& operator-=(const Complex& sc);
Complex& operator*=(const Complex& sc);
Complex& operator/=(const Complex& sc);
double& operator[](const int index);
const double& operator[](const int index) const;
Complex operator+() const;//对数据无实质改变
Complex operator-() const;//实部虚部取反
Complex& operator++();//对实部前置++
Complex operator++(int);//对实部后置++
Complex& operator--();//对实部前置--
Complex operator--(int);//对实部后置--
private://private部分为内部实现,可任意增加、修改或删除
double a;//实部
double b;//虚部
};
//全局变量和函数部分,不可增加、修改或删除
std::ostream& operator<<(std::ostream& ostr, const Complex& sc);//输出a+bi,支持文件输出
std::istream& operator>>(std::istream& ostr, Complex& sc);//输入实部虚部到sc,支持文件输入
Complex operator+(const Complex& sc1, const Complex& sc2);//加法
Complex operator-(const Complex& sc1, const Complex& sc2);//减法
Complex operator*(const Complex& sc1, const Complex& sc2);//乘法
Complex operator/(const Complex& sc1, const Complex& sc2);//除法
#endif /* defined(__Complex__) */
.cpp文件如下:
#include "Complex.h"
#include <cmath>
#include <stdlib.h>
#include <sstream>
using namespace std;
Complex :: Complex() { a=0; b=0; }
Complex :: Complex ( const double a, const double b ){
this->a = a;
this->b = b;
}
Complex :: Complex ( const double a ){
this->a = a;
b = 0;
}
Complex :: ~Complex(){
a = 0;
b = 0;
}
const double Complex :: getRealPart() const{
return a;
}
const double Complex :: getImaginaryPart() const{
return b;
}
Complex Complex :: add ( const Complex sc ) const{
return Complex( a+sc.a, b+sc.b );
}
Complex Complex :: subtract ( const Complex sc ) const{
return Complex(a-sc.a,b-sc.b);
}
Complex Complex::multiply ( const Complex sc ) const{
return Complex ( a*sc.a - b*sc.b, b*sc.a + a*sc.b );
}
Complex Complex :: divide ( const Complex sc ) const{
return Complex( ( a*sc.a + b*sc.b ) / ( sc.a*sc.a + sc.b*sc.b ) , (b*sc.a - a*sc.b ) / (sc.a*sc.a + sc.b*sc.b ) );
}
double Complex :: abs() const{
return pow ( (a*a + b*b ), 0.5);
}
std :: string Complex :: toString () const{
stringstream ss;
ss << a;
if ( b > 0 ) ss<< " + "<< b<< " i ";
else if ( b < 0 ) ss<< b<<" i ";
return ss.str();
}
Complex& Complex :: operator += ( const Complex& sc ){
*this = add(sc);
return *this;
}
Complex& Complex :: operator -= ( const Complex& sc ){
*this = subtract(sc);
return *this;
}
Complex& Complex :: operator *= ( const Complex& sc ){
*this = multiply(sc);
return *this;
}
Complex& Complex :: operator /= ( const Complex& sc ){
*this = divide(sc);
return *this;
}
double& Complex :: operator[] (const int index){
if ( index==0 ){ return a; }
else if ( index==1 ){ return b; }
}
const double& Complex :: operator[] (const int index) const{
if ( index == 0 ){ return a; }
else if ( index == 1 ){ return b; }
}
Complex Complex :: operator +() const{
return *this;
}
Complex Complex :: operator -() const{
return Complex(-a,-b);
}
Complex& Complex :: operator ++(){
a++;
return *this;
}
Complex Complex :: operator ++ ( int ){
Complex sc;
sc.a = a++;
sc.b = b;
return sc;
}
Complex& Complex :: operator--(){
a--;
return *this;
}
Complex Complex :: operator--(int){
Complex sc;
sc.a = a--;
sc.b = b;
return sc;
}
std :: ostream& operator << ( std :: ostream& ostr, const Complex& sc){
ostr <<sc.getRealPart();
if( sc.getImaginaryPart()>0)ostr <<"+" <<sc.getImaginaryPart() <<"i";
else if( sc.getImaginaryPart()<0)ostr <<sc.getImaginaryPart() <<"i";
return ostr;
}
std :: istream& operator >> ( std::istream& ostr, Complex& sc){
ostr >>sc[0] >>sc[1];
return ostr;
}
Complex operator + ( const Complex& sc1, const Complex& sc2){
return sc1.add(sc2);
}
Complex operator - ( const Complex& sc1, const Complex& sc2){
return sc1.subtract(sc2);
}
Complex operator * ( const Complex& sc1, const Complex& sc2){
return sc1.multiply(sc2);
}
Complex operator / ( const Complex& sc1, const Complex& sc2){
return sc1.divide(sc2);
}
望采纳
.h文件如下:
#ifndef __Complex__
#define __Complex__
#include <iostream>
class Complex{
public://public部分为对外接口,不可增加、修改或删除
Complex();//a=0,b=0
Complex(const double a, const double b);
Complex(const double a);//b=0
~Complex();
const double getRealPart() const;//返回实部
const double getImaginaryPart() const;//返回虚部
Complex add(const Complex sc) const;//加法,sc作为第二操作数
Complex subtract(const Complex sc) const;//减法,sc作为第二操作数
Complex multiply(const Complex sc) const;//乘法,sc作为第二操作数
Complex divide(const Complex sc) const;//除法,sc作为除数
double abs() const;//返回绝对值
std::string toString() const;//转为字符串a+bi,如果虚部为0,只显示a
Complex& operator+=(const Complex& sc);
Complex& operator-=(const Complex& sc);
Complex& operator*=(const Complex& sc);
Complex& operator/=(const Complex& sc);
double& operator[](const int index);
const double& operator[](const int index) const;
Complex operator+() const;//对数据无实质改变
Complex operator-() const;//实部虚部取反
Complex& operator++();//对实部前置++
Complex operator++(int);//对实部后置++
Complex& operator--();//对实部前置--
Complex operator--(int);//对实部后置--
private://private部分为内部实现,可任意增加、修改或删除
double a;//实部
double b;//虚部
};
//全局变量和函数部分,不可增加、修改或删除
std::ostream& operator<<(std::ostream& ostr, const Complex& sc);//输出a+bi,支持文件输出
std::istream& operator>>(std::istream& ostr, Complex& sc);//输入实部虚部到sc,支持文件输入
Complex operator+(const Complex& sc1, const Complex& sc2);//加法
Complex operator-(const Complex& sc1, const Complex& sc2);//减法
Complex operator*(const Complex& sc1, const Complex& sc2);//乘法
Complex operator/(const Complex& sc1, const Complex& sc2);//除法
#endif /* defined(__Complex__) */
.cpp文件如下:
#include "Complex.h"
#include <cmath>
#include <stdlib.h>
#include <sstream>
using namespace std;
Complex :: Complex() { a=0; b=0; }
Complex :: Complex ( const double a, const double b ){
this->a = a;
this->b = b;
}
Complex :: Complex ( const double a ){
this->a = a;
b = 0;
}
Complex :: ~Complex(){
a = 0;
b = 0;
}
const double Complex :: getRealPart() const{
return a;
}
const double Complex :: getImaginaryPart() const{
return b;
}
Complex Complex :: add ( const Complex sc ) const{
return Complex( a+sc.a, b+sc.b );
}
Complex Complex :: subtract ( const Complex sc ) const{
return Complex(a-sc.a,b-sc.b);
}
Complex Complex::multiply ( const Complex sc ) const{
return Complex ( a*sc.a - b*sc.b, b*sc.a + a*sc.b );
}
Complex Complex :: divide ( const Complex sc ) const{
return Complex( ( a*sc.a + b*sc.b ) / ( sc.a*sc.a + sc.b*sc.b ) , (b*sc.a - a*sc.b ) / (sc.a*sc.a + sc.b*sc.b ) );
}
double Complex :: abs() const{
return pow ( (a*a + b*b ), 0.5);
}
std :: string Complex :: toString () const{
stringstream ss;
ss << a;
if ( b > 0 ) ss<< " + "<< b<< " i ";
else if ( b < 0 ) ss<< b<<" i ";
return ss.str();
}
Complex& Complex :: operator += ( const Complex& sc ){
*this = add(sc);
return *this;
}
Complex& Complex :: operator -= ( const Complex& sc ){
*this = subtract(sc);
return *this;
}
Complex& Complex :: operator *= ( const Complex& sc ){
*this = multiply(sc);
return *this;
}
Complex& Complex :: operator /= ( const Complex& sc ){
*this = divide(sc);
return *this;
}
double& Complex :: operator[] (const int index){
if ( index==0 ){ return a; }
else if ( index==1 ){ return b; }
}
const double& Complex :: operator[] (const int index) const{
if ( index == 0 ){ return a; }
else if ( index == 1 ){ return b; }
}
Complex Complex :: operator +() const{
return *this;
}
Complex Complex :: operator -() const{
return Complex(-a,-b);
}
Complex& Complex :: operator ++(){
a++;
return *this;
}
Complex Complex :: operator ++ ( int ){
Complex sc;
sc.a = a++;
sc.b = b;
return sc;
}
Complex& Complex :: operator--(){
a--;
return *this;
}
Complex Complex :: operator--(int){
Complex sc;
sc.a = a--;
sc.b = b;
return sc;
}
std :: ostream& operator << ( std :: ostream& ostr, const Complex& sc){
ostr <<sc.getRealPart();
if( sc.getImaginaryPart()>0)ostr <<"+" <<sc.getImaginaryPart() <<"i";
else if( sc.getImaginaryPart()<0)ostr <<sc.getImaginaryPart() <<"i";
return ostr;
}
std :: istream& operator >> ( std::istream& ostr, Complex& sc){
ostr >>sc[0] >>sc[1];
return ostr;
}
Complex operator + ( const Complex& sc1, const Complex& sc2){
return sc1.add(sc2);
}
Complex operator - ( const Complex& sc1, const Complex& sc2){
return sc1.subtract(sc2);
}
Complex operator * ( const Complex& sc1, const Complex& sc2){
return sc1.multiply(sc2);
}
Complex operator / ( const Complex& sc1, const Complex& sc2){
return sc1.divide(sc2);
}
望采纳
更多追问追答
追问
请问有那种简单一点的算法嘛emmmm
追答
嗯,你可以去掉其中重载操作符,四则混合运算以及一些和你需求无关的操作,具体你可以看我头文件的注释,在cpp文件保留你需要的就可以了。这个也谈不上算法,你看每个函数单独也不是很复杂,只有两三行代码,主要是当时学校要求实现的功能比较完备,看着就比较麻烦
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询