大数运算c语言链表,数据结构的一个问题:用链表实现大整数的加减乘除运算...

书中例子代码是直接用STL的list的,我自己写了一个带头结点的双向循环链表DouList,已经能实现大整数的加法了。现在就是搞不清乘法和除法的算法怎么写。

大家麻烦看一下我写的这几个类,然后帮忙写一下大整数乘法,除法的代码。或者可以提供点思路,3Q啊

//DouListNode.h 结点类

template

class DouListNode

{

T data;

DouListNode* link, * prior;

public:

DouListNode():link(NULL),prior(NULL){};

DouListNode(T value):link(NULL),prior(NULL),data(value){}

~DouListNode(){};

void SetLink(DouListNode* next);

void SetPrior(DouListNode* pre);

DouListNode* GetLink();

DouListNode* GetPrior();

T & GetData();

};

template

void DouListNode::SetLink(DouListNode* next)

{

link=next;

}

template

void DouListNode::SetPrior(DouListNode* pre)

{

prior=pre;

}

template

DouListNode* DouListNode::GetLink()

{

return link;//unhandled exception in XXXXX.exe  Access violation

}

template

DouListNode* DouListNode::GetPrior()

{

return prior;

}

template

T& DouListNode::GetData()

{

return data;

}

///

//DouList.h 双向循环链表类

#include "DouListNode.h"

template

class DouList

{

DouListNode* head,*tail,*cur;

public:

DouList();

~DouList(){};

bool AddTail(T value);

bool AddHead(T value);

void RemoveThis(bool direction);

void RemoveAll();

void SetBegin();

int GetCount();

void TowardCur();

void BackCur();

DouListNode* GetCur();

DouListNode* GetHead();

DouListNode* GetTail();

void InsertAfter(T value);

bool IsEmpty();

T GetNext();

T GetPrior();

};

template

DouList::DouList()

{

head=tail=new DouListNode;

cur=NULL;

head->SetLink(head);

head->SetPrior(tail);

}

template

bool DouList::AddTail(T value)

{

DouListNode* add=new DouListNode(value);

tail->SetLink(add);

add->SetPrior(tail);

tail=tail->GetLink();

tail->SetLink(head);

head->SetPrior(add);

if (tail != NULL)

{

return true;

}

else

{

return false;

}

}

template

bool DouList::AddHead(T value)

{

DouListNode* add=new DouListNode(value);

add->SetPrior(head);

add->SetLink(head->GetLink());

(head->GetLink())->SetPrior(add);

head->SetLink(add);

if (tail == head)

{

tail=head->GetLink();

}

if (add != NULL)

{

return true;

}

else

{

return false;

}

}

template

void DouList::RemoveThis(bool direction)

{

if (cur == head)

{

if (direction ==0 )

{

cur =cur->GetLink();

}

if (direction ==1 )

{

cur=cur->GetPrior();

}

}

DouListNode* preCur=NULL;

DouListNode* nextCur=NULL;

preCur=cur->GetPrior();

nextCur=cur->GetLink();

preCur->SetLink(nextCur);

nextCur->SetPrior(preCur);

if (direction==0)

{

cur=nextCur;

}

if (direction == 1)

{

cur =preCur;

}

}

template

void DouList::RemoveAll()

