软件工程

本类阅读TOP10

·PHP4 + MYSQL + APACHE 在 WIN 系统下的安装、配置
·Linux 入门常用命令(1)
·Linux 入门常用命令(2)
·使用 DCPROMO/FORCEREMOVAL 命令强制将 Active Directory 域控制器降级
·DirectShow学习(八): CBaseRender类及相应Pin类的源代码分析
·基于ICE方式SIP信令穿透Symmetric NAT技术研究
·Windows 2003网络负载均衡的实现
·一网打尽Win十四种系统故障解决方法
·数百种 Windows 软件的免费替代品列表
·收藏---行百里半九十

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
分析模式-计量的C++实现——回复ch0877

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

ch0877翻译了一篇名叫《分析模式-计量》的文章(http://www.csdn.net/Develop/article/14/14449.shtm),很不错。我就试着用C++实现了一下。

以下是源代码:

#ifndef _QUANTITY_H_
#define _QUANTITY_H_

#include <crtdbg.h>

template<typename ValueType>
class Quantity
{
public:
 template<typename UnitType>Weight(const ValueType & amount, const UnitType & unit)
  : m_amount(amount), m_unit(new unit_wrap<UnitType, ValueType>(unit))
 {
 }
 ~Quantity()
 {
  delete m_unit;
 }
 Quantity(const Quantity & other)
  : m_amount(other.m_amount), m_unit(other.m_unit ? other.m_unit->clone() : 0)
 {
 }
 Quantity & operator=(const Quantity & rhs)
 {
  if(&rhs == this)
   return *this;
  m_amount = rhs.m_amount;
  delete m_unit;
  m_unit = rhs.m_unit ? rhs.m_unit->clone() : 0;
  return *this;
 }
 template<typename UnitType> void convert(const UnitType & new_unit)
 {
  _ASSERT(m_unit);
  m_amount = m_unit->to_standard(m_amount);
  delete m_unit;
  m_unit = new unit_wrap<UnitType, double>(new_unit);
  m_amount = m_unit->from_standard(m_amount);
 }
 Quantity & operator+=(const Quantity & rhs)
 {
  _ASSERT(m_unit);
  _ASSERT(rhs.m_unit);
  m_amount = m_unit->from_standard(
   m_unit->to_standard(m_amount) + rhs.m_unit->to_standard(rhs.m_amount) );
  return *this;
 }
 Quantity & operator-=(const Quantity & rhs)
 {
  _ASSERT(m_unit);
  _ASSERT(rhs.m_unit);
  m_amount = m_unit->from_standard(
   m_unit->to_standard(m_amount) - rhs.m_unit->to_standard(rhs.m_amount) );
  return *this;
 }
 bool operator==(const Quantity & rhs)
 {
  _ASSERT(m_unit);
  _ASSERT(rhs.m_unit);
  return equal(m_unit->to_standard(m_amount) , rhs.m_unit->to_standard(rhs.m_amount) );
 }
 bool operator>(const Quantity & rhs)
 {
  _ASSERT(m_unit);
  _ASSERT(rhs.m_unit);
  return (m_unit->to_standard(m_amount) > rhs.m_unit->to_standard(rhs.m_amount) );
 }

private:
 template<typename T>bool equal(const T & l, const T & r)
 {return (l == r);}
 template<>bool equal<double>(const double & l, const double & r)
 {
  static double precision = 0.000000001;
  return (l - r < precision);
 }

private:
 template<typename ValueType>
 class unit_base
 {
 public:
  virtual ~unit_base()
  {
  }
 public:
  virtual unit_base * clone() const = 0;
  virtual ValueType to_standard(const ValueType & val) const = 0;
  virtual ValueType from_standard(const ValueType & val) const = 0;
 };
 template<typename UnitType, typename ValueType>
 class unit_wrap : public unit_base<ValueType>
 {
 public:
  unit_wrap(const UnitType & unit) : m_content(unit)
  {
  }
 public:
  virtual unit_base<ValueType> * clone() const
  {
   return new unit_wrap(m_content);
  }
  virtual ValueType to_standard(const ValueType & val) const
  {
   return m_content.to_standard(val);
  }
  virtual ValueType from_standard(const ValueType & val) const
  {
   return m_content.from_standard(val);
  }
 private:
  UnitType m_content;
 };

private:
 ValueType    m_amount;
 unit_base<ValueType> * m_unit;
};

template<typename ValueType>
Quantity<ValueType> inline _cdecl operator+(const Quantity<ValueType> & a,
           const Quantity<ValueType> & b)
{return (Quantity<ValueType>(a)+=b);}

template<typename ValueType>
Quantity<ValueType> inline _cdecl operator-(const Quantity<ValueType> & a, const Quantity<ValueType> & b)
{return (Quantity<ValueType>(a)-=b);}


#endif /* _QUANTITY_H_ */

以下是示例代码:

typedef Quantity<double> Weight;
class UNIT_KG
{
public:
 double to_standard(double val) const
 {return val;}
 double from_standard(double val) const
 {return val;}
};

class UNIT_G
{
public:
 double to_standard(double val) const
 {return val/1000.0;}
 double from_standard(double val) const
 {return val*1000.0;}
};

UNIT_KG _kg;
UNIT_G _g;

 Weight w1(1.0, _kg);
 Weight w2(1.0, _g);
 w1.convert(_g);
 w1.convert(_kg);
 w2 = w1;
 Weight w10(1.0, _kg);
 Weight w20(500.0, _g);
 w10 +=  w20;
 Weight w30(w10 + w20);
 bool b = (w10 == w20);
 b = (w30 > w10);




相关文章

相关软件