1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
void add (BigInt & res,const BigInt &a,const BigInt &b) {
//value of res is altered.
}
void sub(BigInt & res,const BigInt & a,const BigInt & b) {
//value of res is altered.
}
BigInt operator + (const BigInt & a,const BigInt & b) {
BigInt res;
res.digits.reserve(a.digits.size()+2);
if (b.negative==a.negative) {
if (b.digits.size()>a.digits.size()) {
add(res,b,a);
} else {
add(res,a,b);
}
} else if (a.negative) {
if (b>a) {
sub(res,b,a);
res.negative=false;
} else {
sub(res,a,b);
res.negative=true;
}
} else {
if (a>b) {
sub(res,a,b);
res.negative=false;
} else {
sub(res,b,a);
res.negative=true;
}
}
return res;
}
BigInt operator - (const BigInt & a,const BigInt & b) {
BigInt res;
res.digits.reserve(a.digits.size()+2);
if (!b.negative && !a.negative) {
if (a>b) {
sub(res,a,b);
} else {
sub(res,b,a);
res.negative=true;
}
} else if (b.negative && a.negative) {
if (b>a) {
sub(res,b,a);
} else {
sub(res,a,b);
res.negative=true;
}
} else if (b.negative) {
add(res,a,b);
} else {
add(res,a,b);
res.negative=true;
}
return res;
}
|