Class Complex
2009-04-21 15:36
148 查看
///////////The delarcation///////////
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
#include <fstream>
using namespace std;
class Complex
{
public:
Complex(){real = 1; img = 1;}
Complex(int nReal, int nImg);
~Complex(){}
Complex const operator ! ();
Complex& operator ++ ();
Complex const operator ++ (int);
Complex const operator -- ();
Complex const operator -- (int);
friend Complex const operator += ( Complex &x, Complex &y)
{
x = x + y;
return x;
}
Complex const operator + (Complex &x);
Complex const operator - (Complex &x);
friend Complex const operator -= ( Complex &x, Complex &y)
{
x = x - y;
return x;
}
double const operator * (Complex &x);
bool const operator > (Complex &x);
bool const operator < (Complex &x);
bool const operator == (const Complex &x);
friend Complex const operator / (const Complex &x,const Complex &y)
{
if (y.real == 0 && y.img == 0)
{
cerr<<"Divled by zero!/n";
exit(1);
}
double temReal=0,temImg=0;
temReal = (y.real * x.real - y.img * x.img) / ( y.real * y.real - y.img * y.img);
temImg = y.real * x.img - x.real * y.img ;
return Complex ( temReal, temImg);
}
friend const ostream & operator << (ostream &out, const Complex &x)
{
out<<"Number: "<<x.real<<"+";
out<<((x.img>=0)?"":"(")<<x.img<<(x.img>=0?"":")")<<"i"<<endl;
return out;
}
friend const istream & operator >> (istream &in, Complex &x)
{
in>>x.real>>x.img;
return in;
}
void ShowInfo();
private:
int real;
int img;
};
/*ostream & operator << (ostream &out, const Complex &x)
{
out<<"Number: "<<x.real<<"+"<<x.img<<"i"<<endl;
return out;
}
istream & operator >> (istream &in, Complex &x)
{
in>>x.real>>x.img;
return in;
}*/
#endif
////the end of file//////////
////////the definition///////////
#include "Complex.h"
#include <math.h>
Complex::Complex(int nReal, int nImg)
{
real = nReal;
img = nImg;
}
void Complex::ShowInfo()
{
if (img > 0 )
{
cout<<"Number :"<<real<<"+"<<img<<"i"<<endl;
}
else
cout<<"Number :"<<real<<"+"<<"("<<img<<")"<<"i"<<endl;
}
Complex const Complex::operator +(Complex &x)
{
Complex tem;
tem.real = this->real + x.real;
tem.img = this->img + x.img;
return tem;
}
Complex const Complex::operator - (Complex &x)
{
Complex tem;
tem.real = this->real - x.real;
tem.img = this->img - x.img;
return tem;
}
Complex const Complex::operator ! ()
{
return Complex(-this->real,-this->img);
}
double const Complex::operator * (Complex &x)
{
double result=0;
if( (this->img>0)&&(x.img>0) || (this->img<0)&&(x.img<0) ) //same value
{
result = this->real*x.real - this->img*x.img;
return result;
}
else
result = this->real*x.real + this->img*x.img;
return result;
}
bool const Complex::operator == (const Complex &x)
{
if( (this->real == x.real) && (this->img == x.img))
return true ;
else
return false;
}
Complex const Complex::operator ++ (int)
{
Complex temp(*this);
real++;
img++;
return temp;
}
Complex & Complex::operator ++()
{
real++;
img++;
return *this;
}
Complex const Complex::operator --()
{
real--;
img--;
return *this;
}
Complex const Complex::operator --(int)
{
Complex temp(*this);
real--;
img--;
return temp;
}
bool const Complex::operator < (Complex &x)
{
if( ( pow(this->real,2) - pow(this->img,2) )
< ( pow(x.real,2) - pow(x.img,2) ) )
return true;
else
return false;
}
bool const Complex::operator > (Complex &x)
{
if( ( pow(this->real,2) - pow(this->img,2) )
> ( pow(x.real,2) - pow(x.img,2) ) )
return true;
else
return false;
}
///Test Function////////
#include "Complex.h"
void main()
{
Complex my(1,2),you(2,-5);
Complex result;
Complex test1(1,0),test2(2,0);
result = my + you;
my.ShowInfo();
you.ShowInfo();
result.ShowInfo();
cout<<my;
cout<<you;
cout<<!result;
cout<<((my == you)?"Right!/n":"Wrong!/n");
result = test1/my;
cout<<result;
cout<<my;
cout<<you;
my+=you;
cout<<my;
cin>>my;
my-=you;
cout<<my;
Complex *ptCplex = new Complex[100];
for ( int i = 0; i < 100; i++)
{
//ptCplex[i].real = i * 10;
//ptCplex[i].img = i * i ;
ptCplex[i].ShowInfo();
}
delete []ptCplex;
}
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
#include <fstream>
using namespace std;
class Complex
{
public:
Complex(){real = 1; img = 1;}
Complex(int nReal, int nImg);
~Complex(){}
Complex const operator ! ();
Complex& operator ++ ();
Complex const operator ++ (int);
Complex const operator -- ();
Complex const operator -- (int);
friend Complex const operator += ( Complex &x, Complex &y)
{
x = x + y;
return x;
}
Complex const operator + (Complex &x);
Complex const operator - (Complex &x);
friend Complex const operator -= ( Complex &x, Complex &y)
{
x = x - y;
return x;
}
double const operator * (Complex &x);
bool const operator > (Complex &x);
bool const operator < (Complex &x);
bool const operator == (const Complex &x);
friend Complex const operator / (const Complex &x,const Complex &y)
{
if (y.real == 0 && y.img == 0)
{
cerr<<"Divled by zero!/n";
exit(1);
}
double temReal=0,temImg=0;
temReal = (y.real * x.real - y.img * x.img) / ( y.real * y.real - y.img * y.img);
temImg = y.real * x.img - x.real * y.img ;
return Complex ( temReal, temImg);
}
friend const ostream & operator << (ostream &out, const Complex &x)
{
out<<"Number: "<<x.real<<"+";
out<<((x.img>=0)?"":"(")<<x.img<<(x.img>=0?"":")")<<"i"<<endl;
return out;
}
friend const istream & operator >> (istream &in, Complex &x)
{
in>>x.real>>x.img;
return in;
}
void ShowInfo();
private:
int real;
int img;
};
/*ostream & operator << (ostream &out, const Complex &x)
{
out<<"Number: "<<x.real<<"+"<<x.img<<"i"<<endl;
return out;
}
istream & operator >> (istream &in, Complex &x)
{
in>>x.real>>x.img;
return in;
}*/
#endif
////the end of file//////////
////////the definition///////////
#include "Complex.h"
#include <math.h>
Complex::Complex(int nReal, int nImg)
{
real = nReal;
img = nImg;
}
void Complex::ShowInfo()
{
if (img > 0 )
{
cout<<"Number :"<<real<<"+"<<img<<"i"<<endl;
}
else
cout<<"Number :"<<real<<"+"<<"("<<img<<")"<<"i"<<endl;
}
Complex const Complex::operator +(Complex &x)
{
Complex tem;
tem.real = this->real + x.real;
tem.img = this->img + x.img;
return tem;
}
Complex const Complex::operator - (Complex &x)
{
Complex tem;
tem.real = this->real - x.real;
tem.img = this->img - x.img;
return tem;
}
Complex const Complex::operator ! ()
{
return Complex(-this->real,-this->img);
}
double const Complex::operator * (Complex &x)
{
double result=0;
if( (this->img>0)&&(x.img>0) || (this->img<0)&&(x.img<0) ) //same value
{
result = this->real*x.real - this->img*x.img;
return result;
}
else
result = this->real*x.real + this->img*x.img;
return result;
}
bool const Complex::operator == (const Complex &x)
{
if( (this->real == x.real) && (this->img == x.img))
return true ;
else
return false;
}
Complex const Complex::operator ++ (int)
{
Complex temp(*this);
real++;
img++;
return temp;
}
Complex & Complex::operator ++()
{
real++;
img++;
return *this;
}
Complex const Complex::operator --()
{
real--;
img--;
return *this;
}
Complex const Complex::operator --(int)
{
Complex temp(*this);
real--;
img--;
return temp;
}
bool const Complex::operator < (Complex &x)
{
if( ( pow(this->real,2) - pow(this->img,2) )
< ( pow(x.real,2) - pow(x.img,2) ) )
return true;
else
return false;
}
bool const Complex::operator > (Complex &x)
{
if( ( pow(this->real,2) - pow(this->img,2) )
> ( pow(x.real,2) - pow(x.img,2) ) )
return true;
else
return false;
}
///Test Function////////
#include "Complex.h"
void main()
{
Complex my(1,2),you(2,-5);
Complex result;
Complex test1(1,0),test2(2,0);
result = my + you;
my.ShowInfo();
you.ShowInfo();
result.ShowInfo();
cout<<my;
cout<<you;
cout<<!result;
cout<<((my == you)?"Right!/n":"Wrong!/n");
result = test1/my;
cout<<result;
cout<<my;
cout<<you;
my+=you;
cout<<my;
cin>>my;
my-=you;
cout<<my;
Complex *ptCplex = new Complex[100];
for ( int i = 0; i < 100; i++)
{
//ptCplex[i].real = i * 10;
//ptCplex[i].img = i * i ;
ptCplex[i].ShowInfo();
}
delete []ptCplex;
}
相关文章推荐
- 简单的问题,用类创建一个对象error C2380: type(s) preceding 'Complex' (constructor with return type, or illegal redefinition of current class-n
- 1553 simple complex class
- A simple C++ complex class
- The complex class
- 复数类:class Complex
- 类重定义错误 error C2011: “complex”:“class”类型重定义
- A class extends ArrayList, but the instance of the calss do not work in a complex case.
- servlet-class :cvc-complex-type.2.4.a
- class Complex
- A simple Complex class
- 面向对象复数类(class Complex) 的重载运算符 一元二元的实现与使用
- 复数类模板(Complex Class)
- Java静态内部类(static class)
- EJB3 Exception:Cannot instantiate class: org.jnp.interfaces.NamingContextFactory
- 测试解析class常量池
- Scala 的Value Class 和 Universal Traits
- tomcat无法启动 ClassNotFoundException org.springframework.web.context.ContextLoaderListener
- Java静态内部类(static class)
- c# 中的静态类(satic class)和具体类的区别及用法
- 遇到的问题--Retrieving the COM class factory for component with CLSID