#include
#include
#include
#include
using namespace std;
class bign {
public :
//4个必须函数
bign(string str) {
char * ch = new char[str.length()];
strcpy(ch,str.c_str());
for (int i=str.length()-1; i>=0; --i) this->bnArr.push_back(int(ch[i])-48);
this->length = bnArr.size();
}
int * bnA() {
int * bnAS = new int[this->length];
for (int i=0; i
bnAS[i] = this->bnArr[i];
return bnAS;
}
int len() {
return this->length;
}
void chPrivate(string str) {
this->bnArr.clear();
char * ch = new char[str.length()];
strcpy(ch,str.c_str());
for (int i=str.length()-1; i>=0; --i) this->bnArr.push_back(int(ch[i])-48);
this->length = this->bnArr.size();
}
//add()不用其他函数
string add(bign a) {
vector
vector
vector
vector
string sumStr="";
for(int i=0; i
for(int i=0; i
while(tempA.size() < tempT.size()) {
tempA.push_back(0);
}
if(tempT.size() < tempA.size())
while(tempA.size() > tempT.size()) {
tempT.push_back(0);
}
int bey=0;
for(vector
sum.push_back(((*it1)+(*it2)+bey)%10);
bey = ((*it1)+(*it2)+bey)/10;
}
if(bey) sum.push_back(bey);
for (int i=0; i
sumStr += (* it);
return sumStr;
}
//sub()不用其他函数
string sub(bign s) {
vector
vector
vector
vector
string subStr="";
bool equJud = true;
for(int i=0; i
for(int i=0; i
for(int i=tempT.size()-1; i>=0; --i) {
if(tempT[i]!=tempS[i]) {
if (tempT[i]
}
}
}
if((tempT.size()>tempS.size())||((tempT.size()==tempS.size())&&(equJud==true))) {
while(tempT.size()>tempS.size()) {
tempS.push_back(0);
}
for(int i = 0; i
else {
int j=1;
while(tempT[i+j] == 0) ++j;
--tempT[i+j];
while(--j) tempT[i+j] = 9;
sub.push_back(tempT[i]+10-tempS[i]);
}
}
}
if((tempT.size()
}
for(int i = 0; i
else {
int j=1;
while(tempS[i+j] == 0) ++j;
--tempS[i+j];
while(--j) tempS[i+j] = 9;
sub.push_back(tempS[i]+10-tempT[i]);
}
}
subStr+= "-";
}
for(int i=sub.size()-1; (sub[i]==0)&&(i!=0); --i) sub.pop_back();
for (int i=0; i
subStr += (* it);
return subStr;
}
//multi()用到了add函数
string multi(bign m) {
vector
vector
vector
vector
string multiStr="";
for(int i=0; i
for(int i=0; i
for(vector
int bey=0,dis=0;
for(vector
mul.push_back(((*it1)*(*it2)+bey)%10);
bey = ((*it1)*(*it2)+bey)/10;
++dis;
}
if(bey) {
mul.push_back(bey);
++dis;
}
if(it1!=tempT.begin()) {
string str1="",str2="";
for(vector
str1 += char((* it3) + 48);
--dis;
if(dis==0) {
int tempPro = pro;
while(tempPro--) str1 += "0";
for(--it3; it3!=mul.begin(); --it3) str2 += char((* it3) + 48) ;
str2 += char((* mul.begin()) + 48);
bign tempAdd1(str1);
bign tempAdd2(str2);
string addStr = tempAdd1.add(tempAdd2);
char * mulCh = new char[addStr.length()];
strcpy(mulCh,addStr.c_str());
mul.clear();
for (int i=addStr.length()-1; i>=0; --i) mul.push_back(int(mulCh[i])-48);
}
}
}
}
for(int i=mul.size()-1; (mul[i]==0)&&(i!=0); --i) mul.pop_back();
for (int i=0; i
multiStr += (* it);
return multiStr;
}
//division()用到了sub、add函数
string division(bign d) {
vector
vector
vector
string divStr1="";
string divStr2="";
string divStr3="";
string divStr="";
for(int i=0; i
for(int i=0; i
for(vector
bign temp1(divStr1);
bign temp2(divStr2);
if(char(temp1.sub(temp2)[0])=='-') return "0";
else {
divStr3 = divStr1.substr(0,temp2.len());
bign temp3(divStr3);
for(int i=temp2.len()-1; (i
if(divStr3!="0") divStr3 += divStr1.substr(++i,1);
else divStr3 = divStr1.substr(++i,1);
temp3.chPrivate(divStr3);
if(char(temp3.sub(temp2)[0])=='-') divCh.push_back('0');
} else {
int bus=0;
string divStr4=divStr2;
bign temp4(divStr4);
while (char(temp3.sub(temp4)[0])!='-') {
++bus;
divStr4 = temp4.add(temp2);
temp4.chPrivate(divStr4);
}
divCh.push_back(char(bus+48));
divStr4 = temp4.sub(temp2);
temp4.chPrivate(divStr4);
divStr3 = temp3.sub(temp4);
temp3.chPrivate(divStr3);
}
}
for(vector
divStr += (* it);
return divStr;
}
}
//rest()用到了division函数,multi函数,sub函数
string rest(bign r) {
string str1="",str2="",restStr="";
for(int i=this->length-1; i>=0; --i) str1 += char(this->bnArr[i]+48);
for(int i=r.len()-1; i>=0; --i) str2 += char(r.bnA()[i]+48);
bign a(str1);
bign b(str2);
bign c(a.division(b));
bign d(b.multi(c));
restStr = a.sub(d);
return restStr;
}
private:
vector
int length;
};
int main() {
bign a("10001");
bign b("110");
bign c("1212999");
bign d("9989999");
cout << a.add(b) << endl << c.sub(d) << endl << c.multi(d) << endl << c.division(a) << endl << d.rest(b);
return 0;
}