#include <iostream.h>
#include <math.h>
#include <string.h>
#define Max 1024
class CLongInt
{
boolsymbol;//数字符号
intdigit;//数字位数
charnumeral[Max];//用来存放各位的数字
public:
CLongInt(char*str)
{
symbol=true;
digit=0;
memset(numeral,0,Max);
input(str);
}
CLongInt()
{
symbol=true;//默认为正数
digit=0;//默认为0
numeral[0]='0';
}
friend ostream& operator<<(ostream& os,CLongInt& d);
friend istream& operator>>(istream& is,CLongInt& d);
friendCLongInt abs_add(CLongInt c1,CLongIntc2); //绝对值相加
friendCLongInt abs_minus(CLongInt c1,CLongIntc2);//绝对值相减
friend CLongInt abs_mul(CLongInt c1,CLongIntc2);//绝对值相乘
friend CLongInt abs_mul(CLongInt c1,int abs,intsize);
//friendCLongInt abs_div(CLongInt c1,CLongIntc2);//绝对值相除
int CompareLongInt(CLongInt c1,CLongIntc2);// 比较长整数大小
voidinput(char* str);
void print();
CLongInt operator +(CLongInt d);
CLongInt operator -(CLongInt d);
CLongInt operator *(CLongInt d);
};
void CLongInt::print()
{
int i;
if(symbol==false)
cout<<"-";
for(i=digit-1;i>=0;i--)
{
cout<<numeral[i];
//puts(numeral);
}
}
intCLongInt::CompareLongInt(CLongInt c1,CLongInt c2)//0大于,1等于,2小于
{
int i;
if(c1.symbol==c2.symbol)//比较的两者符号相同
{
if(c1.symbol==true)
{
if(c1.digit>c2.digit)
{
return 0;
}
elseif(c1.digit<c2.digit)
{
return 2;
}
else//两者长度相等
{
for(i=c1.digit-1;i>=0;i--)
{
if(c1.numeral[i]>c2.numeral[i]){
return 0;
break;
}
else if(c1.numeral[i]<c2.numeral[i]){
return 2;
break;
}
}
if(i==-1){
return 1;
}
}
}
if(c1.symbol==false)
{
if(c1.digit>c2.digit)
{
return 2;
}
elseif(c1.digit<c2.digit)
{
return 0;
}
else//两者长度相等
{
for(i=c1.digit-1;i>=0;i--)
{
if(c1.numeral[i]>c2.numeral[i]){
return 2;
break;
}
else if(c1.numeral[i]<c2.numeral[i]){
return 0;
break;
}
}
if(i==-1){
return 1;
}
}
}
}
else//比较的两者符号不同
{
if(c1.symbol==true)//前正后负
{
return 0;
}
else if(c1.symbol!=true)
{
return 2;
}
}
}
void CLongInt::input(char*str)
{
this->digit=0;
int i=0,j=0;
int len=strlen(str);
char*s=(char*)str;
if(*str<='9'&&*str>='0')
{
this->symbol=true;
this->digit = len;
}
else if(*str=='-')
{
i++;
this->digit = len-1;
this->symbol=false;
}
else if(*str=='+')
{
i++;
this->digit = len-1;
this->symbol=true;
}
else
{
cout<<"数据输入错误,请重新输入!!!";
return;
}
while(i<len)//除了第一位其他位都应该是数字
{
if(s[i]>'9'||s[i]<'0')
{
//cout<<"liufenfen"<<s[i]<<endl;
this->digit=0;
cout<<"数据输入错误,请重新输入!!!";
return;
}
else
{
this->numeral[this->digit-j-1]=s[i];//除去正负号
this->numeral[this->digit]= ' ';
}
i++;
j++;
}
}
CLongInt abs_add(CLongIntc1,CLongInt c2)//绝对值相加
{
CLongInt temp;
int carry =0;//标记进位
int i,number = 0;
if(c1.digit>=c2.digit)//前一个数比后一个数位数要大
{
for(i=0;i<c2.digit;i++)
{
//cout<<"c1:"<<c1.numeral[i]<<endl;
//cout<<"c2:"<<c2.numeral[i]<<endl;
//cout<<"c1+c2:"<<((c1.numeral[i]-'0')+(c2.numeral[i]-'0'))<<endl;
temp.numeral[i]=((c1.numeral[i]-'0')+(c2.numeral[i]-'0')+carry)%10+ 48; //转化成数字运算然后转化成字符
//每次进位后将进位清零
if(((c1.numeral[i]-'0')+(c2.numeral[i]-'0')+carry)/10==1){//有进位
carry = 1;
} else
carry=0;
}
for(i=c2.digit;i<c1.digit;i++)//超越的位数
{
temp.numeral[i]=((c1.numeral[i]-'0')+carry)%10+48;
if(((c1.numeral[i]-'0')+carry)/10==1){
carry = 1;
} else
carry=0;
}
if(carry == 1)
{//最高位进位判断
temp.digit=c1.digit+1;
temp.numeral[c1.digit]= '1';
temp.numeral[c1.digit+1]= ' ';
}
else
{
temp.digit=c1.digit;
temp.numeral[c1.digit]= ' ';
}
}
else//后一个位数要大
{
for(i=0;i<c1.digit;i++)
{
//cout<<"c1:"<<c1.numeral[i]<<endl;
//cout<<"c2:"<<c2.numeral[i]<<endl;
//=cout<<"c1+c2:"<<((c1.numeral[i]-'0')+(c2.numeral[i]-'0'))<<endl;
temp.numeral[i]=((c1.numeral[i]-'0')+(c2.numeral[i]-'0')+carry)%10+ 48;
//转化成数字运算然后转化成字符
if(((c1.numeral[i]-'0')+(c2.numeral[i]-'0')+carry)/10==1){//有进位
carry = 1;
}
else
carry=0;//每次进位后将进位清零
}
for(i=c1.digit;i<c2.digit;i++)//超越的位数
{
temp.numeral[i]=((c2.numeral[i]-'0')+carry)%10+48;
if(((c2.numeral[i]-'0')+carry)/10==1){
carry = 1;
}
else
carry=0;
}
if(carry ==1){//最高位进位判断
temp.digit=c2.digit+1;
temp.numeral[c2.digit]= '1';
temp.numeral[c2.digit+1]= ' ';
}else{
temp.digit=c2.digit;
temp.numeral[temp.digit]= ' ';
}
}
returntemp;
}
CLongInt abs_minus(CLongIntc1,CLongIntc2)//绝对值相减
{
CLongInt temp;
intcarry=0;//carry表示借位
if(c1.digit>c2.digit)//前者位数大于后者
{
for(intk=0;k<c2.digit;k++){
if(c1.numeral[k]=='0'&&carry==1){
c1.numeral[k] = '9';
carry = 1;
}elseif(carry == 1){
c1.numeral[k]=(c1.numeral[k]-'0') - 1 + 48;
carry = 0;
}
if(c1.numeral[k]<c2.numeral[k]){
carry = 1;
temp.numeral[k] = (c1.numeral[k]-'0')+10-(c2.numeral[k]-'0') +48;
}else{
temp.numeral[k]= (c1.numeral[k]-'0')-(c2.numeral[k]-'0') + 48;
}
}
for(k=c2.digit;k<c1.digit;k++){
if(c1.numeral[k]=='0'&&carry==1){
temp.numeral[k] = '9';
carry = 1;
}elseif(carry == 1){
temp.numeral[k]=(c1.numeral[k]-'0') - 1 + 48;
carry = 0;
}else{
temp.numeral[k] = c1.numeral[k];
}
}
temp.digit=c1.digit;
temp.numeral[c1.digit] =' ';
k=c1.digit-1;
while(temp.numeral[k]=='0'){
temp.digit--;
temp.numeral[k]= ' ';
k--;
}
}
elseif(c1.digit==c2.digit)//两者位数相等且前者大于后者
{
carry=0;
for(intk=0;k<c2.digit;k++){
if(c1.numeral[k]=='0'&&carry==1){
c1.numeral[k] = '9';
carry = 1;
}elseif(carry == 1){
c1.numeral[k]=(c1.numeral[k]-'0') - 1 + 48;
}
if(c1.numeral[k]<c2.numeral[k]){
carry = 1;
temp.numeral[k] = (c1.numeral[k]-'0')+10-(c2.numeral[k]-'0') +48;
}else{
carry= 0;
temp.numeral[k]= (c1.numeral[k]-'0')-(c2.numeral[k]-'0') + 48;
}
}
temp.digit=c1.digit;
temp.numeral[c1.digit] =' ';
k=c1.digit-1;
while(temp.numeral[k]=='0'){
temp.digit--;
temp.numeral[k]= ' ';
k--;
}
}
returntemp;
}
CLongInt abs_mul(CLongIntc1,CLongIntc2)//绝对值相乘
{
CLongInttemp,mid; //中间值
int i;
temp.numeral[0]='0';
temp.digit=1;
temp.numeral[1]=' ';
for(i=0;i<c2.digit;i++)
{
intcn=c2.numeral[i]-'0';
mid=abs_mul(c1,cn,i);
temp=abs_add(temp,mid);
}
returntemp;
}
CLongInt abs_mul(CLongInt c1,intabs,intsize)//单位数与长整数相乘
{
CLongInttemp;
inti,carry=0;
temp.digit=c1.digit;
for(i=0;i<c1.digit;i++)
{
intmid=(c1.numeral[i]-'0')*abs+carry;
temp.numeral[i]=mid%10 + 48;
carry=mid/10;
}
temp.numeral[c1.digit] = ' ';
if(carry>0){
temp.digit++;
temp.numeral[c1.digit] = carry+ 48;
temp.numeral[temp.digit] =' ';
}
if(size!=0){
for(i=temp.digit;i>=0;i--){
temp.numeral[i+size]= temp.numeral[i];
}
for(i=0;i<size;i++){//在移位后的数据后面添加size个0
temp.numeral[i]= '0';
}
temp.digit+=size;
}
return temp;
}
CLongInt CLongInt::operator+(CLongInt d)
{
int i=0;
CLongInt temp;
if(symbol==d.symbol)//相加的两个数符号相同
{
temp=abs_add(*this,d);
temp.symbol=symbol;
}
else//如果两者符号不同
{
if(digit>d.digit)//前者大于后者
{
temp=abs_minus(*this,d);//做减法
temp.symbol=symbol;
}
elseif(digit==d.digit)//两者长度相等
{
for(i=digit-1;i>=0;i--)
{
if(numeral[i]>d.numeral[i])//前者数值大于后者
{
temp.symbol=symbol;
temp=abs_minus(*this, d);
}
elseif(numeral[i]<d.numeral[i])//后者数值大于前者
{
temp.symbol=d.symbol;
temp=abs_minus(d,*this);
}
else
{
temp.numeral[0]='0';//两者相等
temp.symbol=true;
}
}
}
else//前者长度小于后者长度
{
temp=abs_minus(d ,*this);
temp.symbol=d.symbol;
}
}
return temp;
}