class Biginteger {
Node *first;
int length;
public:
Biginteger() {
first = new Node(0);
}
Node *GetHead() {
return first;
}
friend ostream&operator<<(ostream &, const Biginteger&);
friend istream&operator>>(istream &, Biginteger&);
Biginteger operator+(int);
Biginteger operator*(Biginteger &);
Biginteger operator*(int);
bool operator==(Biginteger&);
Biginteger Cal_Mul(Biginteger&);
Biginteger Cal_Mul(int);
void Delete();
};
2)类实现(只有关键部分)
Biginteger Biginteger::operator*(int R)
{
Biginteger C;
Node *pc = C.first, *pa = GetHead()->link;
pc->insertAfter(0);
int t = R % 10;
int temp;
int n;
int num = 0;
while (R) {
pa = GetHead()->link;
pc = C.GetHead();
n = 0;
while (n < num) {
pc = pc->link;
n++;
}
num++;
while (pa != NULL) {
temp = t * pa->value;
if (pc->link == NULL) {
pc->insertAfter(temp % 10);
}
else {
pc->link->value += (temp % 10);
}
pc = pc->link;
if (pc->value >= 10) {
if (pc->link == NULL) {
pc->insertAfter((pc->value) / 10);
}
else {
pc->link->value += ((pc->value) / 10);
}
pc->value = (pc->value) % 10;
}
if (temp >= 10) {
if (pc->link == NULL) {
pc->insertAfter(temp / 10);
}
else {
pc->link->value += (temp / 10);
}
if (pc->link->value >= 10) {
if (pc->link->link == NULL) {
pc->link->insertAfter((pc->link->value) / 10);
}
else {
pc->link->link->value += ((pc->link->value) / 10);
}
pc->link->value = (pc->link->value) % 10;
}
}
pa = pa->link;
}
R /= 10;
t = R % 10;
if (pc->link == NULL && pa != NULL)
pc = pc->insertAfter(0);
else pc = pc->link;
}
return C;
}
3)阶乘实现
Biginteger Biginteger::Cal_Mul(int R) {
Biginteger ans;
ans.first->insertAfter(1);
if (R == 1) {
return ans;
}
for (int i(2); i <= R; i++) {
ans = ans * i;
}
return ans;
}
后记
依旧没有注释,提供一种思路吧,其实我觉得我写的很糙,太长了,而且一个结点只存了一个数字,有点浪费,不过亲测可用,思路差不多的可以参考一下。
2018/11/14 23: 31:14