Analytical mode - measured C ++ implementation - Reply CH0877

zhaozj2021-02-11  169

CH0877 translated a article named "Analysis Mode - Measurement" (http://www.9cbs.net/develop/Article/14/14449.shtm), very good. I tried to realize it with C .

The following is the source code:

#ifndef _quantity_h_ # define _Quantity_H_

#include

template class Quantity {public: template Weight (const ValueType & amount, const UnitType & unit): m_amount (amount), m_unit (new unit_wrap (unit)) {} ~ Quantity () {Delete M_Unit;} Quantity (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 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 (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 Bool Equal (const t & l, const t & r) {return (l == r);} template <> bool equal (const Double & l, const double & r) { static double precision = 0.000000001; return (l - r 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 class unit_wrap: public unit_base {public: unit_wrap (const UnitType & unit ): m_content (unit) {} public: virtual unit_base * 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 valuepepe & val) const {return m_content.from_standard (val);} private: unittype m_content;

Private: valueType m_amount; unit_base * m_unit;

Template Quantity Inline _CDECL Operator (Const Quantity & A, const QUANTITY & B) {Return (Quantity (a) = b);}

Template Quantity Inline _CDECL Operator- (Const Quantity & A, const quantity & b) {return (a) - = b);

#ENDIF / * _QUANTINTITY_H_ * /

The following is a sample code:

typedef Quantity 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);

转载请注明原文地址:https://www.9cbs.com/read-5859.html

New Post(0)