新手学编程,有没有哪位前辈有C++代码,关于双向链表的实现两个大数相加?

小狼号角 发布于 2016/05/12 08:07
阅读 413
收藏 0
c++

第1部分:

 设计并实现了代表一个双向链表一类。这个类必须有以下要求:

 1.链表和节点必须被实现为C ++模板

1.列表必须是通用的 - 它不应该执行算术/逻辑功能。

2.它必须包括一个析构函数,拷贝构造函数和重载运算符=

3.它必须包括方法中的前部和在列表的背面插入

4.它必须包括一个方法以返回列表的长度

5.它必须提供一个基于迭代器的接口,用于用户从前向后

6.它必须包括用于从后到前的用户的基于迭代器的接口

 第2部分:

 类LargeInt的实施将使用动态物理结构来存储的整数的个别数字,并且将提供一种能够对整数进行一些基本的I / O和算术运算。

具体地,类应包括:

 

1)默认的构造函数

2)运算符函数重载运算符+

3)操作符函数重载运算符= =

4)操作函数重载运算符<<

5)操作符函数重载运算符>>

 注1:自LargeInt类不包含指针,没有必要为一个拷贝构造函数或析构函数。

注2:您可以假定巨大的整数只能是正数(或0)。

注3:你的巨大整数类型的实现必须封装为一个C ++类,聚集了巨大的整数值的内部表示一个列表对象。巨大的整数类型不是列表中,也没有任何意义为它可以从使用继承列表而得。

#include "stdafx.h"

#include <iostream>

using namespace std;

template <class T>

struct Listnode

{

       T data;

       Listnode* Former;

       Listnode* next;

};

 

 

template <class T>

class  DoubleLists

{

private:

       Listnode<T>* head;

       Listnode<T>* loc;

       Listnode<T>* end;

       int len;

public:

       DoubleLists()

       {

             head = NULL;

             loc = head;

             end = NULL;

             len = 0;

       }

 

       ~DoubleLists()

       {

             while (loc->Former != NULL)

             {

                    delete loc;

                    GetNext();

             }

             loc = NULL;

             head = NULL;

             end = NULL;

       }

 

       DoubleLists& operator=(DoubleLists& datasouce);

       void AddDataInEnd(T data);

       void AddDataInStart(T data);

       bool IsEnpty();

       int GenListLen();

       Listnode<T>* GetNext();

       Listnode<T>* GetFormer();

       Listnode<T>* GetFirst();

       Listnode<T>* GetLoc();

       void PrintList();

};

 

template <class T>

void DoubleLists<T>::PrintList()

{

       while (this->loc->next != NULL)

       {

             cout << (this->loc->data) << endl;

             this->GetNext();

       }

}

 

template <class T>

DoubleLists<T>& DoubleLists<T>::operator=(DoubleLists<T>& datasouce)

{

       this->head = datasouce.head;

       this->end = datasouce.end;

       this->len = datasouce.len;

       this->loc = this->head;

       return this;

}

 

 

template <class T>

bool DoubleLists<T>::IsEnpty()

{

       if (this->head == NULL)

             return true;

       else

             return false;

}

 

template <class T>

Listnode<T>* DoubleLists<T>::GetFormer()

{

       if (this->loc->Former != NULL)

       {

             this->loc = this->loc->Former;

             return this->loc;

       }

       else

             return NULL;

}

 

template <class T>

Listnode<T>* DoubleLists<T>::GetNext()

{

       if (this->loc->next != NULL)

       {

             this->loc = this->loc->next;

             return this->loc;

       }

       else

             return NULL;

}

 

template <class T>

Listnode<T>* DoubleLists<T>::GetFirst()

{

       if (this->head == NULL)

       {

             return NULL;

       }

       else

       {

             this->loc = this->head;

             return this->head;

       }

}

 

template <class T>

Listnode<T>* DoubleLists<T>::GetLoc()

{

       return this->loc;

}

 

template <class T>

int DoubleLists<T>::GenListLen()

{

       return this->len;

}

 

template <class T>

void DoubleLists<T>::AddDataInStart(T indata)

{

       Listnode<T> *temp;

       if (this->head == NULL)

       {

             temp = new Listnode<T>;

             temp->data = indata;

             temp->Former = NULL;

             temp->next = NULL;

             this->end = temp;

             this->head = temp;

             this->loc = temp;

             this->len = 1;

       }

       else

       {

             temp = new Listnode<T>;

             temp->data = indata;

             temp->Former = NULL;

             temp->next = this->head;

             this->head = temp;

             this->len++;

       }

}

 

template <class T>

void DoubleLists<T>::AddDataInEnd(T indata)

{

       static Listnode<T> *temp;

       if (this->head == NULL)

       {

             temp = new Listnode<T>;

             temp->data = indata;

             temp->Former = NULL;

             temp->next = NULL;

             this->end = temp;

             this->head = temp;

             this->loc = temp;

             this->len = 1;

       }

       else

       {

             temp = new Listnode<T>;

             temp->data = indata;

             temp->Former = this->end;

             this->end->next = temp;

             temp->next = NULL;

             this->end = temp;

             this->len++;

       }

}

 

int main()

{

       int i = 0;

       DoubleLists<char> clist;

       char *loctest = "HELLO WORLD";

       while (loctest[i] != '\0')

       {

             clist.AddDataInEnd(loctest[i]);

             i++;

       }

       clist.PrintList();

       while (1)

             getchar();

}

 

 

class LargeInt

{

private:

       char datain[200];

       int databit[200];

       int len;

       int overflow;

       LargeInt(char *x, int inlen)

       {

             int i = 0;

             int j = inlen - 1;

             len = inlen;

             overflow = 0;

             for (i = 0; i < 200; i++)

             {

                    databit[i] = 0;

                    datain[i] = '\0';

             }

             for (i = 0; i < len; i++)

             {

                    datain[i] = x[j];

                    j--;

             }

       }

       ~LargeInt();

       void GetDataBit()

       {

             int i = 0;

             for (i; i < len; i++)

                    databit[i] = datain[i] - 48;

       }

public:

       LargeInt& operator+(LargeInt& data);

       bool operator==(LargeInt& data);

       LargeInt& operator>>(int x);

       LargeInt& operator<<(int x);

};

 

bool LargeInt::operator==(LargeInt& data)

{

       if (this->len != data.len)

             return false;

       else

       {

             for (int i = 0; i < 200; i++)

             {

                    if (this->databit[i] == data.databit[i])

                           continue;

                    else

                           return false;

             }

             return true;

       }

}

 

LargeInt& LargeInt::operator+(LargeInt& data)

{

       LargeInt ans("0", 0);

       int addlen;

       if (this->len > data.len)

             addlen = this->len;

       else

             addlen = data.len;

       for (int i = 0; i < addlen; i--)

       {

             ans.databit[i] = (data.databit[i] + this->databit[i] + ans.overflow) % 10;

             if ((data.databit[i] + this->databit[i] + ans.overflow) >= 10)

                    ans.overflow = 1;

       }

       ans.len = addlen;

       for (int i = 0; i < addlen; i++)

       {

             ans.datain[i] = ans.databit[i] + 48;

       }

       return ans;

}

 

template<typename A>

int main()

{

       A data;

       DoubleLists<class T> object;

       operator = (object);

       while (object.IsEnpty() == true)

       {

             cin >> data;

             object.AddDataInStart(data);

             object.GetFirst();

             object.GetLoc();

             object.GenListLen();

             object.AddDataInEnd(data);

 

       }

       object.PrintList();

       system("pause");

       return 0;

 

}

加载中
返回顶部
顶部