Floating Point for Integer Operation of large number operation will be supplemented later

Source: Internet
Author: User

/*
* Date: 2006-08-14
* Ver: 0.1
*/

# Ifndef _ ln_h
# DEFINE _ ln_h
# Include <memory. h>
# Define string_length 100

Typedef Enum
{
None_type = 0,
Hex_type = 1,
Bcd_type = 2
} Data_string_type;

Class ln
{
Public:
Ln (): DT (none_type), sign (0) {memset (DS, 0, string_length );}
~ Ln (){}
Ln (const ln &);
Ln (const INT );
Ln & operator = (const ln &);
Ln & operator = (const INT );
Ln & operator = (const char *);
Ln & operator/(const ln &);
Ln & operator + = (LN );
Ln & operator * = (const ln );
Ln & operator-= (LN );
Ln & operator/= (LN );
Ln & operator <= (unsigned int N );
Ln & operator >>= (unsigned int N );

Friend ln operator + (LN, LN );
Friend bool operator <(LN, LN );
Friend bool operator = (LN, LN );
Friend bool operator> (LN, LN );
Friend bool operator> = (LN, LN );
Operator int ();

PRIVATE:
Ln & operator * = (char Mul );
Protected:
Void strs2hexs (char *, INT); // convert a string to a numeric format
Void hexs2strs (char *, INT); // convert a number to a string format
Void hexscalco (char *, Int &); // calculates the bitwise after addition overflow.
Void bcdscalco (char *, Int &); // calculates the bitwise after addition overflow.
Char hexscalcc (char *, Int &); // The bitwise operation after the subtraction overflow of a number returns the symbol
Char bcdscalcc (char *, Int &); // The bitwise operation after the subtraction overflow of a number returns the symbol
Void killhead0 (LN &, unsigned INT); // deletes the front 0
Void killtail0 (LN &, unsigned INT); // remove the suffix 0
Void herohead0 (LN &, INT); // Add the front 0
Void herotail0 (LN &, unsigned INT); // remove the suffix 0

Void hexs2bcds (char *, unsigned INT );
Char hex2bcd (char HEX) {return (hex/10) <4) | (hex % 10 );}
PRIVATE:
Char DS [string_length];
Data_string_type DT;
Char sign;
Char FN; // number of decimal places
};
# Endif

# Include "ln. H"
# Include "string. H"
# Include "stdlib. H"

LN: ln (const ln & that)
{
* This = that;
}

LN: ln (const int N)
{
* This = N;
}
Ln & ln: Operator = (const ln & that)
{
Dt = That. DT;
Sign = That. sign;
Memcpy (DS, that. DS, string_length );
Return * this;
}

Ln & ln: Operator = (const int N)
{
Int M = N;
If (M <0)
{
Sign = 1;
M =-m;
}
_ ITOA (M, DS, 10 );
Return * this;
}
Ln & ln: Operator = (const char * Str)
{
Sign = 0;
Memcpy (DS, STR, string_length );
Return * this;
}
Ln & ln: Operator/(const ln & that)
{
Return * this;
}
Ln & ln: Operator + = (ln that)
{
Ln Tn = that;
Int large = 0, diff = 0;
Char * PLG = 0, * PLT = 0;
 
If (sign ^ that. Sign)
{
If (sign)
{
Sign = That. sign;
That-= * this;
* This = that;
Return * this;
}
That. Sign = sign;
* This-= that;
Return * this;
}

Large = strlen (PLG = const_cast <char *> (DS ));
Diff = strlen (Plt = const_cast <char *> (TN. DS ));

Strs2hexs (DS, large );
Strs2hexs (TN. DS, diff );

If (large-diff <0)
{
Large ^ = diff;
Diff ^ = large;
Large ^ = diff;

PLG = const_cast <char *> (TN. DS );
PLT = Ds;
}
Diff = Large-diff;
For (INT I = Large-1; I> = diff; I --)
{
PLG [I] + = PLT [I-diff];
}
Hexs2bcds (PLG, large );
Bcdscalco (PLG, large );
If (Ds! = PLG)
* This = tn;
Hexs2strs (DS, large );
Return * this;
}

 

