←ïðåäûäóùàÿ ñëåäóþùàÿ→
1 2 3
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
#define COUNT unsigned int
#define TRUE 1
#define FALSE 0
#define ILLEGAL 10
enum
{
PLUS,
MINUS
};
class unlim
{
public:
unlim(char*);
unlim();
unlim(unlim&);
~unlim();
unlim
&operator = (char*),
&operator = (unlim&);
friend int
operator == (unlim&,unlim&),
operator != (unlim&,unlim&),
operator > (unlim&,unlim&),
operator >= (unlim&,unlim&),
operator < (unlim&,unlim&),
operator <= (unlim&,unlim&);
friend unlim
operator + (unlim&), // unary
operator - (unlim&), // unary
operator + (unlim&,unlim&), // binary
operator - (unlim&,unlim&), // binary
operator * (unlim&,unlim&),
abs(unlim&);
friend ostream
&operator << (ostream&,unlim&);
private:
struct descriptor
{
char
*body;
COUNT
len,
HowMany;
};
descriptor
*pv; //pointer to value descriptor
char
sign,
digit(COUNT number);
char &operator [](COUNT i) {return pv->body[i];}
void
init0(), //init by zero
NotDigit(), //message "no digit" & init0
optimize(), //optimize length of body
error(char*); //display error message
};
inline void unlim::error(char *message)
{
cout <<"Unlim class error: "
<<message
<<"\n";
}
void unlim::init0()
{
(pv->body)=new char;
*(pv->body)=0;
(pv->len)=1;
sign=PLUS;
}
char unlim::digit(COUNT number)
{
if ( number>=(pv->len) )
return ILLEGAL;
char byte=(pv->body)[number/2];
if (number%2==0)
return byte%10;
else
return byte/10;
}
unlim::unlim()
{
pv=new descriptor;
init0();
(pv->HowMany)=1;
}
unlim::~unlim()
{
if ( --(pv->HowMany)==0 )
{
delete pv->body;
delete pv;
}
}
char DecVal(char symbol)
{
if ( isdigit(symbol) )
return symbol-'0';
return ILLEGAL;
}
unlim::unlim(char *string)
{
pv=new descriptor;
(pv->HowMany)=1;
COUNT Length=strlen(string);
if (Length==0)
{
error("Empty string assigned. Value=0");
init0();
return;
}
else
{
COUNT LeftLimit=0;
switch (string[0])
{
case '-':
sign=MINUS;
LeftLimit=1;
break;
case '+':
LeftLimit=1;
default:
sign=PLUS;
}
if (Length-LeftLimit==0)
{
error("Sign without value. Value=0");
init0();
return;
}
else
{
while (string[LeftLimit]=='0')
LeftLimit++;
if ( (Length-=LeftLimit)==0 )
{
init0();
return;
}
COUNT DestLength=Length/2+Length%2;
(pv->body)=new char[DestLength];
for ( COUNT si=Length+LeftLimit-1, ki=0 ; ki<DestLength ; si-=2,ki++ )
{
char a=DecVal(string[si]);
if (a==ILLEGAL)
{
NotDigit();
return;
}
(pv->body)[ki]=a;
if (si!=LeftLimit)
{
char a=DecVal(string[si-1]);
if (a==ILLEGAL)
{
NotDigit();
return;
}
(pv->body)[ki]+=10*a;
}
}
(pv->len)=Length;
}
}
}
void unlim::NotDigit()
{
error("Not digit symbol in string. String ignored. Value=0");
delete pv->body;
init0();
}
unlim::unlim(unlim &arg)
{
(arg.pv)->HowMany++;
pv=arg.pv;
sign=arg.sign;
}
unlim &unlim::operator=(unlim &arg)
{
(arg.pv)->HowMany++;
if ( --(pv->HowMany)==0 )
{
delete pv->body;
delete pv;
}
pv=arg.pv;
sign=arg.sign;
return *this;
}
unlim &unlim::operator=(char *string)
{
return *this=unlim(string);
}
ostream &operator<<(ostream &s,unlim &x)
{
if (x.sign==MINUS)
s << "-";
←ïðåäûäóùàÿ ñëåäóþùàÿ→
1 2 3
|
|