#include
#include
#include
#include
#include
using namespace std;
template
void _swap(T &a, T &b)
{
T tmp = T();
tmp = a;
a = b;
b = tmp;
}
class BigData
{
public:
//0初始化
BigData(){}
BigData(const char *src);
BigData(bool flag,int size);
BigData(int data);
BigData(vector& src);
BigData(string &src);
BigData operator =(const BigData &data);
BigData(const BigData&data);
BigData(bool sign,const BigData&data);
bool operator >(const BigData &data) const;
bool operator <(const BigData &data) const;
bool operator ==(const BigData &data) const;
bool operator !=(const BigData &data) const;
bool operator >=(const BigData &data) const;
bool operator <=(const BigData &data) const;
BigData operator -(); //取反运算符
BigData operator+(const BigData &src);
BigData operator-(const BigData &src);
BigData operator *(const BigData &data);
BigData operator /(const BigData &data);
BigData operator +=(const BigData &daat);
BigData operator -=(const BigData &data);
BigData operator *=(const BigData &daat);
BigData operator /=(const BigData &data);
BigData operator ++(); //前置++
BigData operator --();
BigData operator ++(int);//后置++
BigData operator --(int);
void show()
{
if (_data.size() == 0)
{
cout <<"0"<(cout, ""));
cout << endl;
}
private:
vector _data;
bool _sign;
};
BigData::BigData(const char *src)
{
_sign = true;
if (src[0] == '-')
{
_sign = false;
}
int len = strlen(src);
int min = 0;
if (!_sign)
{
min = 1;
}
for (int i = len - 1; i >= min; i--)
{
if (src[i] == '-' && i == 0)
{
_sign = false;
break;
}
_data.push_back(src[i] - '0');
}
}
BigData::BigData(int data)
{
if (data < 0)
{
_sign = false;
}
else
{
_sign = true;
}
int tmp = 0;
while (data)
{
tmp = data % 10; //从低位到高位得到每一位数字
_data.push_back(tmp); //存入每一位数字
data /= 10;
}
cout << "succeed" << endl;
}
BigData::BigData(bool sign,const BigData &data)
{
this->_sign = sign;
_data = data._data;
cout << "succeed" << endl;
}
BigData::BigData(bool flag, int size)
{
_sign = flag;
while (size > 0)
{
_data.push_back(0);
size--;
}
}
BigData::BigData(vector& src)
{
_sign = true;
_data = src;
cout << "succeed" << endl;
}
BigData::BigData(const BigData &data)
{
this->_sign = data._sign;
_data = data._data;
cout << "succeed" << endl;
}
BigData::BigData(string &src)
{
_sign = true;
int len = src.size();
for (int i = len - 1; i >= 0; i--)
{
if (src[i] == '-' && i == 0)
{
_sign = false;
break;
}
_data.push_back(src[i] - '0');
}
}
BigData BigData::operator+(const BigData &src)
{
BigData num1 = *this;
BigData num2 = src;
if (_sign == true && src._sign == false)//正+负---正-(-负)
{
num2 = -num2;
return num1 - num2;
}
if (_sign == false && src._sign == true)//负+正---正-(-负)
{
num1 = -num1;
return num2 - num1;
}
//正正,负负
vector::iterator it1 = num1._data.begin();
vector::iterator it2 = num2._data.begin();
BigData tmp;
int add = 0;
for (; it1 != num1._data.end() && it2 != num2._data.end();it1++, it2++)
{
tmp._data.push_back((*it1 + *it2 + add) % 10);
add = (*it1 + *it2 + add) / 10;
}
while (it1 != num1._data.end())
{
tmp._data.push_back(*it1++ + add);
add = 0;
}
while (it2 != num2._data.end())
{
tmp._data.push_back(*it2++ + add);
add = 0;
}
tmp._sign = this->_sign;
return tmp;
}
BigData BigData::operator-(const BigData &src)
{
BigData num1 = *this;
BigData num2 = src;
BigData tmp;
if (_sign == true && src._sign == false) //正-负---正+(-负)
{
num2 = -num2;
return num1 + num2;
}
if (_sign == false && src._sign == true) //负-正---负+(-正)
{
num2 = -num2;
return num1 + num2;
}
if (_sign == false && src._sign == false)
{
if (num1 < num2) //
{
BigData temp(false, (-num1) - (-num2));
return temp;
}
if (num1 > num2)
{
BigData temp(true, (-num2) - (-num1));
return temp;
}
else
{
return tmp;
}
}
if (_sign == true && src._sign == true)
{
if (num1 < num2) //
{
BigData temp(false, (num2 - num1)); //正正
return temp;
}
}
bool flag = true;
vector::iterator it1 = num1._data.begin();
vector::iterator it2 = num2._data.begin();
tmp._sign = flag;
int add = 0;
for (; it1 != num1._data.end() && it2 != num2._data.end();
it1++, it2++)
{
tmp._data.push_back((*it1 - *it2 ));
}
while (it1 != num1._data.end())
{
tmp._data.push_back(*it1++);
add = 0;
}
for (unsigned int i = 0; i < tmp._data.size() - 1; i++ )
{
if (tmp._data[i] < 0)
{
tmp._data[i] += 10;
tmp._data[i + 1] --;
}
}
tmp._sign = this->_sign;
return tmp;
}
bool BigData::operator >(const BigData &data) const
{
cout << "bool BigData::operator >(const BigData &data) const"<_sign != data._sign) //符号不同
{
return _sign > data._sign;
}
if (this->_data.size() > data._data.size()) //符号相同,且this长
{
if (this->_sign == true)
{
return true;
}
else
{
return false;
}
}
if (this->_data.size() < data._data.size())//符号相同,且this短
{
if (this->_sign == true)
{
return false;
}
else
{
return true;
}
}
for (int i = _data.size() - 1; i >=0; i--) //符号相同,长度相同
{
if (_data[i] > data._data[i])
{
if (this->_sign == true)
{
return true;
}
else
{
return false;
}
}
else if (_data[i] < data._data[i])
{
if (this->_sign == true)
{
return false;
}
else
{
return true;
}
}
}
return false;
}
bool BigData::operator <(const BigData &data) const
{
cout <<"bool BigData::operator <(const BigData &data) const"<< endl;
if (!(*this > data) && !(*this == data))
{
return true;
}
return false;
}
bool BigData::operator ==(const BigData &data) const
{
cout <<"bool BigData::operator ==(const BigData &data) const"<< endl;
if (this->_data == data._data && this->_sign == data._sign)
{
return true;
}
return false;
}
bool BigData::operator !=(const BigData &data) const
{
cout <<"bool BigData::operator !=(const BigData &data) const"<< endl;
if (!(*this == data))
{
return true;
}
return false;
}
bool BigData::operator >=(const BigData &data) const
{
cout <<"bool BigData::operator >=(const BigData &data) const"<< endl;
if (*this > data || *this == data)
{
return true;
}
return false;
}
bool BigData::operator <=(const BigData &data) const
{
if (!(*this > data))
{
return true;
}
return false;
}
BigData BigData::operator =(const BigData &data)
{
cout <<"BigData BigData::operator =(const BigData &data)"<_sign = data._sign;
this->_data = data._data;
return *this;
}
BigData BigData::operator -() //改变了BigData对象的符号
{
this->_sign = !this->_sign;
return *this;
}
BigData BigData::operator *(const BigData &data)
{
BigData num1(*this);
BigData num2(data);
int len1 = num1._data.size();
int len2 = num2._data.size();
int size = len1 * len2;
bool sign = true;
if (num1._sign == true && num2._sign == false)
{
sign = false;
}
if (num1._sign == false&&num2._sign == true)
{
sign = false;
}
BigData tmp(sign,size);
for (int i = 0; i < len1; i++)
{
for (int j = 0; j < len2; j++)
{
tmp._data[i + j] += num1._data[i] * num2._data[j];
}
}
for (unsigned int i = 0; i < tmp._data.size()-1; i++)
{
if (tmp._data[i] >= 10)
{
tmp._data[i + 1] += tmp._data[i] / 10;
tmp._data[i] %= 10;
}
}
while (tmp._data.size() > 1 && tmp._data[tmp._data.size() - 1] == 0)
{
tmp._data.erase(tmp._data.end()-1);
}
return tmp;
}
BigData BigData::operator /(const BigData &data)
{
BigData tmp;
BigData num1 = *this;
BigData num2 = data;
if (num1 == num2)
{
tmp._data.push_back(1);
tmp._sign = true;
}
if (num1._sign == false && num2._sign == false)
{
if (num1 > num2)
{
return tmp;
}
num1 = -num1;
num2 = -num2;
BigData temp = num1/ num2;
return temp;
}
if (num1._sign == true && num2._sign == true)
{
if (num1 < num2)
{
return tmp;
}
tmp._sign = true;
}
if (num1._sign == false)
{
num1 = -num1;
BigData temp(false, num1 / num2);
return temp;
}
if (num2._sign == false)
{
num2 = -num2;
BigData temp (false,num1 / num2);
return temp;
}
BigData zoro(0);
zoro._sign = true;
BigData one(1);
one._sign = true;
int len = num1._data.size();
BigData temp(len,tmp._sign);
tmp = temp;
while ((num1 = (num1 - num2)) >= zoro)
{
tmp += one;
}
return tmp;
}
BigData BigData::operator +=(const BigData &data)
{
*this = *this + data;
return *this;
}
BigData BigData::operator -=(const BigData &data)
{
*this = *this - data;
return *this;
}
BigData BigData::operator *=(const BigData &data)
{
*this = *this * data;
return *this;
}
BigData BigData::operator /=(const BigData &data)
{
*this = *this / data;
return *this;
}
BigData BigData::operator ++()
{
BigData one(1);
*this += one;
return *this;
}
BigData BigData::operator --()
{
BigData one(1);
*this -= one;
return *this;
}
BigData BigData::operator ++(int)
{
BigData temp(*this);
BigData one(1);
*this += one;
return temp;
}
BigData BigData::operator --(int)
{
BigData temp(*this);
BigData one(1);
*this -= one;
return temp;
}
int main()
{
BigData data1(12442); //int类型构造BigData对象
vector d({ 2,3,2,3,4,5,6,7,5,3,2,4,4,5,6,7,5,3,4,2,5,6 });
BigData data2 = d; //vector类型构造BigData对象
BigData data3("1234565346634436"); //const char*类型构造BigData对象
BigData data4("6543216433333333333333333");//const char*类型构造BigData对象
string dt = "-134423546";
BigData data5(dt); //string类型构造BigData对象
BigData data = data3 + data4; //data3调用+方法,data4是参数
BigData data6 = data3 - data4;
BigData data8("-12");
BigData data9("24");
data = data3 - data4 - data9;
data.show();
data = data8 - data9 - data9;
data.show();
data = data8 * data9 * data8;
data.show();
data = data9 / data8;
data.show();
data++;
data.show();
data6.show();
return 0;
}