一道关于C++复数类模板的题目,求解答 5

编写一个复数类模板Complex,其数据成员real和image的类型未知,定义相应的成员函数,包括构造函数、输出复数值的函数、求复数和的函数和求复数差的函数,主函数中定... 编写一个复数类模板Complex,其数据成员real和image的类型未知,定义相应的成员函数,包括构造函数、输出复数值的函数、求复数和的函数和求复数差的函数,主函数中定义模板类对象,分别以int和double实例化类型参数。 展开
 我来答
冰点青蛙
2011-04-14 · TA获得超过304个赞
知道小有建树答主
回答量:200
采纳率:0%
帮助的人:93.1万
展开全部
// CComplex.h: interface for the CComplex class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CComplex_H__0405C89D_644D_4500_B511_C1CA7F856E32__INCLUDED_)
#define AFX_CComplex_H__0405C89D_644D_4500_B511_C1CA7F856E32__INCLUDED_

#include<iostream>
#include<cmath>
using namespace std;

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CComplex
{
public:

void show();
double real;
double image;
public:

CComplex(){real=0;image=0;}
~CComplex(){}
CComplex(double a,double b){real=a;image=b;}
double Getreal(){return real;}
double Getimage(){return image;}
double abs(){return sqrt(real*real+image*image);}
CComplex operator +(CComplex &other);
CComplex operator +(const double &other);
CComplex operator +(const int &other);

CComplex operator -(CComplex &other);
CComplex operator -(const double &other);
CComplex operator -(const int &other);

CComplex operator *(CComplex &other);
CComplex operator *(const double &other);
CComplex operator *(const int &other);

CComplex operator /(CComplex &other);
CComplex operator /(const double &other);
CComplex operator /(const int &other);

void operator +=(CComplex &other);
void operator +=(const double &other);
void operator +=(const int &other);

void operator -=(CComplex &other);
void operator -=(const double &other);
void operator -=(const int &other);

void operator *=(CComplex &other);
void operator *=(const double &other);
void operator *=(const int &other);

void operator /=(CComplex &other);
void operator /=(const double &other);
void operator /=(const int &other);

CComplex operator =(CComplex &other);
CComplex operator =(const double &other);
CComplex operator =(const int &other);

bool operator ==(CComplex &other);
bool operator ==(const double &other);
bool operator ==(const int &other);

friend ostream& operator<<(ostream &os,CComplex &other);
friend istream& operator>>(istream &is,CComplex &other);

};

#endif // !defined(AFX_CComplex_H__0405C89D_644D_4500_B511_C1CA7F856E32__INCLUDED_)

// CComplex.cpp: implementation of the CComplex class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Complex.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

void CComplex::show()
{

if(real>0&&image<0)
printf("%g%gj",real,image);
else if(real>0&&image>0)
printf("%g+%gj",real,image);
else if(real<0&&image>0)
printf("%g+%gj",real,image);
else if(real<0&&image<0)
printf("%g%gj",real,image);
else if(real==0&&image!=0)
printf("%gj",image);
else if(real!=0&&image==0)
printf("%g",real);
else
printf("0");
}

CComplex CComplex::operator+(CComplex &other)
{
CComplex temp;
temp.real=real+other.real;
temp.image=image+other.image;
return temp;
}

CComplex CComplex::operator +(const double &other)
{

CComplex temp;
temp.real=real+other;
temp.image=image;
return temp;
}

CComplex CComplex::operator +(const int &other)
{
CComplex temp;
temp.real=real+(double)other;
temp.image=image;
return temp;
}

CComplex CComplex::operator-(CComplex &other)
{

CComplex temp;
temp.real=real-other.real;
temp.image=image-other.image;
return temp;
}

CComplex CComplex::operator -(const double &other)
{

CComplex temp;
temp.real=real-(double)other;
temp.image=image;
return temp;
}

CComplex CComplex::operator -(const int &other)
{

CComplex temp;
temp.real=real-(double)other;
temp.image=image;
return temp;
}
CComplex CComplex::operator*(CComplex &other)
{
CComplex temp;
temp.real=(real*other.real-image*other.image);
temp.image=(image*other.real+real*other.image);
return temp;

}

CComplex CComplex::operator *(const double &other)
{
CComplex temp;
temp.real=real*other;
temp.image=image*other;
return temp;
}

CComplex CComplex::operator *(const int &other)
{
CComplex temp;
temp.real=real*(double)other;
temp.image=image*(double)other;
return temp;
}

CComplex CComplex::operator/(CComplex &other)
{

CComplex temp;
temp.real=((real*other.real)+(image*other.image))/(other.real*other.real+other.image*other.image);
temp.image=((image*other.real)-(real*other.image))/(other.real*other.real+other.image*other.image);
return temp;

}

