#include
#include
#include
#include
using namespace std;
class very_long_int{
friend istream& operator>>(istream& instream,very_long_int& very_long);
friend ostream& operator<<(ostream& outstream,const very_long_int very_long);
public:
very_long_int operator + (const very_long_int& other_number);
very_long_int operator * (const very_long_int& other_number);
very_long_int operator =(very_long_int& other_number);
very_long_int factor(int i);
deque
char least(unsigned i) const;
};
istream& operator >> (istream& instream,very_long_int& very_long)
{
const char LOWEST_DIGIST_CHAR = '0';
const char HIGHEST_DIGIST_CHAR = '9';
const char SENTINEL = 'X';
char digist_char;
very_long.digist.erase(very_long.digist.begin(),very_long.digist.end());
do
{
instream >> digist_char;
if ( (LOWEST_DIGIST_CHAR<= digist_char) && (digist_char <= HIGHEST_DIGIST_CHAR))
very_long.digist.push_back(digist_char - LOWEST_DIGIST_CHAR);
} while (digist_char != SENTINEL);
return instream;
}
ostream& operator<<(ostream& outstream,const very_long_int very_long)
{
for (unsigned i =0;i
return outstream;
}//重载<<
very_long_int very_long_int::operator =(very_long_int& other_number) //赋值
{
digist.erase(digist.begin(),digist.end());
for(unsigned i=0;i
return *this;
}
very_long_int very_long_int::operator +(const very_long_int &other_number)
{
unsigned carry = 0,
large_size,
partial_sum;
very_long_int sum;
if (digist.size() > other_number.digist.size())
large_size = digist.size();
else
large_size = other_number.digist.size();
for (unsigned i =0;i < large_size;++i)
{
partial_sum = least(i) + other_number.least(i) + carry;
carry = partial_sum/10;
sum.digist.push_back(partial_sum%10);
}//for
if (carry == 1)
sum.digist.push_back(carry);
reverse(sum.digist.begin(),sum.digist.end());
return sum;
}//重载+
very_long_int very_long_int::operator *(const very_long_int& other_number)
{
unsigned carry=0;
very_long_int result ,temp;
for(unsigned i=0;i
temp.digist.erase(temp.digist.begin(),temp.digist.end());
carry=0;
for(unsigned j=0;j
unsigned n=least(j)*other_number.least(i)+carry;
carry=n/10;
temp.digist.push_front(n%10);
}
if(carry!=0) temp.digist.push_front(carry);
for(unsigned k=0;k //cout<<"temp:"<
}
return result;
} //重载*
//计算阶乘的算法
very_long_int very_long_int::factor(int i){
very_long_int r1,r,result;
r1.digist.push_back(1);
r.digist.push_back(1);
result.digist.push_back(1);
if (i<=1)
return r1;
for (int k = 2;k <=i;++k)
{
r = r + r1;
result = result * r;
}
return result;
}
char very_long_int::least(unsigned i) const
{
if (i >= digist.size())
return 0;
else
return digist[digist.size() - i - 1];
}//least
void main()
{
very_long_int myVeryLongInt1, myVeryLongInt2;
cout << "Please input your very long int1: "<
cout <<"Please input your very long int2: "<
cout <<"int1 + int2 = " << myVeryLongInt1 + myVeryLongInt2 << endl;
cout <<"int1 * int2 = " << myVeryLongInt1 * myVeryLongInt2 << endl;
very_long_int num;
int n ;
cout<<"输入要求的阶乘(n是正整数)n:";
cin>>n;
cout<