📄 vlong.cpp
字号:
if (rhs == 0)
throw domain_error("divide by zero");
if (lhs < rhs)
return *new vlong(lhs);
vlong source = origin.substr(0, rhs.length() < origin.size() ? rhs.length() : origin.size());
origin = origin.substr(source.length(), origin.size() - source.length());
string tmp;
while (origin.size() || source >= rhs)
{
while (source >= rhs)
source-=rhs;
if (origin.size())
{
tmp = source.get_str() + origin.substr(0, 1);
source = tmp;
origin.erase(origin.begin());
}
}
vlong r(source);
return r;
}
const vlong operator+(const vlong& lhs, const int rhs)
{
vlong l(lhs);
vlong r(rhs);
return l + r;
}
const vlong operator-(const vlong& lhs, const int rhs)
{
vlong l(lhs);
vlong r(rhs);
return l - r;
}
const vlong operator*(const vlong& lhs, const int rhs)
{
vlong l(lhs);
vlong r(rhs);
return l * r;
}
const vlong operator/(const vlong& lhs, const int rhs)
{
vlong l(lhs);
vlong r(rhs);
return l / r;
}
const vlong operator%(const vlong& lhs, const int rhs)
{
vlong l(lhs);
vlong r(rhs);
return l % r;
}
const vlong operator+(const vlong& lhs, const string& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l + r;
}
const vlong operator-(const vlong& lhs, const string& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l - r;
}
const vlong operator*(const vlong& lhs, const string& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l * r;
}
const vlong operator/(const vlong& lhs, const string& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l / r;
}
const vlong operator%(const vlong& lhs, const string& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l % r;
}
const vlong operator+(const int lhs, const vlong& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l + r;
}
const vlong operator-(const int lhs, const vlong& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l - r;
}
const vlong operator*(const int lhs, const vlong& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l * r;
}
const vlong operator/(const int lhs, const vlong& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l / r;
}
const vlong operator%(const int lhs, const vlong& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l % r;
}
const vlong operator+(const string& lhs, const vlong& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l + r;
}
const vlong operator-(const string& lhs, const vlong& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l - r;
}
const vlong operator*(const string& lhs, const vlong& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l * r;
}
const vlong operator/(const string& lhs, const vlong& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l / r;
}
const vlong operator%(const string& lhs, const vlong& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l % r;
}
const vlong operator+(const int lhs, const string& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l + r;
}
const vlong operator-(const int lhs, const string& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l - r;
}
const vlong operator*(const int lhs, const string& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l * r;
}
const vlong operator/(const int lhs, const string& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l / r;
}
const vlong operator%(const int lhs, const string& rhs)
{
vlong l(lhs);
vlong r(rhs);
return l % r;
}
const vlong operator+(const string& lhs, const int rhs)
{
vlong l(lhs);
vlong r(rhs);
return l + r;
}
const vlong operator-(const string& lhs, const int rhs)
{
vlong l(lhs);
vlong r(rhs);
return l - r;
}
const vlong operator*(const string& lhs, const int rhs)
{
vlong l(lhs);
vlong r(rhs);
return l * r;
}
const vlong operator/(const string& lhs, const int rhs)
{
vlong l(lhs);
vlong r(rhs);
return l / r;
}
const vlong operator%(const string& lhs, const int rhs)
{
vlong l(lhs);
vlong r(rhs);
return l % r;
}
vlong& vlong::operator++()
{ return *this = *this + 1; }
vlong& vlong::operator--()
{ return *this = *this - 1; }
const vlong operator+=(vlong& lhs, const vlong& rhs)
{ return lhs = lhs + rhs; }
const vlong operator+=(vlong& lhs, const int rhs)
{ return lhs = lhs + rhs; }
const vlong operator+=(vlong& lhs, const string& rhs)
{ return lhs = lhs + rhs; }
const vlong operator-=(vlong& lhs, const vlong& rhs)
{ return lhs = lhs - rhs; }
const vlong operator-=(vlong& lhs, const int rhs)
{ return lhs = lhs - rhs; }
const vlong operator-=(vlong& lhs, const string& rhs)
{ return lhs = lhs - rhs; }
const vlong operator*=(vlong& lhs, const vlong& rhs)
{ return lhs = lhs * rhs; }
const vlong operator*=(vlong& lhs, const int rhs)
{ return lhs = lhs * rhs; }
const vlong operator*=(vlong& lhs, const string& rhs)
{ return lhs = lhs * rhs; }
const vlong operator/=(vlong& lhs, const vlong& rhs)
{ return lhs = lhs / rhs; }
const vlong operator/=(vlong& lhs, const int rhs)
{ return lhs = lhs / rhs; }
const vlong operator/=(vlong& lhs, const string& rhs)
{ return lhs = lhs / rhs; }
const vlong operator%=(vlong& lhs, const vlong& rhs)
{ return lhs = lhs % rhs;}
const vlong operator%=(vlong& lhs, const int rhs)
{ return lhs = lhs % rhs;}
const vlong operator%=(vlong& lhs, const string& rhs)
{ return lhs = lhs % rhs;}
/* -------------- arithmetic overload ----------------*/
/* -------------- stream overload ----------------*/
ostream& operator<<(ostream& os, const vlong& v)
{ os << v.get_str(); }
istream& operator>>(istream& is, vlong& v)
{
string s;
is >> s;
v = s;
}
/* -------------- stream overload ----------------*/
// Propety function
void vlong::input()
{
cout << "Please input the very long num: ";
cin >> num_str;
is_positive = check_positive(num_str);
check_input();
}
const string vlong::get_str() const
{ return num_str; }
void vlong::output() const
{
if (!is_positive) cout << '-';
cout << num_str << endl;
}
void vlong::output_num_vector() const
{
for (vector<long>::const_iterator i = num_vector.begin(); i != num_vector.end(); ++i)
cout << *i << " ";
cout << endl;
}
const bool vlong::get_sig() const
{ return is_positive; }
void vlong::set_sig(bool b)
{ is_positive = b; }
const int vlong::length() const
{ return num_str.size(); }
const vector<long> vlong::get_vec() const
{ return num_vector; }
const vlong abs(const vlong& v)
{
vlong r(v);
r.set_sig(true);
return r;
}
// check function
void vlong::check_input()
{
for (string::iterator i = num_str.begin(); i != num_str.end() ; ++i)
if (*i < '0' || *i > '9')
throw domain_error("INPUT WRONG!!!");
while(*num_str.begin() == '0')
num_str.erase(num_str.begin()++);
}
bool vlong::check_positive(string& s)
{
string::iterator i = s.begin();
if (*i == '-')
{
s.erase(i);
return false;
}
return true;
}
// turned the vector to string, every element in vector is 8-digital decimal
void vlong::vec_to_str()
{
string str;
num_str = "";
stringstream tran;
for (vector<long>::size_type i = 0; i < num_vector.size(); ++i)
{
tran << num_vector[i];
tran >> str;
if (!(str == "0" && num_vector.size() > 1))
num_str += str;
tran.clear();
}
}
// turned the string to vector, every element in vector is 8-digital decimal
void vlong::str_to_vec()
{
long power;
if (num_str == "")
{
vector<long> new_vec;
new_vec.push_back(0);
num_vector = new_vec;
return;
}
for (vector<long>::size_type i = num_vector.size(); i <= (num_str.size() - 1) / seg_length; ++i)
num_vector.push_back(0);
for (vector<long>::size_type i = 0; i < num_vector.size(); ++i)
num_vector[i] =0;
string::iterator i = num_str.end() - 1;
for (vector<long>::size_type counter = num_vector.size(); counter > 0; --counter)
{
power = 1;
for (int j = 0; j < seg_length; ++j)
{
if (i + 1 != num_str.begin())
num_vector[counter - 1] += power * (*i-- - '0');
else
j = seg_length;
power *= 10;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -