home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2000 February
/
PCWorld_2000-02_cd.bin
/
Software
/
TemaCD
/
tcvpa
/
data1.cab
/
MyFileGroup
/
INCLUDE
/
BCD.hpp
< prev
next >
Wrap
C/C++ Source or Header
|
1999-06-03
|
11KB
|
359 lines
#ifndef _INC_BCD_HPP
#define _INC_BCD_HPP
class TC_CBCD;
enum BcdRet {
bcd_Ok,
bcd_Fmt,
bcd_TooLarge,
};
#define BCDFMT_CRNCY 0x00000001L // %$ use currency format
#define BCDFMT_CRNCY_NAME 0x00000002L // %C currency name; +string
#define BCDFMT_CRNCY_POS 0x00000004L // %P currency name position; +number: 0 left, 1 right, 2 at decimal pos
#define BCDFMT_CRNCY_SPACES 0x00000008L // %S currency space separators; +number
#define BCDFMT_SIGN 0x00000010L // %+ include sign
#define BCDFMT_THOUSANDS 0x00000020L // %, separate thousands
#define BCDFMT_PARENTH_NEG 0x00000040L // %( negative in parenteses
#define BCDFMT_DEC_SEP 0x00000080L // %D decimal separator; +string
#define BCDFMT_RIGHT_JUST 0x00000100L // %J right justify
#define BCDFMT_FRACTZ_TRIM 0x00000200L // %f truncate fraction zeroes
#define BCDFMT_THOUSAND_SEP 0x00000400L // %T thousands separator; +string
#define BCDFMT_DEC_NUM 0x00000800L // %d number of digits after decimal point (ignored in case of BCD )
extern TC_COREEX_EXPORT TC_CBCD tcNullBCD ;
// **********************************************************************
class TC_COREEX_EXPORT TC_CBCD
{
#define ABS_VAL(v) (v < 0 ? -v : v)
#define SIZE_OF_BODY(P,S) ((ABS_VAL(S) >= P ? ABS_VAL(S) : P)/2)
#define FBCD_NULL 0
#define FBCD_VAL 1
#define FBCD_OVF 2
private:
enum {
op_Add,
op_Sub,
};
public:
friend inline TC_CBCD operator + (TC_CBCD &a, TC_CBCD &b) { return TC_CBCD(a).Add(b, 0); }
friend inline TC_CBCD operator - (TC_CBCD &a, TC_CBCD &b) { return TC_CBCD(a).Add(b, 1); }
friend inline TC_CBCD operator * (TC_CBCD &a, TC_CBCD &b) { return TC_CBCD(a).Mul(b); }
friend inline TC_CBCD operator / (TC_CBCD &a, TC_CBCD &b) { return TC_CBCD(a).Div(b); }
private: unsigned char m_P ;
private: signed char m_S ;
private: signed char m_Sign ;
private: char m_Flag ;
private: TC_CString m_Body ;
private: int m_Bytes ;
public: TC_CBCD (unsigned char P, signed char S)
{
Init(P,S);
} // end of func
// **********************************************************************
public: TC_CBCD (unsigned char P, signed char S, int val)
{
Init(P,S);
SetFromLong((long)val);
} // end of func
// **********************************************************************
public: TC_CBCD (unsigned char P, signed char S, long val)
{
Init(P,S);
SetFromLong(val);
} // end of func
// **********************************************************************
public: TC_CBCD (unsigned char P, signed char S, unsigned long val)
{
Init(P,S);
SetFromLong((long)val);
} // end of func
// **********************************************************************
public: TC_CBCD (unsigned char P, signed char S, double val)
{
Init(P,S);
SetFromDouble(val);
} // end of func
// **********************************************************************
public: TC_CBCD (unsigned char P, signed char S, const char * val)
{
Init(P,S);
SetValue(val);
} // end of func
// **********************************************************************
public: TC_CBCD (unsigned char P, signed char S,TC_CBCD &val)
{
Init(P,S);
Assign(val);
} // end of func
// **********************************************************************
public: TC_CBCD (TC_CBCD &bcd)
{
Set(bcd);
} // end of func
// **********************************************************************
public: TC_CBCD ()
{
Init(0,0);
} // end of func
// **********************************************************************
public: void Init (unsigned char P, signed char S) ;
public: void SetOverflow () ;
public: void SetNull () ;
public: int SetValue (const char* str, char * fmt = 0)
{
return SetFromString(str, fmt, FALSE);
} // end of func
// **********************************************************************
public: TC_CString Format (char * fmt = 0) ;
public: void Format (TC_CString &s, char * fmt = 0) ;
public: void Set (TC_CBCD & bcd) ;
public: void Assign (TC_CBCD & bcd) ;
public: int Compare (TC_CBCD & bcd)
{
return CompareTo(bcd);
} // end of func
// **********************************************************************
public: BOOL ToBuffer (unsigned char * dest, int dest_size) ;
public: BOOL AssignBuffer (unsigned char * src, int src_size) ;
public: BOOL SetFromBuffer (unsigned char * src) ;
public: int SizeForBuffer ()
{
return 4 + Bytes();
} // end of func
// **********************************************************************
public: static int SizeForBuffer (unsigned char P, signed char S) ;
public: BOOL IsNull () ;
public: BOOL IsOverflow () ;
public: BOOL IsSameType (TC_CBCD & bcd) ;
public: int Prec ()
{
return m_P;
} // end of func
// **********************************************************************
public: int Scale ()
{
return m_S;
} // end of func
// **********************************************************************
public: BOOL IsZero ( void ) ;
public: int Sign ()
{
return m_Sign;
} // end of func
// **********************************************************************
public: void Winout (char * t = 0) ;
public: int IsStringValid (const char* str, char * fmt = 0)
{
return SetFromString(str, fmt, TRUE);
} // end of func
// **********************************************************************
public: BOOL IsFmtValid (const char * fmt) ;
public: int Bytes () ;
public: int AsInt ()
{
return (int)AsLong();
} // end of func
// **********************************************************************
public: long AsLong () ;
public: double AsDouble () ;
private: TC_CBCD& Add (TC_CBCD& bcd, int op) ;
private: TC_CBCD & Mul (TC_CBCD & bcd) ;
private: TC_CBCD & Div (TC_CBCD & bcd) ;
private: TC_CBCD & Neg () ;
public: int operator > (TC_CBCD & bcd)
{
return CompareTo(bcd)==cmp_Greater;
} // end of func
// **********************************************************************
public: int operator < (TC_CBCD & bcd)
{
return CompareTo(bcd)==cmp_Less;
} // end of func
// **********************************************************************
public: int operator == (TC_CBCD & bcd)
{
return CompareTo(bcd)==cmp_Equal;
} // end of func
// **********************************************************************
public: int operator >= (TC_CBCD & bcd)
{
int cmp = CompareTo(bcd);
return (cmp==cmp_Greater || cmp==cmp_Equal);
} // end of func
// **********************************************************************
public: int operator <= (TC_CBCD & bcd)
{
int cmp = CompareTo(bcd);
return (cmp==cmp_Less || cmp==cmp_Equal);
} // end of func
// **********************************************************************
public: int operator != (TC_CBCD & bcd)
{
return CompareTo(bcd)!=cmp_Equal;
} // end of func
// **********************************************************************
public: TC_CBCD& operator += (TC_CBCD & bcd)
{
Add(bcd, op_Add);
return *this;
} // end of func
// **********************************************************************
public: TC_CBCD& operator -= (TC_CBCD & bcd)
{
Add(bcd, op_Sub);
return *this;
} // end of func
// **********************************************************************
public: TC_CBCD& operator *= (TC_CBCD & bcd)
{
Mul(bcd);
return *this;
} // end of func
// **********************************************************************
public: TC_CBCD& operator /= (TC_CBCD & bcd)
{
Div(bcd);
return *this;
} // end of func
// **********************************************************************
public: int operator ! ()
{
return IsZero();
} // end of func
// **********************************************************************
public: TC_CBCD operator - ()
{
return TC_CBCD(*this).Neg();
} // end of func
// **********************************************************************
public: TC_CBCD & operator ++ (int)
{
TC_CBCD bcd(m_P, m_S, 1);
Add(bcd,op_Add);
return *this;
} // end of func
// **********************************************************************
public: TC_CBCD & operator ++ ()
{
TC_CBCD bcd(m_P, m_S, 1);
Add(bcd,op_Add);
return *this;
} // end of func
// **********************************************************************
public: TC_CBCD & operator -- (int)
{
TC_CBCD bcd(m_P, m_S, 1);
Add(bcd,op_Sub);
return *this;
} // end of func
// **********************************************************************
public: TC_CBCD & operator -- ()
{
TC_CBCD bcd(m_P, m_S, 1);
Add(bcd,op_Sub);
return *this;
} // end of func
// **********************************************************************
public: TC_CBCD& operator = (TC_CBCD& val)
{
Assign(val);
return *this;
} // end of func
// **********************************************************************
public: TC_CBCD& operator = (int val)
{
SetFromLong((long)val);
return *this;
} // end of func
// **********************************************************************
public: TC_CBCD& operator = ( long val)
{
SetFromLong(val);
return *this;
} // end of func
// **********************************************************************
public: TC_CBCD& operator = ( unsigned long val)
{
SetFromLong(val);
return *this;
} // end of func
// **********************************************************************
public: TC_CBCD& operator = ( double val)
{
SetFromDouble(val);
return *this;
} // end of func
// **********************************************************************
public: TC_CBCD& operator = ( char * val)
{
SetValue(val);
return *this;
} // end of func
// **********************************************************************
private: void Reset () ;
private: unsigned char * Body () ;
private: void SetDigit (unsigned char digit, int pos) ;
private: unsigned char GetDigit (int pos) ;
private: int WholeDigits () ;
private: int WholeBegin () ;
private: int FractLen () ;
private: unsigned char GetCommonP (TC_CBCD & bcd1, TC_CBCD & bcd2) ;
private: signed char GetCommonS (TC_CBCD & bcd1, TC_CBCD & bcd2) ;
private: void Normalize (unsigned char P, signed char S) ;
private: int SignificantDigits (BOOL no_trail) ;
private: BOOL LShift () ;
private: BOOL RShift () ;
private: int CompareTo (TC_CBCD & bcd, BOOL is_abs = FALSE) ;
private: TC_CBCD & SetFromDouble (double val) ;
private: int SetFromString (const char* str, char * fmt, BOOL is_test ) ;
private: TC_CBCD & SetFromLong (long val) ;
}; // end of class TC_CBCD
// **********************************************************************
#endif // _INC_BCD_HPP