<< "a * b ="<<( a * b) p>
<< "nn ----------------------------------------- --------- nn " p>
; p>
) p>
) p>
programm2; p>
# define COUNT unsigned int p>
# define TRUE 1 p>
# define FALSE 0 p>
# define ILLEGAL 10 p>
enum p>
( p>
PLUS, p>
MINUS p>
); p>
extern class unlim p>
( p>
public: p>
unlim (char *); p>
unlim (); p>
unlim (unlim &); p>
~ unlim (); p>
unlim p>
& operator = (char *), p>
& operator = (unlim &); p>
friend int p>
operator == (unlim &, unlim &), p>
operator! = (unlim &, unlim &), p>
operator> (unlim &, unlim &), p>
operator> = (unlim &, unlim &), p>
operator <(unlim &, unlim &), p>
operator <= (unlim &, unlim &); p>
friend unlim p>
operator + (unlim &),// unary p>
operator - (unlim &),// unary p>
operator + (unlim &, unlim &),// binary p>
operator - (unlim &, unlim &),// binary p>
operator * (unlim &, unlim &), p>
abs (unlim &); p>
friend ostream p>
& operator <<(ostream &, unlim &); p>
private: p>
struct descriptor p>
( p>
char p>
* body; p>
COUNT p>
len, p>
HowMany; p>
); p>
descriptor p>
* pv;// pointer to value descriptor p>
char p>
sign, p>
digit (COUNT number); p>
char p>
& operator [] (COUNT i) (return pv-> body [i ];} p>
void p>
init0 (),// init by zero p>
NotDigit (),// message "no digit" & init0 p>
optimize (),// optimize length of body p>
error (char *);// display error message p>
); p>
inline int operator == (unlim & a, unlim & b) p>
( p>
return! (a! = b); p>
) p>
inline int operator <= (unlim & a, unlim & b) p>
( p>
return (a
) p>
inline int operator> = (unlim & a, unlim & b) p>
( p>
return! (a
) p>
inline int operator> (unlim & a, unlim & b) p>
( p>
return! (a
) p>
inline unlim operator + (unlim & x) p>
( p>
return x; p>
) p>
programm3; p>
# include p>
# include p>
# include p>
# include p>
# define COUNT unsigned int p>
# define TRUE 1 p>
# define FALSE 0 p>
# define ILLEGAL 10 p>
enum p>
( p>
PLUS, p>
MINUS p>
); p>
class unlim p>
( p>
public: p>
unlim (char *); p>
unlim (); p>
unlim (unlim &); p>
~ unlim (); p>
unlim p>
& operator = (char *), p>
& operator = (unlim &); p>
friend int p>
operator == (unlim &, unlim &), p>
operator! = (unlim &, unlim &), p>
operator> (unlim &, unlim &), p>
operator> = (unlim &, unlim &), p>
operator <(unlim &, unlim &), p>
operator <= (unlim &, unlim &); p>
friend unlim p>
operator + (unlim &),// unary p>
operator - (unlim &),// unary p>
operator + (unlim &, unlim &),// binary p>
operator - (unlim &, unlim &),// binary p>
operator * (unlim &, unlim &), p>
abs (unlim &); p>
friend ostream p>
& operator <<(ostream &, unlim &); p>
private: p>
struct descriptor p>
( p>
char p>
* body; p>
COUNT p>
len, p>
HowMany; p>
); p>
descriptor p>
* pv;// pointer to value descriptor p>
char p>
sign, p>
digit (COUNT number); p>
char & operator [] (COUNT i) (return pv-> body [i ];} p>
void p>
init0 (),// init by zero p>
NotDigit (),// message "no digit" & init0 p>
optimize (),// optimize length of body p>
error (char *);// display error message p>
); p>
inline void unlim:: error (char * message) p>
( p>
cout << "Unlim class error:" p>
<
<< "n"; p>
) p>
void unlim:: init0 () p>
( p>
(pv-> body) = new char; p>
* (pv-> body) = 0; p>
(pv-> len) = 1; p>
sign = PLUS; p>
) p>
char unlim:: digit (COUNT number) p>
( p>
if (number> = (pv-> len)) p>
return ILLEGAL; p>
char byte = (pv-> body) [number/2]; p>
if (number% 2 == 0) p>
return byte% 10; p>
else p>
return byte/10; p>
) p>
unlim:: unlim () p>
( p>
pv = new descriptor; p>
init0 (); p>
(pv-> HowMany) = 1; p>
) p>
unlim:: ~ unlim () p>
( p>
if (- (pv-> HowMany) == 0) p>
( p>
delete pv-> body; p>
delete pv; p>
) p>
) p>
char DecVal (char symbol) p>
( p>
if (isdigit (symbol)) p>
return symbol-'0 '; p>
return ILLEGAL; p>
) p>
unlim:: unlim (char * string) p>
( p>
pv = new descriptor; p>
(pv-> HowMany) = 1; p>
COUNT Length = strlen (string); p>
if (Length == 0) p>
( p>
error ( "Empty string assigned. Value = 0 "); p>
init0 (); p>
return; p>
) p>
else p>
( p>
COUNT LeftLimit = 0; p>
switch (string [0]) p>
( p>
case'-': p>
sign = MINUS; p>
LeftLimit = 1; p>
break; p>
case'+': p>
LeftLimit = 1; p>
default: p>
sign = PLUS; p>
) p>
if (Length-LeftLimit == 0) p>
( p>
error ( "Sign without value. Value = 0 "); p>
init0 (); p>
return; p>
) p>
else p>
( p>
while (string [LeftLimit] == '0 ') p>
LeftLimit ++; p>
if ((Length-= LeftLimit) == 0) p>
( p>
init0 (); p>
return; p>
) p>
COUNT DestLength = Length/2 + Length% 2; p>
(pv-> body) = new char [DestLength]; p>
for (COUNT si = Length + LeftLimit-1, ki = 0; ki
( p>
char a = DecVal (string [si ]); p>
if (a == ILLEGAL) p>
( p>
NotDigit (); p>
return; p>
) p>
(pv-> body) [ki] = a; p>
if (si! = LeftLimit) p>
( p>
char a = DecVal (string [si-1 ]); p>
if (a == ILLEGAL) p>
( p>
NotDigit (); p>
return; p>
) p>
(pv-> body) [ki] + = 10 * a; p>
) p>
) p>
(pv-> len) = Length; p>
) p>
) p>
) p>
void unlim:: NotDigit () p>
( p>
error ( "Not digit symbol in string. String ignored. Value = 0 "); p>
delete pv-> body; p>
init0 (); p>
) p>
unlim:: unlim (unlim & arg) p>
( p>
(arg.pv) -> HowMany ++; p>
pv = arg.pv; p>
sign = arg.sign; p>
) p>
unlim & unlim:: operator = (unlim & arg) p>
( p>
(arg.pv) -> HowMany ++; p>
if (- (pv-> HowMany) == 0) p>
( p>
delete pv-> body; p>
delete pv; p>
) p>
pv = arg.pv; p>
sign = arg.sign; p>
return * this; p>
) p>
unlim & unlim:: operator = (char * string) p>
( p>
return * this = unlim (string); p>
) p>
ostream & operator <<(ostream & s, unlim & x) p>
( p>
if (x.sign == MINUS) p>
s <<"-"; p>
for (COUNT i = ((x.pv) -> len); i> 0; i -) p>
s <
return s; p>
) p>
int operator! = (unlim & a, unlim & b) p>
( p>
if ((a.pv) -> len! = (b.pv) -> len) p>
return TRUE; p>
if (a.sign! = b.sign) p>
return TRUE; p>
COUNT length = ((a.pv) -> len)/2 + ((a.pv) -> len)% 2; p>
for (COUNT i = 0; i
if (a [i]! = b [i]) p>
return TRUE; p>
return FALSE; p>
) p>
int operator <(unlim & a, unlim & b) p>
( p>
if (a.sign! = b.sign) p>
return a.sign == MINUS; p>
if ((a.pv) -> len == (b.pv) -> len) p>
( p>
COUNT i = ((a.pv) -> len) -1; p>
while (a.digit (i) == b.digit (i)) p>
( p>
if (i == 0) p>
return FALSE; p>
i -; p>
) p>
char p>
aLess = a.digit (i)
SignPlus = a.sign == PLUS; p>
return (aLess & & SignPlus) | | (! aLess & &! SignPlus); p>
) p>
else p>
( p>
char p>
aShort = (a.pv) -> len <(b.pv) -> len, p>
SignPlus = a.sign == PLUS; p>
return (aShort & & SignPlus) | | (! aShort & &! SignPlus); p>
) p>
) p>
inline int operator == (unlim & a, unlim & b) p>
( p>
return! (a! = b); p>
) p>
inline int operator <= (unlim & a, unlim & b) p>
( p>
return (a
) p>
inline int operator> = (unlim & a, unlim & b) p>
( p>
return! (a
) p>
inline int operator> (unlim & a, unlim & b) p>
( p>
return! (a
) p>
inline unlim operator + (unlim & x) p>
( p>
return x; p>
) p>
unlim abs (unlim & x) p>
( p>
unlim r = x; p>
r.sign = PLUS; p>
return r; p>
) p>
unlim operator - (unlim & x) p>
( p>
if ((x.pv) -> len == 1 & & x [0] == 0) p>
( p>
unlim y; p>
return y; p>
) p>
unlim y = x; p>
if (x.sign == PLUS) p>
y.sign = MINUS; p>
else p>
y.sign = PLUS; p>
return y; p>
) p>
void unlim:: optimize () p>
( p>
COUNT i = pv-> len/2 + pv-> len% 2-1; p>
char optimized = FALSE; p>
while (pv-> body [i] == 0) p>
( p>
optimized = TRUE; p>
if (i - == 0) p>
( p>
init0 (); p>
return; p>
) p>
) p>
if (optimized) p>
( p>
char * NewBody = new char [+ + i]; p>
for (COUNT ni = 0; ni
NewBody [ni] = pv-> body [ni]; p>
delete pv-> body; p>
pv-> body = NewBody; p>
pv-> len = (i -) * 2; p>
) p>
if ((pv-> body [i]/10) == 0 & & (pv-> len% 2) == 0) p>
pv-> len -; p>
) p>
inline COUNT max (COUNT a, COUNT b) p>
( p>
return (a> b)? a: b; p>
) p>
unlim operator + (unlim & a, unlim & b) p>
( p>
unlim r; p>
delete (r.pv) -> body; p>
if (a.sign == b.sign) p>
( p>
r.sign = a.sign; p>
COUNT p>
rlen = max ((a.pv) -> len, (b.pv) -> len) +1, p>
alen = (a.pv) -> len/2 + (a.pv) -> len% 2, p>
blen = (b.pv) -> len/2 + (b.pv) -> len% 2; p>
(r.pv) -> len = rlen; p>
rlen = rlen/2 + rlen% 2; p>
(r.pv) -> body = new char [rlen]; p>
* (r.pv) -> body = 0; p>
for (COUNT i = 0; i
( p>
unsigned char sum = (i
r [i] + = sum% 100; p>
r [i +1] = sum/100; p>
) p>
if (r.digit ((r.pv) -> len-1) == 0) p>
(r.pv) -> len -; p>
) p>
else p>
( p>
unlim p>
aa = a, p>
bb = b; p>
if (abs (a)
( p>
aa = b; p>
bb = a; p>
) p>
r.sign = aa.sign; p>
COUNT p>
rlen = (aa.pv) -> len, p>
blen = (bb.pv) -> len/2 + (bb.pv) -> len% 2; p>
(r.pv) -> len = rlen; p>
rlen = rlen/2 + rlen% 2; p>
(r.pv) -> body = new char [rlen]; p>
* (r.pv) -> body = 0; p>
for (COUNT i = 0; i
( p>
char sub = aa [i] - (i
if (sub <0) p>
( p>
r [i +1] =- 1; p>
sub + = 100; p>
) p>
else p>
r [i +1] = 0; p>
r [i] = sub; p>
) p>
r.optimize (); p>
) p>
return r; p>
) p>
unlim operator - (unlim & a, unlim & b) p>
( p>
return a + (-b); p>
) p>
unlim operator * (unlim & a, unlim & b) p>
( p>
unlim r; p>
delete (r.pv) -> body; p>
if (a.sign == b.sign) p>
r.sign = PLUS; p>
else p>
r.sign = MINUS; p>
COUNT p>
rlen = (a.pv) -> len + (b.pv) -> len, p>
alen = (a.pv) -> len/2 + (a.pv) -> len% 2, p>
blen = (b.pv) -> len/2 + (b.pv) -> len% 2; p>
(r.pv) -> len = rlen; p>
rlen = rlen/2 + rlen% 2; p>
(r.pv) -> body = new char [rlen]; p>
COUNT i; p>
for (i = 0; i
r [i] = 0; p>
for (i = 0; i
( p>
unsigned int p>
next = 0, p>
mul; p>
for (COUNT j = 0; j
( p>
next + = r [i + j]; p>
mul = a [i] * b [j] + next; p>
r [i + j] = mul% 100; p>
next = mul/100; p>
) p>
r [i + blen] = next; p>
) p>
if (r.digit ((r.pv) -> len-1) == 0) p>
(r.pv) -> len -; p>
if (r.digit ((r.pv) -> len-1) == 0) p>
r.init0 (); p>
return r; p>
) p>