Description The
program fills in the blanks and outputs the specified result
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int MAX = 110;
class CHugeInt {
// 在此处补充你的代码
};
int main()
{
char s[210];
int n;
while (cin >> s >> n) {
CHugeInt a(s);
CHugeInt b(n);
cout << a + b << endl;
cout << n + a << endl;
cout << a + n << endl;
b += n;
cout << ++ b << endl;
cout << b++ << endl;
cout << b << endl;
}
return 0;
}
Sample input
99999999999999999999999999888888888888888812345678901234567789 12
6 6
Sample output
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
25
25
26
12
12
12
13
13
14
analysis:
- Constructor
CHugeInt(char* s)
andCHugeInt(int n)
- Overloaded operator
CHugeInt& operator+(cosnt CHugeInt& c)
class + class
CHugeInt operator+(int n)
class + int
friend CHugeInt operator+(int n,CHugeInt& c)
int + class
CHugeInt operator+=(int n)
+=
CHugeInt operator++()
front ++
CHugeInt operator++(int)
post ++
friend ostream& operator<<(ostream& os,const CHugeInt c)
stream insertion.
The return value or reference will be discussed in detail- The difficulty lies in: how to calculate the addition.
First of all, I definitely want to reverse the array so that it can be calculated in order. Then the method used thinks of the full adder of the computer system (1), three inputs (s1, s2 and carry before carry), and two outputs (sum and carry carry). Both create a temporary object, and then the temporary object to store the structure. Set up two chars to represent the corresponding elements of the arrays on the two class objects, and then temporarily int k,,
k= c1+c2-'0' -'0'+carry
and then judge whether k is greater than or equal to 10, and consider the next bit carry.
The termination condition is to terminate when c1\c2\carry is equal to 0. The 0 here also means that the null pointer means for char.
private:
char ch[210];
public:
void reverse(char *ch)
{
int i=0, j=strlen(ch)-1;
for(;i<=j;i++,j--)
swap(ch[i],ch[j]);
}
CHugeInt(const char* s)
{
memset(ch,'\0',sizeof(ch));
strcpy(ch,s);
reverse(ch);
}
CHugeInt(int n)
{
memset(ch,'\0',sizeof(ch));
sprintf(ch,"%d",n);
reverse(ch);
}
CHugeInt operator+(const CHugeInt& c)
{
CHugeInt temp(0);
int carry=0;
for(int i=0;i<210;i++)
{
char c1= ch[i];
char c2= c.ch[i];
cout<<c1<<" "<<c2<<endl;
if(c1==0&c2==0&&carry==0)
break;
if(c1==0)
c1='0';
if(c2==0)
c2='0';
int k = c1-'0'+c2-'0'+carry;
if(k>=10)
{
k=k%10;
temp.ch[i] = k+'0';
carry =1;
}
else
{
carry = 0;
temp.ch[i] = k+ '0';
}
}
return temp;
}
CHugeInt operator+(int n)
{
return *this + CHugeInt(n);
}
friend CHugeInt operator+(int n,CHugeInt& c)
{
return c + CHugeInt(n) ;
}
CHugeInt& operator+=(int n)
{
*this= *this + n;
return *this;
}
CHugeInt& operator++()
{
*this = *this + 1;
return *this ;
}
CHugeInt operator++(int a)
{
CHugeInt temp(*this);
*this = temp+CHugeInt(1);
return temp;
}
friend ostream& operator<<(ostream& os,const CHugeInt& h)
{
for(int i=strlen(h.ch)-1;i>=0;i--)
os<<h.ch[i];
return os;
}
- The newly learned function
memset(char*,char,sizeof(char_arr)
is to set all the arrays to char,
sprintf(char*,"%d",int)
that is, to write int as a string into char*, which
swap(char* s1,char* s2)
is to exchange- What
kind of return class + class return value, because the returned temp is a temporary object,
other return values or references can be, depending on how you write the return, for example,
return *this+1
it must return a value, because this is a return value member function (class + class )
*this = *this+1; return *this
That can return a reference.