Accidentally found last year to write the course design homework, writing is also good, involved in a lot of knowledge, and now have some memory, have time to take a good look at C + +.
Topic: Design and implement large integer classes and test their subtraction operations (at least one number is more than 20 digits). Use it to calculate and display 30. (Required: Copy constructor, arithmetic overload, friend function, insert and fetch operator overload must be implemented)
Problem Analysis:
To implement a truly large integer class, where the number of digits is indeterminate and infinitely large, so select the container is the best, with the array must determine its size, then this so-called large integer number of digits have an upper limit, so here I choose to use vector, and the string class for its input and output operations, the code is as follows. Implements copy constructors, arithmetic overloads, friend functions, insert and fetch operator overloads. Arithmetic is also true for negative numbers, and division only considers the quotient.
Code implementation: (Running environment is Microsoft Visual Studio 2010)
#include <iostream> #include <vector> #include <string> #include <cstring> using namespace std;
Class BigInt {public:bigint ();
BIGINT (long int s);
BigInt (string str);
BigInt (bigint& a);
Friend BigInt operator+ (BigInt a,bigint b);
Friend BigInt operator-(BigInt a,bigint b);
Friend BigInt operator* (BigInt a,bigint b); Friend BigInt operator/(BigInt a,bigint b);//Division algorithm is not very good friend ostream& operator<< (ostream& out,bigint&
a);
Friend istream& operator>> (istream& in,bigint& a);
BigInt operator= (BigInt a);
Friend int Sizejudge (BigInt a,bigint b);
Vector<int> VEC;
int negative;//0 is positive, 1 is negative};
Bigint::bigint () {vec.push_back (0);
Negative = 0;
} bigint::bigint (long int s) {if (s<0) {negative = 1;
s =-S;
else negative = 0;
int i;
while (s>9) {i = s%10;
Vec.push_back (i);
s/=10;
} vec.push_back (s);
} bigint::bigint (String str) {if (str[0] = = '-') {negative = 1; str = STR.SUBSTR (1,stR.size ()-1);
else negative = 0;
for (int i=str.size () -1;i>=0;i--) {vec.push_back (str[i]-' 0 ');
} bigint::bigint (bigint& a) {VEC = A.vec;
negative = a.negative;
} BigInt bigint::operator= (BigInt a) {VEC = A.vec;
negative = a.negative;
return *this;
} ostream& operator<< (ostream& out,bigint& a) {string str= "";
int judge = 0;
if ((A.vec[a.vec.size () -1]+ ' 0 ') = = ') judge = 1;
if ((a.negative = = 1) &&judge = = 0) {str = '-';
for (int i=a.vec.size () -1;i>=0;i--) {str = (a.vec[i]+ ' 0 ');
} out<<str;
return out;
} istream& operator>> (istream& in,bigint& a) {string str= "";
in>>str;
A.vec.clear ();
for (int i=str.size () -1;i>=0;i--) {a.vec.push_back (str[i]-' 0 ');
} return in;
} BigInt operator+ (BigInt a,bigint b) {int negative_judge = 0;
if (a.negative = = 1&&b.negative = = 1)//full Negative {Negative_judge = 1; } if (a.negative = = 0&&b.negative = 0)/All positive {Negative_judge= 0;
} if (a.negative = = 0&&b.negative = = 1)//a positive b minus {b.negative = 0;
return (A-B);
} if (a.negative = = 1&&b.negative = = 0)//a Negative b positive {a.negative = 0;
return (B-A);
} BigInt c,tmp;
int alen,blen,min,max,i;
Alen = A.vec.size ();
Blen = B.vec.size ();
if (alen>blen) {C.vec = A.vec;
Tmp.vec = B.vec;
min = Blen;
max = Alen;
else {C.vec = B.vec;
Tmp.vec = A.vec;
min = Alen;
max = Blen;
for (i=0;i<min-1;i++)//min=max.
{C.vec[i] + = Tmp.vec[i];
if (c.vec[i]>9) {c.vec[i] = 10;
C.VEC[I+1] + + 1;
}} C.vec[i] +=tmp.vec[i];
if (c.vec[i]>9) {c.vec[i]-=10;
if (min = max) C.vec.push_back (1);
else c.vec[i+1] +=1;
for (i=min;i<max-1;i++) {if (c.vec[i]>9) {c.vec[i]-=10;
C.vec[i+1] +=1;
} if (c.vec[max-1]>9) {c.vec[max-1]-=10;
C.vec.push_back (1);
I=c.vec.size () -1;//remove the previous 0 while (c.vec[i] = = 0) {c.vec.pop_back ();
i--; } if (Negative_judge = = 1) {string str= "-";
C.vec.push_back (str[0]-' 0 ');
return C;
} BigInt operator-(BigInt a,bigint b) {int negative_judge = 0;
if (a.negative = = 1&&b.negative = = 1)//full negative {a.negative = 0;
b.negative = 0;
return (B-A);
} if (a.negative = = 0&&b.negative = = 0)//full positive {Negative_judge = 0;
} if (a.negative = = 0&&b.negative = = 1)//a positive b minus {b.negative = 0;
return (A+B);
} if (a.negative = = 1&&b.negative = = 0)//a Negative b positive {b.negative = 1;
return (A+B);
} BigInt c,tmp;
int alen,blen,min,max,i,judge=0;
Alen = A.vec.size ();
Blen = B.vec.size ();
The large value is assigned to C, and the decimal value is assigned to the TMP if (alen>blen) {C.vec = A.vec;
Tmp.vec = B.vec;
min = Blen;
max = Alen;
Judge = 1;
else if (alen = = Blen) {for (i=alen-1;i>=0;i--) {if (A.vec[i]>b.vec[i]) {C.vec = A.vec;
Tmp.vec = B.vec;
min = Blen;
max = Alen;
Judge = 1;
Break
else if (A.vec[i]<b.vec[i]) {C.vec = B.vec;
Tmp.vec = A.vec;
min = Alen; max = Blen;
Judge = 2;
Break
} if (I==-1) return (BigInt) 0;
else {C.vec = B.vec;
Tmp.vec = A.vec;
min = Alen;
max = Blen;
Judge = 2; for (i=0;i<min;i++)//min=max.
c>tmp {C.vec[i]-= tmp.vec[i];
if (c.vec[i]<0) {C.vec[i] + = 10;
C.VEC[I+1]-= 1;
} if (Min<max) for (i=min;i<max;i++) {if (c.vec[i]<0) {c.vec[i] +=10;
C.vec[i+1]-=1;
} if (judge = = 2) {string str= "-";
C.vec.push_back (str[0]-' 0 ');
I=c.vec.size () -1;//remove the previous 0 while (c.vec[i] = = 0) {c.vec.pop_back ();
i--;
return C;
} BigInt operator* (BigInt a,bigint b) {BigInt C;
int alen,blen,i,j,max,tmp;
Alen = A.vec.size ();
Blen = B.vec.size ();
max = Alen+blen;
C.vec.clear ();
for (i=0;i<max;i++) {c.vec.push_back (0);
for (i=0;i<alen;i++) {for (j=0;j<blen;j++) {tmp = C.vec[i+j]+a.vec[i]*b.vec[j];
if (tmp>9) {if (I+j+1<max) {c.vec[i+j+1] = = TMP/10;
TMP%= 10;
} else {c.vec.push_back (TMP/10);
TMP%= 10;
}} C.vec[i+j] = tmp;
} i=c.vec.size () -1;//remove the previous 0 while (c.vec[i] = = 0) {c.vec.pop_back ();
i--; } if ((a.negative = = 0&&b.negative = 1) | | (A.negative = = 1&&b.negative = 0))
A positive B negative//a negative B positive {string str= "-";
C.vec.push_back (str[0]-' 0 ');
c.negative = 1;
else {c.negative = 0;
return C;
} BigInt operator/(BigInt a,bigint b) {if (Sizejudge (a,b) = = 1) {return (BigInt) 0;
else if (Sizejudge (a,b) = = 0) {return (BIGINT) 1;
} BigInt tmp;
int alen,blen,i;
Alen = A.vec.size ();
Blen = B.vec.size ();
if (Blen = = 1&&b.vec[0] = = 0) {BigInt err ("error");
return err;
} i = Alen-blen;
int j = 1;
while (i>0) {J *=10;
i--;
BigInt Count (j);
TMP = COUNT*B;
if (Sizejudge (a,tmp) = = 1) {while (Sizejudge (a,tmp) = = 1) {count = count+1;
TMP = B*count; } else if (Sizejudge (a,tmp) = = 1) {while (Sizejudge (a,tmp) = = 1) {count = Count-1;
TMP = B*count; } if ((a.negative = = 0&&b.negative = 1) | | (A.negative = = 1&&b.negative = 0))
A positive B negative//a negative B positive {string str= "-";
Count.vec.push_back (str[0]-' 0 ');
count.negative = 1;
else {count.negative = 0;
return count;
The int sizejudge (BigInt a,bigint b) {//1 represents greater than, 0 represents equals,-1 represents less than int alen,blen,i;
Alen = A.vec.size ();
Blen = B.vec.size ();
if (Alen>blen) {return 1;
else if (alen = = Blen) {for (i=alen-1;i>=0;i--) {if (A.vec[i]>b.vec[i]) {return 1;
else if (A.vec[i]<b.vec[i]) {return-1;
} if (i==0) return 0;
else {return-1;
the int main () {////test data input//bigint A ("999999999999999999999"), B ("8888"), C;
cin>>b;
cout<< "The input:" <<b<<endl;
c = a+b;
cout<<a<< "+" <<b<< "=" <<c<<endl;
Cin.get ();
Test addition//bigint A ("999999999999999999999"), B ("8888"), C;
c = a+b; cout<<a<< "+" <<b<< "
= "<<c<<endl;
BigInt d ("-1111");
c = a+d;
cout<<a<< "+" <<d<< "=" <<c<<endl;
c = 7777777+a;
cout<< "7777777" << "+" <<a<< "=" <<c<<endl;
BigInt s (a);//Call copy constructor//c = S+a;
cout<<s<< "+" <<a<< "=" <<c<<endl;
Test subtraction//bigint A ("999999999999999999999"), B ("8888"), C;
c = a-b;
cout<<a<< "-" <<b<< "=" <<c<<endl;
BigInt d ("-1111");
c = a-d;
cout<<a<< "-" <<d<< "=" <<c<<endl;
c = 7777777-a;
cout<< "7777777" << "-" <<a<< "=" <<c<<endl;
Test multiplication//bigint A ("999999999999999999999"), B ("8888"), C;
c = a*b;
cout<<a<< "*" <<b<< "=" <<c<<endl;
BigInt d ("-1111");
c = a*d;
cout<<a<< "*" <<d<< "=" <<c<<endl;
c = 7777777*a; cout<< "7777777" << "*" <<a<< "=" <<c<<endl;
Test Division//bigint A ("999999999999999999999"), B ("888888888888888"), C;
c = A/b;
cout<<a<< "/" <<b<< "=" <<c<<endl;
c = b/a;
cout<<b<< "/" <<a<< "=" <<c<<endl;
BigInt d ("-111111111111111");
c = A/D;
cout<<a<< "/" <<d<< "=" <<c<<endl;
c = 0/a;
cout<< "0" << "/" <<a<< "=" <<c<<endl;
Calculates and displays 30.
BigInt A ("a"), C ("1");
int i = 30;
while (i>0) {c = c*a;
A = A-1;
i--; } cout<< "30!
= "<<c<<endl;
return 0;
}
Run Result:
1. Input:
2. Addition: (contains call copy constructor)
3. Subtraction:
4. Multiplication:
5. Division: (For business)
6. Calculate and display 30.