Ln & ln: Operator * = (ln that)
{
Ln TN1, TN2;
Int thislen = strlen (DS), thatlen = strlen (that. DS );
Int I = 0;
Char Ts = 0;
TS = Sign ^ that. sign;

Sign = 0;
That. Sign = 0;
 
TN1 = * this;
Strs2hexs (const_cast <char *> (that. DS), thatlen );
For (I = thatlen-1; I> = 0; I --)
{
TN1 * = That. DS [I];
TN2 + = TN1;
DS [thislen] = '0 ';
DS [thislen + 1] = 0;
Thislen ++;
TN1 = * this;
}
* This = TN2;
Sign = ts;
Return * this;
}

Ln & ln: Operator-= (ln that)
{
Char S = 0;
Int large = 0, diff = 0, I = 0;
 
If (sign ^ that. Sign)
{
That. Sign ^ = sign;
* This + = that;
Return * this;
}

If (* THIS <that)
S = 1;
Sign = 0;
That. Sign = 0;
If (* THIS <that)
{
That-= * this;
* This = that;
Sign = s;
Return * this;
}

Large = strlen (DS );
Diff = Large-strlen (that. DS );

Herohead0 (that, diff );
Strs2hexs (DS, strlen (DS ));
Strs2hexs (that. DS, strlen (that. DS ));

For (I = Large-1; I> = 0; I --)
DS [I]-= That. DS [I];

Bcdscalcc (DS, large );
Hexs2strs (DS, large );
Sign = s;
Return * this;
}
Ln & ln: Operator/= (ln that)
{
Char Ts = 0;
Unsigned int I = 0, Count = 0;
Ln Tn = 0;

TS = Sign ^ that. sign;
Sign = 0;
That. Sign = 0;
 
If (* THIS <that)
{
* This = 0;
Return * this;
}
Count = strlen (DS)-strlen (that. DS );
Herotail0 (that, count );
 
For (I = 0; I <count + 1; I ++)
{
While (* This> = That)
{
TN + = 1;
* This-= that;
Killhead0 (* This, strlen (DS ));
}
TN <= 1;
Killtail0 (that, 1 );
}
TN> = 1;
* This = tn;
Sign = ts;
Return * this;
}

Ln & ln: Operator <= (unsigned int N)
{
Herotail0 (* This, N );
Return * this;
}
Ln & ln: Operator >>= (unsigned int N)
{

Int LL = strlen (DS), M = N;
If (LL <= N)
{
* This = 0;
Return * this;
}
While (N)
DS [ll-n --] = '0 ';
Killtail0 (* This, M );
Return * this;
}

