home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Mother of All Windows Books
/
CD-MOM.iso
/
cd_mom
/
utility
/
apps
/
wincheck
/
dde_sdk.zi$
/
MATH.C
< prev
next >
Wrap
Text File
|
1991-10-17
|
12KB
|
495 lines
// MATH.C -- Here is where the nitty gritty math part comes into play
#include <windows.h>
#include <stdlib.h>
#include "payroll.h"
#include "basedefs.h"
extern int CentsPerDollar;
extern HWND ghWnd;
extern char cComma, cCurrency, cDecimal;
extern char szCaptionName[];
void Add ( PMONEY Sum,
MONEY Adder1,
MONEY Adder2 );
void Subtract ( PMONEY Difference,
MONEY Subber1,
MONEY Subber2 );
BOOL bFigureDollarAmount ( LPSTR szIDD_AMOUNT, PMONEY pfAmount );
int ItoA ( char *szString );
int CentsItoA ( char *szString );
LONG LtoA ( char *szString );
void DollarsCents ( MONEY Cash, char *szBuckage, BYTE Flags );
//********************************************************************
void Add ( PMONEY Sum,
MONEY Adder1,
MONEY Adder2 )
{
BOOL Negative;
Negative = FALSE;
// Special case 1: Adder 1 is negative, Adder 2 is positive
if (
(( Adder1.Dollars < 0 ) || ( Adder1.Cents < 0 ))
&&
(( Adder2.Dollars >= 0 ) && (Adder2.Cents >= 0))
)
{
if (Adder1.Cents < 0) // Tiny case, negative cents
{
if (Adder2.Dollars > 0) // This will bring us to positive for sure
{
Adder2.Cents += CentsPerDollar;
(Adder2.Dollars)--;
}
}
else
{
Adder1.Dollars = 0L - (Adder1.Dollars);
Subtract ( Sum, Adder2, Adder1 );
return;
}
}
// Special case 2: Adder 2 is negative, Adder 1 is positive
if (
(( Adder2.Dollars < 0 ) || (Adder2.Cents < 0))
&&
(( Adder1.Dollars >= 0 ) && ( Adder1.Cents >= 0 ))
)
{
if (Adder2.Cents < 0) // Tiny case, negative cents
{
if (Adder1.Dollars > 0) // This will bring us to positive for sure
{
Adder1.Cents += CentsPerDollar;
(Adder1.Dollars)--;
}
}
else
{
Adder2.Dollars = 0L - (Adder2.Dollars);
Subtract ( Sum, Adder1, Adder2 );
return;
}
}
// Special case 3: Adder 1 is negative, Adder 2 is negative
if (
(( Adder2.Dollars < 0 ) || (Adder2.Cents < 0))
&&
(( Adder1.Dollars < 0 ) || (Adder1.Cents < 0))
)
{
Negative = TRUE;
Adder1.Dollars = 0L - Adder1.Dollars;
Adder2.Dollars = 0L - Adder2.Dollars;
if (Adder1.Cents < 0) Adder1.Cents = 0 - Adder1.Cents;
if (Adder2.Cents < 0) Adder2.Cents = 0 - Adder2.Cents;
}
if ( (Sum->Cents = (Adder1.Cents + Adder2.Cents)) >= CentsPerDollar )
{
(Sum->Cents) -= CentsPerDollar;
if (Adder1.Dollars >= 0)
(Adder1.Dollars)++; // Add the carry arbitralily to one of the adders
else
(Adder1.Dollars)--; // Add the carry arbitralily to one of the adders
}
Sum->Dollars = Adder1.Dollars + Adder2.Dollars;
if (Negative)
{
Sum->Dollars = 0L - Sum->Dollars;
if (!(Sum->Dollars))
Sum->Cents = 0 - Sum->Cents;
}
}
//********************************************************************
void Sub ( PMONEY Difference,
MONEY Subber1,
MONEY Subber2 )
{
// Special Case: Subtract a negative number from a number
if (
(Subber2.Dollars < 0L) ||
(Subber2.Cents < 0)
)
{
if (Subber2.Dollars)
Subber2.Dollars = 0L - Subber2.Dollars;
else
Subber2.Cents = 0 - Subber2.Cents;
Add ( Difference, Subber1, Subber2 );
return;
}
if (Subber1.Cents < Subber2.Cents) // Borrow
{
(Subber1.Cents) += CentsPerDollar;
(Subber1.Dollars)--;
}
Difference->Cents = Subber1.Cents - Subber2.Cents;
Difference->Dollars = Subber1.Dollars - Subber2.Dollars;
}
//********************************************************************
void Subtract ( PMONEY Difference,
MONEY Subber1,
MONEY Subber2 )
{
BOOL Negative;
// Special case #1: subtract 0
if ((!(Subber2.Dollars)) && (!(Subber2.Cents)))
{
Difference->Dollars = Subber1.Dollars;
Difference->Cents = Subber1.Cents;
return;
}
// Special case #2: subtract positive from a negative: Dollars
if (Subber1.Dollars < 0)
{
Subber1.Dollars = 0L - Subber1.Dollars;
Add ( Difference, Subber1, Subber2 );
Difference->Dollars = 0L - Difference->Dollars;
return;
}
if (Subber1.Cents < 0) // this is only for tiny negative numbers
{
Subber1.Cents = 0 - Subber1.Cents;
Add ( Difference, Subber1, Subber2 );
Difference->Dollars = 0L - Difference->Dollars;
return;
}
// If we get here, then Subber1 MUST be a postive number
Negative = FALSE;
if (Subber1.Dollars < Subber2.Dollars ) Negative = TRUE;
if (Subber1.Dollars == Subber2.Dollars )
if (Subber1.Cents < Subber2.Cents ) Negative = TRUE;
if (!Negative)
Sub (Difference, Subber1, Subber2);
else
{
Sub (Difference, Subber2, Subber1);
if (Difference->Dollars)
Difference->Dollars = 0L - Difference->Dollars;
else
Difference->Cents = 0 - Difference->Cents;
}
}
//*************************************************************************
//
// Parses a string into dollars and cents
//
//*************************************************************************
BOOL bFigureDollarAmount ( LPSTR szIDD_AMOUNT, PMONEY pfAmount )
{
int i, j, Cents;
HWND hWnd;
char szStrippedText[64];
BOOL BadNumber;
char *pToken;
LONG Dollars;
j = 0;
BadNumber = FALSE;
for ( i = 0; i < lstrlen ( (LPSTR)szIDD_AMOUNT ); i++ )
switch ( szIDD_AMOUNT[i] )
{
case '-': hWnd = GetFocus();
MessageBox ( ghWnd, "Negative Sign Unneccessary", szCaptionName, MB_OK | MB_ICONHAND );
SetFocus ( hWnd );
break;
case ' ': break; // ignore spaces
case 'o':
case 'O': szStrippedText[j] = '0'; j++; BadNumber = TRUE; break;
case 'i':
case 'I':
case 'l':
case 'L': szStrippedText[j] = '1'; j++; BadNumber = TRUE; break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': szStrippedText[j] = szIDD_AMOUNT[i]; j++; break;
default : if (
(cComma == szIDD_AMOUNT[i]) || // ignore commas
(cCurrency == szIDD_AMOUNT[i]) // ignore currency
)
{
// do nothing
}
else
{
if (cDecimal == szIDD_AMOUNT[i])
{
szStrippedText[j] = 0;
j++;
}
else
{
BadNumber = TRUE;
}
}
break;
}
szStrippedText[j] = 0;
j++;
szStrippedText[j] = 26; // Mark and EOF
pToken = szStrippedText;
if (*pToken != 26)
{
Dollars = LtoA ( pToken );
while (*pToken) pToken++;
pToken++;
}
else
Dollars = 0;
if (*pToken != 26)
{
Cents = CentsItoA ( pToken );
while (*pToken) pToken++;
pToken++;
}
else
Cents = 0;
if (Cents >= CentsPerDollar)
{
while ((LONG)Cents > (10L*(LONG)CentsPerDollar)) Cents /= 10;
Cents += 5; // round the number
Cents /= 10;
BadNumber = TRUE;
}
pfAmount->Dollars = Dollars;
pfAmount->Cents = Cents;
return !BadNumber;
}
//*************************************************************************
//
// int ItoA (szString)
//
//*************************************************************************
int ItoA ( char *szString )
{
int Len = lstrlen ( (LPSTR)szString );
int Value, Total, i;
Value = 1;
Total = 0;
Len--;
for ( i = 0; i < Len; i++ ) Value *= 10;
while (*szString)
{
Total += Value * (*szString - '0');
Value /= 10;
szString++;
}
return Total;
}
//*************************************************************************
//
// int ItoA (szString)
//
//*************************************************************************
int CentsItoA ( char *szString )
{
int Len = lstrlen ( (LPSTR)szString );
int OldLen = Len;
int Value, Total, i;
Value = 1;
Total = 0;
Len--;
for ( i = 0; i < Len; i++ ) Value *= 10;
while (*szString)
{
Total += Value * (*szString - '0');
Value /= 10;
szString++;
}
if (1 == OldLen) Total *= 10;
return Total;
}
//*************************************************************************
//
// int LtoA (szString) -- Uses no LONG multiplication/division
//
//*************************************************************************
LONG LtoA ( char *szString )
{
int Len = lstrlen ( (LPSTR)szString );
LONG Values[10] = { 1L,
10L,
100L,
1000L,
10000L,
100000L,
1000000L,
10000000L,
100000000L,
1000000000L };
LONG Total;
int j;
Total = 0;
Len--;
while (*szString)
{
for ( j = 0; j < (*szString - '0'); j++ ) Total += Values[Len];
Len--;
szString++;
}
return Total;
}
//*************************************************************************
//
//
//
//*************************************************************************
void DollarsCents ( MONEY Cash, char *szBuckage, BYTE Flags )
{
char szDollars[21];
BOOL bNegative;
int NumDigits;
int i, j;
LONG Dollars= Cash.Dollars;
int Cents = Cash.Cents;
BOOL bCurrency = (BOOL)(Flags & CURRENCY);
BOOL bCommas = (BOOL)(Flags & COMMAS);
bNegative = FALSE;
if (Dollars < 0L) // Negative money
{
bNegative = TRUE;
Dollars = 0 - Dollars;
}
if (Cents < 0) // Nagative money, Vol II
{
bNegative = TRUE;
Cents = -1 * Cents;
}
wsprintf ( szDollars, "%ld", Dollars );
NumDigits = lstrlen ( szDollars );
j = 0;
if (bNegative)
{
szBuckage[j] = '-';
j++;
}
if (bCurrency)
{
szBuckage[j] = cCurrency;
j++;
}
for ( i = 0; i < NumDigits; i++ )
{
szBuckage[j] = szDollars[i];
j++;
if (
(!((NumDigits-i-1) % 3))
&&
(i != (NumDigits-1))
&&
(bCommas)
)
{
if (!cComma) cComma = ',';
szBuckage[j] = cComma;
j++;
}
}
if (CentsPerDollar > 1)
{
szBuckage[j] = cDecimal;
j++;
}
switch (CentsPerDollar)
{
case 10000: szBuckage[j] = (char)((Cents/1000)+'0');
j++;
Cents %= 1000;
case 1000: szBuckage[j] = (char)((Cents/100)+'0');
j++;
Cents %= 100;
case 100: szBuckage[j] = (char)((Cents/10)+'0');
j++;
Cents %= 10;
case 10: szBuckage[j] = (char)(Cents+'0');
j++;
}
szBuckage[j] = 0;
}