【C++】大数的+-*/四则运算

时间:2022-01-27 17:32:36

所谓大数,则指数值特别大的数,可能会有99位,100位,远远超过了long long表示的范围。

这样的数作四则运算,需要用到字符串。用字符串通过每一位的字符的四则运算来模拟。

废话少说,上代码:

 #include <iostream>
#include <vector>
using namespace std; class BigNum{
vector<char> m_vec;
//构造函数,析构函数,size()函数,友元重载>>,<<,重载[] + - * / =。
public:
BigNum();
~BigNum(); public:
int size() const {
return m_vec.size();
}
void clr(){
m_vec.clear();
} public:
friend ostream& operator<<(ostream &os,const BigNum &data);
friend istream& operator>>(istream &is,BigNum &data); public:
char operator[](int nIndex) const; BigNum operator+(const BigNum &data);
BigNum operator-(BigNum &data);
BigNum operator*(const BigNum &data);
BigNum operator/(BigNum &data);
BigNum& operator=(const BigNum &data);
bool operator==(const BigNum &data);
bool operator>(const BigNum &data);
bool operator>=(const BigNum &data);
}; BigNum::BigNum(){} BigNum::~BigNum(){} ostream& operator<<(ostream &os,const BigNum &data){
vector<char>::const_iterator itVec = data.m_vec.begin();
for(; itVec != data.m_vec.end(); itVec++){
os << *itVec;
}
os << endl;
return os;
} istream& operator>>(istream &is,BigNum &data){
//必须清空is的缓冲区,不然is.get()得到的是上一次回车的\n。并且不能用is.ignore(),因为ignore()会把第一次输入的第一个字符删除掉。
is.sync();
while(true){
char c = is.get();
if(' ' == c || '\r' == c || '\n' == c)
break;
if( c < '' || c > ''){
data.m_vec.clear();
cerr << "not accept" << endl;
break;
}
data.m_vec.push_back(c);
} return is;
} char BigNum::operator[](int nIndex) const
{
if(nIndex < || nIndex >= m_vec.size())
{
cerr << "error occur in []";
return ' ';
}
return m_vec[nIndex];
} bool BigNum::operator==(const BigNum &data){
if(m_vec.size() != data.m_vec.size())
return false;
for(int i = ; i < m_vec.size(); i++){
if(m_vec[i] != data.m_vec[i])
return false;
}
return true;
} bool BigNum::operator>(const BigNum &data){
int a = m_vec.size();
int b = data.m_vec.size();
if(a > b){
return true;
}
else if(a < b){
return false;
}
else if(a == b)
{
for(int i = ; i < a; i++){
if(m_vec[i] > data.m_vec[i]){
return true;
}
else if(m_vec[i] < data.m_vec[i]){
return false;
}
}
return false;
}
} bool BigNum::operator>=(const BigNum &data){
if(*this > data || *this == data)
return true;
return false;
} BigNum BigNum::operator+ (const BigNum &data){
int nCarry = ;
BigNum numTemp;
vector<char>& vecTemp = numTemp.m_vec; //必须是引用 int i = data.size() - ;
int j = m_vec.size() -;
for(; i >= || j >= ; i--,j--){
char a = j>=? m_vec[j] - '': ;
char b = i>=? data[i] - '' :;
char c = a + b + nCarry;
nCarry = c / ;
vecTemp.push_back(c% + '');
} if(nCarry != )
{
vecTemp.push_back(nCarry + '');
} //reverse vecTemp
for(i = , j = vecTemp.size() - ; i < j; i++,j--)
{
char cTemp = vecTemp[i];
vecTemp[i] = vecTemp[j];
vecTemp[j] = cTemp;
}
return numTemp;
} BigNum& BigNum::operator=(const BigNum &data)
{
m_vec.clear();
vector<char>::const_iterator itVec = data.m_vec.begin();
for(; itVec != data.m_vec.end(); itVec++)
{
m_vec.push_back(*itVec);
}
return *this;
} BigNum BigNum::operator*(const BigNum &data){
int nCarry = ;
BigNum result;
BigNum numTemp;
vector<char>& vecTemp = numTemp.m_vec; int i = data.size()-;
for(; i >= ; i--){
char a = data[i] - '';
int j = m_vec.size() - ;
for(; j >= ; j--){
char b = m_vec[j] - '';
char c = b * a + nCarry;
nCarry = c/;
vecTemp.push_back(c % + '');
}
if(nCarry != ){
vecTemp.push_back(nCarry + '');
nCarry = ;
} //reverse vecTemp
int n = ;
int m = vecTemp.size() - ;
for(; n < m; n++,m--){
char cTemp = vecTemp[n];
vecTemp[n] = vecTemp[m];
vecTemp[m] = cTemp;
} for(int t = data.size() - ; t > i; t--)
{
vecTemp.push_back('');
} result = result + numTemp;
vecTemp.clear();
}
return result;
} BigNum BigNum::operator-(BigNum &data){
//m_vec > data.m_vec
int nCarry = ;
BigNum numTemp;
vector<char>& vecTemp = numTemp.m_vec; int i = data.size() - ;
int j = m_vec.size() -;
for(; i >= || j >= ; i--,j--){
char a = j>=? m_vec[j] - '': ;
char b = i>=? data[i] - '' :;
char c = a - nCarry;
if(c < b){
//需要借位
c = c + ;
nCarry = ;
vecTemp.push_back( (c-b) + '');
}
else{
nCarry = ;
vecTemp.push_back( (c-b) + '');
}
} //记录前面产生几个0
int zero = ;
vector<char>::const_iterator itVec = vecTemp.end()-;
for(; ; itVec--){
if( *itVec == ''){
zero++;
}
else
break;
if(itVec == vecTemp.begin())
break;
} //pop掉0
for(int k = zero; k > ; k--)
vecTemp.pop_back(); //reverse
for(i = , j = vecTemp.size() - ; i < j; i++,j--)
{
char cTemp = vecTemp[i];
vecTemp[i] = vecTemp[j];
vecTemp[j] = cTemp;
} return numTemp;
} BigNum BigNum::operator/(BigNum &data){
//m_vec > data.m_vec
BigNum numTemp;
vector<char>& vecTemp = numTemp.m_vec;
BigNum &numThis = *this; int lenA = m_vec.size();
int lenB = data.m_vec.size();
for (int i = lenB; i < lenA; i++){
data.m_vec.push_back('');
}
for(int j = lenB; j <= lenA; j++){
int a = ;
while(numThis >= data){
numThis = numThis - data;
a++;
}
vecTemp.push_back( a + '');
data.m_vec.pop_back();
}
return numTemp;
} int main()
{
BigNum numA;
BigNum numB;
BigNum numC;
char opt;
while(){
cin >> numA;
cin >> opt;
cin >> numB; switch(opt){
case '+':
numC = numA + numB;
cout << numC;
break;
case '-':
if(numA == numB){
cout << << endl;
}
else if(numA > numB){
numC = numA - numB;
cout << numC;
}
else{
numC = numB - numA;
cout << '-';
cout << numC;
}
break;
case '*':
numC = numA * numB;
cout << numC;
break;
case '/':
if(numA == numB){
cout << << endl;
}
else if(numA > numB){
numC = numA / numB;
cout << numC;
}
else{
cout << << endl;
}
break;
}
//清空numA,numB。
numA.clr();
numB.clr();
numC.clr();
}
return ;
}