Ln operator + (ln rgh, ln lgh)
{
RGH + = lgh;
Return RGH;
}
Bool operator <(ln rgh, ln lgh)
{
Int RL = 0, LL = 0;

If (RGH. Sign ^ lgh. Sign)
Return RGH. sign;
RGH. killhead0 (RGH, strlen (RGH. DS ));
RGH. killhead0 (lgh, strlen (lgh. DS ));

If (RL = strlen (const_cast <char *> (RGH. DS) = (LL = strlen (const_cast <char *> (lgh. DS ))))
{
If (strcmp (RGH. DS, lgh. DS) = 0)
Return false;
}
If (! RGH. Sign & RL> LL)
Return false;
If (RL = LL)
{
If (! RGH. Sign & strcmp (RGH. DS, lgh. DS)> 0)
Return false;
}

Return true;
}
Bool operator = (ln rgh, ln lgh)
{
RGH. killhead0 (RGH, strlen (RGH. DS ));
RGH. killhead0 (lgh, strlen (lgh. DS ));
If (! (RGH. Sign ^ lgh. Sign) & strcmp (RGH. DS, lgh. DS) = 0)
Return true;
Return false;
}
Bool operator> (ln rgh, ln lgh)
{
If (RGH <lgh | RGH = lgh)
Return false;
Return true;
}

Bool operator> = (ln rgh, ln lgh)
{
Return! (RGH <lgh );
}

LN: Operator int ()
{
Int LL = 0, TR = 0;

LL = strlen (DS );
Strs2hexs (DS, LL );
For (INT I = 0; I <ll; I ++)
{
Tr * = 10;
Tr + = DS [I];
}
If (sign)
Tr =-TR;
Return TR;
}

 

 

Ln & ln: Operator * = (char Mul)
{
Int thislen = strlen (DS );

Strs2hexs (DS, thislen );
For (INT I = thislen-1; I> = 0; I --)
DS [I] * = Mul;
Hexs2bcds (DS, thislen );
Bcdscalco (DS, thislen );
Hexs2strs (DS, thislen );
Return * this;

}
Void ln: strs2hexs (char * pstr, int Len)
{
Int I = 0;
For (I = 0; I <Len; I ++)
{
If (pstr [I] <= '9' & pstr [I]> = '0 ')
Pstr [I]-= '0 ';
Else if (pstr [I]> = 'A' & pstr [I] <= 'Z ')
Pstr [I]-= 'a'-10;
Else if (pstr [I]> = 'A' & pstr [I] <= 'Z ')
Pstr [I]-= 'a'-10;
Else
{
Pstr [I] = 0;
Break;
}
}
}

Void ln: hexs2strs (char * Phex, int Len)
{
Int I = 0;
For (I = 0; I <Len; I ++)
{
If (Phex [I] <= 9 & Phex [I]> = 0)
Phex [I] + = '0 ';
Else
Phex [I] + = 'a'-10;
}
}

Void ln: hexscalco (char * Phex, Int & Len)
{
Int I = 0;
For (I = len-1; I> 0; I --)
{
Phex [I-1] + = (Phex [I]> 4) & 0x0f;
Phex [I] & = 0x0f;
}
If (Phex [I] & 0xf0)
{
Phex [Len + 1] = 0;
For (I = len-1; I> 0; I --)
Phex [I + 1] = Phex [I];
Phex [I + 1] = Phex [I] & 0x0f;
Phex [I]> = 4;
Len ++;
}
}
Void ln: bcdscalco (char * Phex, Int & Len)
{
Int I = 0;
Char temp = 0;
For (I = len-1; I> 0; I --)
{
Phex [I-1] = (Phex [I-1] & 0xf0) + hex2bcd (Phex [I]> 4) & 0x0f) + (Phex [I-1] & 0x0f ));
Phex [I] & = 0x0f;
}
If (Phex [I] & 0xf0)
{
Phex [Len + 1] = 0;
For (I = len-1; I> 0; I --)
Phex [I + 1] = Phex [I];
Phex [I + 1] = Phex [I] & 0x0f;
Phex [I]> = 4;
Len ++;
}
}
Char ln: hexscalcc (char * Phex, Int & Len)
{
Int I = 0;
For (I = len-1; I> 0; I --)
{
If (Phex [I] & 0xf0)
{
Phex [I-1] --;
}
Phex [I] & = 0x0f;
}
If (Phex [I] & 0xf0)
{
Phex [I] & = 0x0f;
Return 1;
}
Return 0;
}

Char ln: bcdscalcc (char * Phex, Int & Len)
{
Int I = 0;
For (I = len-1; I> 0; I --)
{
If (Phex [I] & 0xf0)
{
Phex [I-1] --;
}
Phex [I] + = 10;
Phex [I] % = 10;
}
If (Phex [I] & 0xf0)
{
Phex [I] + = 10;
Phex [I] % = 10;
Return 1;
}
Return 0;
}
Void ln: killhead0 (LN & that, unsigned int N)
{
Char * PPS = That. Ds;
Unsigned int I = 0;
For (I = 0; I <strlen (that. DS) & N; I ++, n --)
{
If (that. DS [I] = '0 ')
PPS ++;
Else
Break;
}
If (PPS = That. DS)
Return;
For (I = 0; I <strlen (that. DS) + pds-that.ds; I ++)
That. DS [I] = PDS [I];
That. DS [I] = 0;
}
Void ln: killtail0 (LN & that, unsigned int N)
{
Int I = strlen (that. DS );
While (n --)
{
If (that. DS [I-1] = '0 ')
That. DS [I-1] = 0;
Else
Break;
}
}
Void ln: herohead0 (LN & that, int N)
{
Char * PPS = That. Ds;
Int I = 0;
For (I = strlen (that. DS); I> = 0; I --)
PDS [I + N] = That. DS [I];
For (I = 0; I <n; I ++)
PPS [I] = '0 ';
}

Void ln: herotail0 (LN & that, unsigned int N)
{
Unsigned int LL = strlen (that. DS), I = 0;
For (I = 0; I <n; I ++)
That. DS [LL + I] = '0 ';
That. DS [LL + I] = 0;
}

Void ln: hexs2bcds (char * SRC, unsigned int N)
{
While (n --)
{
SRC [N] = hex2bcd (SRC [N]);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.