CComplex CComplex::operator /(const double &other)
{
CComplex temp;
temp.real=real/other;
temp.image=image/other;
return temp;

}

CComplex CComplex::operator /(const int &other)
{
CComplex temp;
temp.real=real/(double)other;
temp.image=image/(double)other;
return temp;
}

void CComplex::operator+=(CComplex &other)
{
this->real+=other.real;
this->image+=other.image;
}

void CComplex::operator +=(const double &other)
{

this->real+=other;
}

void CComplex::operator +=(const int&other)
{
this->real+=(double)other;
}

void CComplex::operator-=(CComplex &other)
{
this->real-=other.real;
this->image-=other.image;
}

void CComplex::operator -=(const double &other)
{

this->real-=other;
}

void CComplex::operator -=(const int &other)
{
this->real-=(double)other;
}

void CComplex::operator*=(CComplex &other)
{
this->real=(real*other.real-image*other.image);
this->image=(image*other.real+real*other.image);;
}

void CComplex::operator *=(const double &other)
{

this->real=real*other;
this->image=image*other;
}

void CComplex::operator *=(const int &other)
{
this->real=real*(double)other;
this->image=image*(double)other;
}

void CComplex::operator/=(CComplex &other)
{
this->real=((real*other.real)+(image*other.image))/(other.real*other.real+other.image*other.image);
this->image=((image*other.real)-(real*other.image))/(other.real*other.real+other.image*other.image);
}

void CComplex::operator /=(const double &other)
{

this->real=real/other;
this->image=image/other;
}

void CComplex::operator /=(const int &other)
{
this->real=real/(double)other;
this->image=image/(double)other;
}

CComplex CComplex::operator= (CComplex &other)
{

this->real=other.real;
this->image=other.image;
return *this;

}
CComplex CComplex::operator =(const double &other)
{
this->real=other;
this->image=image;
return *this;

}
CComplex CComplex::operator =(const int &other)
{
this->real=(double)other;
this->image=image;
return *this;
}

bool CComplex::operator ==(CComplex &other)
{

if(this->real=other.real&&this->image==other.image)
return true;
else return false;
}
bool CComplex::operator ==(const double &other)
{

if(this->real==other&&this->image==0)
return true;
else
return false;
}
bool CComplex::operator ==(const int &other)
{
if(this->real==(double)other&&this->image==0)
return true;
else
return false;
}

ostream& operator<<(ostream &os,CComplex &other)
{
other.show();
return cout;
}

istream& operator>>(istream &is,CComplex &other)
{
is>>other.real;
is>>other.image;
return cin;
}

// ComplexClass.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "Complex.h"

int main(int argc, char* argv[])
{

CComplex test1(10,6);
CComplex test2(5,3);

cout<<test1*test2<<endl;
cout<<test1+test2<<endl;
cout<<test1/test2<<endl;
cout<<test1-test2<<endl;

printf("Hello World!\n");
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2011-04-20
展开全部
clude <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>

#define PRECISION 0.0000000000000001 //要求精度
//我们知道float和double不能直接和0比较
//float的精度只有6位,double是15到16位

class CComplex{
public:
//构造函数
CComplex(double realPart_value = 0.0, double imagePart_value = 0.0)
{
realPart = realPart_value;
imagePart = imagePart_value;
}

//打印函数
void print() const
{
cout<<"RealPart:"<<realPart<<" ImagePart:"<<imagePart<<endl;
}

//重载‘+’
CComplex operator +(CComplex &c); //两个复数相加
CComplex operator +(double r); //复数和一个实数相加

//重载'-'
CComplex operator -(CComplex &c); //同上
CComplex operator -(double r);

//重载'*'
CComplex operator *(CComplex &c);
CComplex operator *(double r);

//重载'/'
CComplex operator /(CComplex &c);
CComplex operator /(double r);

private:
double realPart; //实部
double imagePart; //虚部

};

CComplex CComplex::operator +(CComplex &c)
{
CComplex temp;
temp.realPart = realPart + c.realPart;
temp.imagePart =imagePart + c.imagePart;
return temp;
}

CComplex CComplex::operator +(double c)
{
CComplex temp;
temp.realPart = realPart + c;
temp.imagePart = imagePart;
return temp;
}

CComplex CComplex::operator -(CComplex &c)
{
CComplex temp;
temp.realPart = realPart - c.realPart;
temp.imagePart =imagePart - c.imagePart;
return temp;
}

CComplex CComplex::operator -(double c)
{
CComplex temp;
temp.realPart = realPart - c;
temp.imagePart = imagePart;
return temp;
}

CComplex CComplex::operator *(CComplex &c)
{
CComplex temp;
temp.realPart = realPart * c.realPart - imagePart * c.imagePart;
temp.imagePart =realPart * c.imagePart + imagePart * c.realPart;
return temp;
}

CComplex CComplex::operator *(double c)
{
CComplex temp;
temp.realPart = realPart * c;
temp.imagePart = imagePart * c;
return temp;
}

CComplex CComplex::operator /(CComplex &c)
{
if((c.realPart >(-PRECISION) && c.realPart < PRECISION)
&& (c.imagePart > (-PRECISION) && c.imagePart < PRECISION))
{
cout<<"The divisor can not be zero!"<<endl;
exit(-1);
}
double div = c.realPart * c.realPart + c.imagePart * c.imagePart; //分母先提出来
CComplex temp;
temp.realPart = (realPart * c.realPart + imagePart * c.imagePart) / div;
temp.imagePart = (imagePart * c.realPart - realPart * c.imagePart) / div;
return temp;
}

CComplex CComplex::operator /(double c)
{
if(c >(-PRECISION) && c < PRECISION ) //被除数不能为零,其实太小了也不行,我们否则会溢出
{
cout<<"The divisor can not be zero!"<<endl;
exit(-1);
}
CComplex temp;
temp.realPart = realPart / c;
temp.imagePart = imagePart / c;
return temp;
}

//主函数和先关测试
int main(int argc, char* argv[])
{

CComplex c1(10.0, 10.0);
CComplex c2(10.0, 10.0);
c1.print();
c2.print();

CComplex temp(0, 0);
//加 ‘+’
temp = c1 + c2;
temp.print();

temp = c1 + 10;
temp.print();

//减 ‘-’
temp = c1 - c2;
temp.print();

temp = c1 - 10;
temp.print();

//乘 ‘*’
temp = c1 * c2;
temp.print();

temp = c1 * 10;
temp.print();

//除 ‘/’
temp = c1 / c2;
temp.print();

temp = c1 / 10;
temp.print();
return 0;
}
另外,团IDC网上有许多产品团购,便宜有口碑
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
itkd
2011-04-15
知道答主
回答量:9
采纳率:0%
帮助的人:6.5万
展开全部
一下代码好像可以有点用,自己看看。改改!

#include<iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex(double x=0,double y=0):real=x,imag=y{};
Complex add(Complex & A,Complex &);
Complex sub(Complex &,Complex &);
Complex mul(Complex &,Complex &);
Complex div(Complex &,Complex &);
void show();
void getreal()
{
cout<<"input real:";
cin>>real;
}
void getimag()
{
cout<<"input imag:";
cin>>imag;
}

};

#include"complex.h"
void Complex::show()
{ if(imag==0)
cout<<real<<endl;
else if(imag<0)
{
if(real==0)
cout<<imag<<'i'<<endl;
else
cout<<real<<imag<<'i'<<endl;
}
else
{
if(real==0)
cout<<imag<<'i'<<endl;
else
cout<<real<<'+'<<imag<<'i'<<endl;
}
}
/*void complex::set()
{
}*/

Complex Complex:: add(Complex &A,Complex & B)
{
Complex T;
T.real=A.real+B.real;
T.imag=A.imag+B.imag;
return T;
}
Complex Complex ::sub(Complex &A,Complex & B)
{
Complex T;
T.real=A.real-B.real;
T.imag=A.imag-B.imag;
return T;
}
Complex Complex:: mul(Complex &A,Complex & B)
{
Complex T;
T.real=A.real*B.real-A.imag*B.imag;
T.imag=A.real*B.imag+A.imag*B.real;
return T;
}
Complex Complex::div(Complex &A,Complex & B)
{
Complex T;
if(B.real==0 &&B.imag==0)
cout<<"分母为零无意义!"<<endl;
else
T.real=(A.real*B.real+A.imag*B.imag)/(B.real*B.real+B.imag*B.imag);
T.imag=(A.imag*B.real-A.real*B.imag)/(B.real*B.real+B.imag*B.imag);
return T;
}

#include"complex.h"
void main()
{
Complex x,y,z;
x.getreal();
x.getimag();
x.show();
y.getreal();
y.getimag();
y.show();
z=z.add(x,y);
z.show();
z=z.sub(x,y);
z.show();
z=z.mul(x,y);
z.show();
z=z.div(x,y);
z.show();

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
a36254094
2011-04-14 · TA获得超过208个赞
知道小有建树答主
回答量:196
采纳率:0%
帮助的人:176万
展开全部
struct ComplexNumber //复数的结构体
{
float imag;// 虚部
float real;//实部
};

class Comlpex1
{
public:
ComplexNumber *array; //定义一个结构体的数组指针

protected:
private:
};
复数的输入用FOR 分别传给
array[i].real
array[i].imag
其它运算自己弄吧
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
renchunyun
2011-04-14
知道答主
回答量:39
采纳率:0%
帮助的人:39.8万
展开全部
很简单的,从网上搜搜吧
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式