{

SetBegin();

int length=GetCount();

for(int i=0;i

{

RemoveThis(0);

}

cur=head;

}

template

void DouList::SetBegin()

{

cur=head;

}

template

int DouList::GetCount()

{

int num=0;

cur=head;//增加这个初始化

DouListNode* here=cur;

while (cur->GetLink() != here )

{

cur=cur->GetLink();

num++;

}

cur=cur->GetLink();

return num;

}

template

void DouList::TowardCur()

{

cur=cur->GetLink();

}

template

void DouList::BackCur()

{

cur=cur->GetPrior();

}

template

DouListNode* DouList::GetCur()

{

return cur;

}

template

DouListNode* DouList::GetHead()

{

return head;

}

template

DouListNode* DouList::GetTail()

{

return tail;

}

template

bool DouList::IsEmpty()

{

return head->GetLink()== head;

}

template

void DouList::InsertAfter(T value)

{

DouListNode* add=new DouListNode(value);

DouListNode* nextCur=cur->GetLink();

cur->SetLink(add);

add->SetLink(nextCur);

nextCur->SetPrior(add);

add->SetPrior(cur);

if (cur == tail)

{

tail=cur->GetLink();

}

}

template

T DouList::GetNext()

{

if (cur==head)

{

cur=cur->GetLink();

}

T num=cur->GetData();

cur=cur->GetLink();

return num;

}

template

T DouList::GetPrior()

{

if (cur==head)

{

cur=cur->GetPrior();

}

T num=cur->GetData();

cur=cur->GetPrior();

return num;

}

//

//BigInt.h 大整数类

#include 

#include        // setfill(), setw()

#include 

#include "DouList.h"

#ifndef BIGINT

#define BIGINT

const int DIGITS_PER_BLOCK = 3;

class BigInt

{

public:

void read(istream & in);

void display(ostream & out) ;

BigInt operator+(BigInt addend2);

BigInt operator-(BigInt addend2);

BigInt operator*(BigInt addend2);

BigInt operator/(BigInt addend2);

BigInt operator^(BigInt addend2);

private:

DouList myList;

};

inline istream & operator>>(istream & in, BigInt & number)

{

number.read(in);

return in;

}

inline ostream & operator<

{

number.display(out);

return out;

}

#endif

//BigInt.cpp

#include 

#include 

using namespace std;

#include "BigInt.h"

//--- Definition of read()

void BigInt::read(istream & in)

{

static bool instruct = true;

if (instruct)

{

cout <

"spaces.\nEnter a negative integer in last block to signal "

"the end of input.\n\n";

instruct = false;

}

short int block;

const short int MAX_BLOCK = (short) pow(10.0, DIGITS_PER_BLOCK) - 1;

for (;;)

{

in >> block;

if (block 

if (block > MAX_BLOCK)

cerr <

else

myList.AddTail(block);

}

}

//--- Definition of display()

void BigInt::display(ostream & out)

{

int blockCount = 0;

const int BLOCKS_PER_LINE = 20;   // number of blocks to display per line

//myList.SetBegin();

DouListNode* pt=myList.GetHead();

pt=pt->GetLink();

for (int i=0;i

{

out <

if (blockCount == 0)

out <

//if (myList.GetCur() != myList.GetTail() )

out <GetData();

blockCount++ ;

pt=pt->GetLink();

out <

if (blockCount > 0 && blockCount % BLOCKS_PER_LINE == 0)

out <

}

out <

out <GetData();//最后一个数字块后不输出逗号,

}

//--- Definition of operator+()

BigInt BigInt::operator+(BigInt addend2)

{

BigInt sum;

short int first,                  // a block of 1st addend (this object)

second,                 // a block of 2nd addend (addend2)

result,                 // a block in their sum

carry = 0;              // 两个数字块相加后的进位

DouListNode* pt1=myList.GetTail(),  //pt1指向当前myList的链尾结点

*pt2=addend2.myList.GetTail(); //pt2指向addend2的myList的链尾结点

while ( pt1 != myList.GetHead() || pt2 != addend2.myList.GetHead())

{

if (pt1 != myList.GetHead())

{

first=pt1->GetData();

pt1=pt1->GetPrior();  //pt1从链尾向链首移动

}

else

first=0;

if (pt2 != addend2.myList.GetHead())

{

second=pt2->GetData();

pt2=pt2->GetPrior();

}

else

second=0;

short int temp = first + second + carry;

result = temp % 1000;

carry = temp / 1000;

sum.myList.AddHead(result);

}

if (carry > 0)

sum.myList.AddHead(carry);

return sum;

}

大家帮忙写一下减法,乘法,除法等的算法吧,3Q3Q

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值