| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1758 人关注过本帖
标题:复数类
只看楼主 加入收藏
lax521
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2005-8-17
收藏
 问题点数:0 回复次数:10 
复数类

今天总结了对复数类,包括各种运算的重载,希望大家多多指教


#include"stdio.h"
#include"iostream.h"
#include"math.h"

// class
class complex
{
private:

double a,b;
double _copy;

public:
double getreal();
double getimag();
double getZ();
complex(int x)
{
a=x;b=0;
}
complex(double av,double bv):a(av),b(bv)
{
}
complex()
{
}
complex(complex& _copy)
{
a=_copy.a;
b=_copy.b;
}
~complex()//disconstructor
{
}

friend ostream& operator<<(ostream& i,complex& j);
friend istream& operator>>(istream& g,complex& i);


friend complex operator+(complex& x,complex& y);
friend complex operator-(complex& x,complex& y);
friend complex operator*(complex& x,complex& y);
friend complex operator/(complex& x,complex& y);
complex operator+=(complex& y);
complex operator-=(complex& y);
complex operator=(complex& y);
void output()
{
cout<<"z="<<sqrt((a*a)+(b*b))<<endl;
}
void put()
{
cout<<"("<<a<<","<<b<<")"<<endl;
}

};
//output
ostream& operator<<(ostream& stream ,complex& x)
{
x.put();
return stream;
}

//input
istream& operator>>(istream& stream,complex& x)
{
cout<<"\treal=";
cin>>x.a;
cout<<"\timag=";
cin>>x.b;
return stream;
}
//add
complex operator+(complex& x,complex& y)
{
return complex(x.a+y.a,x.b+y.b);
}
//sub
complex operator-(complex& x,complex& y)
{
return complex(x.a-y.a,x.b-y.b);
}
//mul
complex operator*(complex& x,complex &y)
{
return complex((x.a*y.a)-(x.b*y.b),(x.a*y.b)+(x.b*y.a));
}
//div
complex operator/(complex& x,complex& y)
{
return complex(((x.a*y.a+x.b*y.b))/(y.a*y.a+y.b*y.b),(x.b*y.a-x.a*y.b)/(y.a*y.a+y.b*y.b));
}
//+=
complex complex::operator+=(complex& y)
{
this->a+=y.a;
this->b+=y.b;
return *this;
}
//-=
complex complex::operator-=(complex& y)
{
this->a-=y.a;
this->b-=y.b;
return *this;
}

//assignment
complex complex::operator=(complex& y)
{
this->a=y.a;
this->b=y.b;
return *this;
}
double complex::getreal()
{
return a;
}
double complex::getimag()
{
return b;
}
double complex::getZ()
{
return sqrt(a*a+b*b);
}

void main()
{

//class complex
cout<<"\n*****************************************\n";
cout<<"\n**********complex operator***************\n";
cout<<"\n*****************************************\n"<<endl;
complex p,q;
cin>>p;
cout<<p<<endl;
p.output();
cin>>q;
cout<<q<<endl;
q.output();


cout<<"copy constructor"<<endl;
complex _copy;
_copy=p;
_copy.put();

complex div,mul,sub,add;
cout<<"add a+b"<<endl;
add=q+p;
add.put();
cout<<"sub a-b"<<endl;
sub=q-p;
sub.put();
cout<<"mul a*b"<<endl;
mul=p*q;
mul.put();
cout<<"div a/b"<<endl;
div=q/p;
div.put();
cout<<"p+=q"<<endl;
p+=q;
p.put();
complex m,n;
cin>>m;
cout<<m<<endl;
cin>>n;
cout<<n<<endl;
cout<<"m-=n"<<endl;
m-=n;
m.put();
cout<<"p=q"<<endl;
p=q;
p.put();
cout<<"get real"<<endl;
complex _complex;
cin>>_complex;
_complex.getreal();
cout<<"get imag"<<endl;
cin>>_complex;
_complex.getimag();

}

搜索更多相关主题的帖子: double complex include class 
2006-03-11 20:54
sunnvya
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:1094
专家分:0
注 册:2005-11-23
收藏
得分:0 
我今天才学到这


路过
学习

http://www. 第二站>>>提供源码下载
2006-03-11 21:30
我爱璐璐
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-3-11
收藏
得分:0 

不错,最好再把复数的自增和自减运算符也重载了!
complex operator ++ (int);
complex operator -- (int);
complex & operator ++ ();
complex & operator -- ();


2006-03-12 00:20
lax521
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2005-8-17
收藏
得分:0 

没问题,如果你需要的话。

2006-03-12 15:31
lax521
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2005-8-17
收藏
得分:0 

应你要求,修改完毕。
#include"stdio.h"
#include"iostream.h"
#include"math.h"

