复数类模板(Complex Class)
2011-09-18 18:53
239 查看
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
#include <iomanip>
class Complex
{
public:
Complex(double _real,double _imag = 0.0):real(_real),imag(_imag){} //构造函数,初始化列表和默认参数
Complex(std::istream &is){is >> *this;}; //输入构造函数,调用自身的>>操作符
void SetReal(double _real); //更改实部的值
void SetImag(double _imag); //更改虚部的值
void SetVal(double _real,double _imag); //更改整个复数
inline double GetReal() const; //获取实部,常成员函数
inline double GetImag() const; //获取虚部,常成员函数
Complex& operator+=(const Complex &val); //成员操作符+=
Complex& operator*=(const Complex &val); //成员操作符-=
friend bool operator==(const Complex &lhs,const Complex &rhs); //友元函数,需访问私有数据
friend std::istream& operator>>(std::istream &,Complex &); //友元输入操作符,需私有数据
friend std::ostream& operator<<(std::ostream &,const Complex &); //友元输出操作符,需私有数据
private:
double real;
double imag;
};
Complex operator+(const Complex &lhs, const Complex &rhs); //普通函数,实现两个复数+操作
Complex operator*(const Complex &lhs, const Complex &rhs); //普通函数,实现两个复数*操作
//========================分割线,此线上面为定义代码,此线下面是实现代码===============================
inline bool operator==(const Complex &lhs,const Complex &rhs)
{
return (lhs.real == rhs.real) && (lhs.imag == rhs.imag);
}
inline bool operator!=(const Complex &lhs,const Complex &rhs)
{
return !(lhs==rhs);
}
inline Complex& Complex::operator+=(const Complex &val)
{
real += val.real;
imag += val.imag;
return *this;
}
inline Complex operator+(const Complex &lhs,const Complex &rhs)
{
Complex ret(lhs);
ret += rhs;
return ret;
}
inline Complex& Complex::operator*=(const Complex &val)
{
double tReal = real;
double tImag = imag;
real = tReal*val.real - tImag*val.imag;
imag = tReal*val.imag + tImag*val.real;
return *this;
}
inline Complex operator*(const Complex &lhs,const Complex &rhs)
{
Complex ret(lhs);
ret *= rhs;
return ret;
}
inline std::istream& operator>>(std::istream &is,Complex &com)
{
std::cout << "请输入实数部分:" ;
is >> com.real;
if(is)
{
std::cout << "请输入虚数部分:" ;
is >> com.imag;
if(is)
{
return is;
}
else
{
com = Complex(0.0);
}
}
else
{
com = Complex(0.0);
}
return is;
}
inline std::ostream& operator<<(std::ostream &os, const Complex &com)
{
os << "复数为:" << com.real << std::showpos << com.imag << "i" << std::endl;
return os;
}
inline double Complex::GetReal() const
{
return real;
}
inline double Complex::GetImag() const
{
return imag;
}
void Complex::SetReal(double _real)
{
real = _real;
}
void Complex::SetImag(double _imag)
{
imag = _imag;
}
void Complex::SetVal(double _real,double _imag)
{
real = _real;
imag = _imag;
}
#endif
测试代码为:
#include <stdio.h>
#include <tchar.h>
#include <cstdlib>
#include <iostream>
#include <sys/timeb.h>
#include <ctime>
#include <climits>
#include "Complex.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//测试构造函数
Complex c1(3,4);
cout << c1;
//测试流输入构造函数
Complex c2(cin);
cout << c2;
//测试+
Complex c3 = c1 + c2;
cout << c3;
//测试*
Complex c4 = c1*c2;
cout << c4;
//测试*=
Complex c5(3,4);
c5 *= c1;
cout << c5;
//测试==
if(c1==c2)
{
cout << "复数c1和c2相等" << endl;
}
else
{
cout << "复数c1和c2不等" << endl;
}
//测试成员函数SetImag
c1.SetImag(-100.89);
cout << "更改c1的虚部为-100.89:" << c1;
system("pause");
return 0;
}
转自:/article/5819716.html
虚怀若谷
#define COMPLEX_H
#include <iostream>
#include <iomanip>
class Complex
{
public:
Complex(double _real,double _imag = 0.0):real(_real),imag(_imag){} //构造函数,初始化列表和默认参数
Complex(std::istream &is){is >> *this;}; //输入构造函数,调用自身的>>操作符
void SetReal(double _real); //更改实部的值
void SetImag(double _imag); //更改虚部的值
void SetVal(double _real,double _imag); //更改整个复数
inline double GetReal() const; //获取实部,常成员函数
inline double GetImag() const; //获取虚部,常成员函数
Complex& operator+=(const Complex &val); //成员操作符+=
Complex& operator*=(const Complex &val); //成员操作符-=
friend bool operator==(const Complex &lhs,const Complex &rhs); //友元函数,需访问私有数据
friend std::istream& operator>>(std::istream &,Complex &); //友元输入操作符,需私有数据
friend std::ostream& operator<<(std::ostream &,const Complex &); //友元输出操作符,需私有数据
private:
double real;
double imag;
};
Complex operator+(const Complex &lhs, const Complex &rhs); //普通函数,实现两个复数+操作
Complex operator*(const Complex &lhs, const Complex &rhs); //普通函数,实现两个复数*操作
//========================分割线,此线上面为定义代码,此线下面是实现代码===============================
inline bool operator==(const Complex &lhs,const Complex &rhs)
{
return (lhs.real == rhs.real) && (lhs.imag == rhs.imag);
}
inline bool operator!=(const Complex &lhs,const Complex &rhs)
{
return !(lhs==rhs);
}
inline Complex& Complex::operator+=(const Complex &val)
{
real += val.real;
imag += val.imag;
return *this;
}
inline Complex operator+(const Complex &lhs,const Complex &rhs)
{
Complex ret(lhs);
ret += rhs;
return ret;
}
inline Complex& Complex::operator*=(const Complex &val)
{
double tReal = real;
double tImag = imag;
real = tReal*val.real - tImag*val.imag;
imag = tReal*val.imag + tImag*val.real;
return *this;
}
inline Complex operator*(const Complex &lhs,const Complex &rhs)
{
Complex ret(lhs);
ret *= rhs;
return ret;
}
inline std::istream& operator>>(std::istream &is,Complex &com)
{
std::cout << "请输入实数部分:" ;
is >> com.real;
if(is)
{
std::cout << "请输入虚数部分:" ;
is >> com.imag;
if(is)
{
return is;
}
else
{
com = Complex(0.0);
}
}
else
{
com = Complex(0.0);
}
return is;
}
inline std::ostream& operator<<(std::ostream &os, const Complex &com)
{
os << "复数为:" << com.real << std::showpos << com.imag << "i" << std::endl;
return os;
}
inline double Complex::GetReal() const
{
return real;
}
inline double Complex::GetImag() const
{
return imag;
}
void Complex::SetReal(double _real)
{
real = _real;
}
void Complex::SetImag(double _imag)
{
imag = _imag;
}
void Complex::SetVal(double _real,double _imag)
{
real = _real;
imag = _imag;
}
#endif
测试代码为:
#include <stdio.h>
#include <tchar.h>
#include <cstdlib>
#include <iostream>
#include <sys/timeb.h>
#include <ctime>
#include <climits>
#include "Complex.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//测试构造函数
Complex c1(3,4);
cout << c1;
//测试流输入构造函数
Complex c2(cin);
cout << c2;
//测试+
Complex c3 = c1 + c2;
cout << c3;
//测试*
Complex c4 = c1*c2;
cout << c4;
//测试*=
Complex c5(3,4);
c5 *= c1;
cout << c5;
//测试==
if(c1==c2)
{
cout << "复数c1和c2相等" << endl;
}
else
{
cout << "复数c1和c2不等" << endl;
}
//测试成员函数SetImag
c1.SetImag(-100.89);
cout << "更改c1的虚部为-100.89:" << c1;
system("pause");
return 0;
}
转自:/article/5819716.html
虚怀若谷
相关文章推荐
- class Complex
- 类重定义错误 error C2011: “complex”:“class”类型重定义
- servlet-class :cvc-complex-type.2.4.a
- 1553 simple complex class
- A simple Complex class
- 面向对象复数类(class Complex) 的重载运算符 一元二元的实现与使用
- 简单的问题,用类创建一个对象error C2380: type(s) preceding 'Complex' (constructor with return type, or illegal redefinition of current class-n
- A simple C++ complex class
- Class Complex
- A class extends ArrayList, but the instance of the calss do not work in a complex case.
- 复数类:class Complex
- The complex class
- Unable to compile class for JSP 问题解决方法
- JAVA 笔记 ClassLoader.getResourceAsStream() 与 Class.getResourceAsStream()的区别
- JVM加载class文件的原理机制
- myeclipse出现Could not find the main class原因分析
- 根据java编译器规则在Class中搜索匹配指定参数类型表的泛型方法(GenericMethod)
- 【java虚拟机系列】JVM类加载器与ClassNotFoundException和NoClassDefFoundError
- The content of element type "session-factory" must match "(property*,mapping*,(class-cache| c
- Qt 之 QSqlDriverCreatorBase Class