//%PRINT OFF #ifndef MONEY /* Inhibit repeated inclusion */ #include /* For fmod, modf, floor, log10 */ //%PRINT ON //%TITLE money Class (freeware version) (copyright 1994, Information Disciplines, Inc) // This special limited version of IDI's money class is packaged for // unrestricted distribution. Unlike the full version, it: // - is self sufficient, not depending on any macros, functions, or other // classes in IDI's library (but it uses standard C++ library routines). // - supports only a single (but customizable) internal representation. //%SPACE 2 // User organizations may use it freely in applications they develop for // their own internal use. Contact IDI at (312) 624-3670: // - for permission to incorporate all or part of this class or its // related functions into commercial software products, commercial // courses, or published material, or // - for information about the full version or other IDI classes. //%SPACE 3 #define MONEY const money /* Conventional keyword for constant declaration */ class money { //%SPACE 3 // Internal representation: Always an integer, scaled so that unity is // ----------------------- the smallest measurable quantity #ifndef moneyIType /* User may override with #define before */ #define moneyIType double/* #include, e.g. if a class is available */ #endif /* for multi-word integers or packed decimal */ /* (However, some constructors and accessors */ /* are limited to double float precision) */ public: static moneyIType scale; // Smallest fraction of monetary unit // represented e.g. 100 = cents, 1000 = mills protected: // Internal integer value = moneyIType value; // amount in monetary units * scale //%SPACE 3 // External representation: Constants used in output and input functions // ----------------------- (initialized in MONEYFW.CPP -- user can override) public: static char pfx_symbol[]; // Leading currency symbol (U.S.: '$') static char sfx_symbol[]; // Trailing currency symbol static char decimal_point; // Character for 100ths (U.S.: period) static char group_separator;// Character for 1000nds (U.S.: comma) static char unit_name[]; // Name of monetary unit (U.S.: "dollar") static char cent_name[]; // Name of fraction unit (U.S.: "cent") //%EJECT protected: static double round(const double x); public: //%SPACE 3 // Constructors: To support literal constants, we allow conversion from // ------------ float. This inhibits detection of some mixed expressions. money(const double x) : value(round(x * scale)) {} money() {} // Default constructor, for efficiency // The compiler will supply appropriate versions of: // - the destructor, // - the copy constructor, // - the assignment operator. //%SPACE 3 // Accessor functions: // ------------------ public: short moneyCents() const {double dummy; return short( modf((value + (value < 0 ? -.5 :.5)) / scale, &dummy) * 100 );} moneyIType moneyInt() const {double dummy; return modf(double(value) / scale, &dummy),dummy;} //%SPACE 3 // Arithmetic member operators: // --------------------------- // For efficiency we follow Scott Myers ("More Effective C++", Addison // Wesley) in defining the compound assignment operators as primitive, // and then defining most ordinary arithmetic operators in terms of them. money& operator+= (MONEY rs) {value += rs.value; return *this;} money& operator-= (MONEY rs) {value -= rs.value; return *this;} money& operator*= (const double rs) {value =round(value*rs); return *this;} money& operator/= (const double rs) {value =round(value/rs); return *this;} money operator- () const // Unary minus {money result; result.value = - value; return result;} money operator+ () const {return *this; } money operator+ (MONEY rs) const {return money(*this) += rs;} money operator- (MONEY rs) const {return money(*this) -= rs;} money operator* (const double rs) const {return money(*this) *= rs;} money operator/ (const double rs) const {return money(*this) /= rs;} double operator/ (MONEY rs) const {return value / rs.value;} //%EJECT // Relational member operators: // --------------------------- short operator== (MONEY rs) const {return value == rs.value;} short operator< (MONEY rs) const {return value < rs.value;} short operator<= (MONEY rs) const {return value <= rs.value;} short operator!= (MONEY rs) const {return value != rs.value;} short operator> (MONEY rs) const {return value > rs.value;} short operator>= (MONEY rs) const {return value >= rs.value;} // For efficiency the following redundant relational operators // avoid creating temporary money objects and invoking constructors: short operator== (const double rs) const {return value == rs * scale;} short operator< (const double rs) const {return value < rs * scale;} short operator<= (const double rs) const {return value <= rs * scale;} short operator!= (const double rs) const {return value != rs * scale;} short operator> (const double rs) const {return value > rs * scale;} short operator>= (const double rs) const {return value >= rs * scale;} }; // ******** End of class definition //%SPACE 3 // Additional (non-member) money operators // --------------------------------------- inline money operator* (const double ls, MONEY rs) {return rs * ls;} inline short operator== (const double ls, MONEY rs) {return rs == ls;} inline short operator< (const double ls, MONEY rs) {return rs > ls;} inline short operator<= (const double ls, MONEY rs) {return rs >= ls;} inline short operator!= (const double ls, MONEY rs) {return rs != ls;} inline short operator> (const double ls, MONEY rs) {return rs < ls;} inline short operator>= (const double ls, MONEY rs) {return rs <= ls;} istream& operator>> (istream& ls, money& rs); ostream& operator<< (ostream& ls, MONEY rs); // (in MONEYFW.CPP) //%PRINT OFF #endif //%PRINT ON