// class
class complex
{
private:

double a,b;
double _copy;

public:
double getreal();
double getimag();
double getZ();
complex(int x)
{
a=x;b=0;
}
complex(double av,double bv):a(av),b(bv)
{
}
complex()
{
}
complex(complex& _copy)
{
a=_copy.a;
b=_copy.b;
}
~complex()//disconstructor
{
}

friend ostream& operator<<(ostream& i,complex& j);
friend istream& operator>>(istream& g,complex& i);


friend complex operator+(complex& x,complex& y);
friend complex operator-(complex& x,complex& y);
friend complex operator*(complex& x,complex& y);
friend complex operator/(complex& x,complex& y);
complex operator+=(complex& y);
complex operator-=(complex& y);
complex operator=(complex& y);
complex& operator--();
complex& operator++();
complex operator++(int);
complex operator--(int);

void output()
{
cout<<"z="<<sqrt((a*a)+(b*b))<<endl;
}
void put()
{
cout<<"("<<a<<","<<b<<")"<<endl;
}

};
//output
ostream& operator<<(ostream& stream ,complex& x)
{
x.put();
return stream;
}

//input
istream& operator>>(istream& stream,complex& x)
{
cout<<"\treal=";
cin>>x.a;
cout<<"\timag=";
cin>>x.b;
return stream;
}
//add
complex operator+(complex& x,complex& y)
{
return complex(x.a+y.a,x.b+y.b);
}
//sub
complex operator-(complex& x,complex& y)
{
return complex(x.a-y.a,x.b-y.b);
}
//mul
complex operator*(complex& x,complex &y)
{
return complex((x.a*y.a)-(x.b*y.b),(x.a*y.b)+(x.b*y.a));
}
//div
complex operator/(complex& x,complex& y)
{
return complex(((x.a*y.a+x.b*y.b))/(y.a*y.a+y.b*y.b),(x.b*y.a-x.a*y.b)/(y.a*y.a+y.b*y.b));
}
//+=
complex complex::operator+=(complex& y)
{
this->a+=y.a;
this->b+=y.b;
return *this;
}
//-=
complex complex::operator-=(complex& y)
{
this->a-=y.a;
this->b-=y.b;
return *this;
}

//assignment
complex complex::operator=(complex& y)
{
this->a=y.a;
this->b=y.b;
return *this;
}
complex& complex::operator++()
{
a++;
b++;
return *this;
}
complex& complex::operator--()
{
a--;
b--;
return *this;
}
complex complex::operator++(int)
{
complex temp=*this;
a++;
b++;
return temp;
}
complex complex::operator--(int)
{
complex temp=*this;
a--;
b--;
return temp;
}
double complex::getreal()
{
return a;
}
double complex::getimag()
{
return b;
}
double complex::getZ()
{
return sqrt(a*a+b*b);
}

void main()
{

//class complex
cout<<"\n*****************************************\n";
cout<<"\n**********complex operator***************\n";
cout<<"\n*****************************************\n"<<endl;
complex p,q;
cin>>p;
cout<<p<<endl;
p.output();
cin>>q;
cout<<q<<endl;
q.output();


cout<<"copy constructor"<<endl;
complex _copy;
_copy=p;
_copy.put();

complex div,mul,sub,add;
cout<<"add a+b"<<endl;
add=q+p;
add.put();
cout<<"sub a-b"<<endl;
sub=q-p;
sub.put();
cout<<"mul a*b"<<endl;
mul=p*q;
mul.put();
cout<<"div a/b"<<endl;
div=q/p;
div.put();
cout<<"p+=q"<<endl;
p+=q;
p.put();
complex m,n;
cin>>m;
cout<<m<<endl;
cin>>n;
cout<<n<<endl;
cout<<"m-=n"<<endl;
m-=n;
m.put();
cout<<"p=q"<<endl;
p=q;
p.put();
cout<<"get real"<<endl;
complex _complex;
cin>>_complex;
_complex.getreal();
cout<<"get imag"<<endl;
cin>>_complex;
_complex.getimag();
p++;
cout<<"p++="<<p<<endl;
complex newc;
cin>>newc;
++newc;
cout<<"++new="<<newc<<endl;
m--;
cout<<"m--="<<m<<endl;
--n;
cout<<"--n="<<n<<endl;

}

2006-03-12 15:48
woodhead
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:1124
专家分:0
注 册:2005-7-18
收藏
得分:0 
复数的++, --, 的数学含义是什么

2006-03-12 16:54
我爱璐璐
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-3-11
收藏
得分:0 

就是复数的实部和虚部各自进行自加运算,但一定要注意前自加与后自加的问题.这个复数类做得可以了,我也是个初学者,在做这个的时候又添加了一个取模,具体的代码如下:
operator double(); //取模运算的函数声明
complex::operator double() //取模运算的函数定义
{
return sqrt(a*a+b*b);
}


2006-03-12 22:48
ltliang
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-3-11
收藏
得分:0 
return complex(x.a+y.a,x.b+y.b);
这句有错误
2006-03-14 19:00
sunnvya
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:1094
专家分:0
注 册:2005-11-23
收藏
得分:0 
有出息
呵呵

http://www. 第二站>>>提供源码下载
2006-03-14 22:28
lax521
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2005-8-17
收藏
得分:0 

这个程序我在VC++6.0和BC 上都测试过了
都通过了,不能有错误吧!

2006-03-15 22:17
快速回复:复数类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052624 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved