数据结构,超出范围的极大整数相加

#include<iostream>
using namespace std;
typedef struct node
{
	int data;
	node* next;
}node;
class Operator
{
public:
	void Establish(node *&head,char a[], int n)//establishment of linked list(链表的建立)
	{
		//head insertion(用头插法)
		head = new node;
		head->next = NULL;
		
		for (int i = 0; i < n; ++i)
		{
			node* temp = new node;//temporary node is used to build linked list(临时结点用于链表的建立)
			temp->data = a[i] - '0';
			temp->next=head->next;
			head->next = temp;
		}
	}
	//because the insertion uses the head insertion method to insert ,resulting in the position of last bit ,so it is necessary to use the head insertion method to re insert it
	//因为用头插法插入,导致个位在最后一位的位置,所以要用头插法对其进行重新插入)

	void Overturn(node *&head)//used to filp linked list(用于链表的翻转)
	{
		//first ,define two pointers ,p,q.p to the first node ,q to the next node of p
		//首先定义两个结点p,q,p指向首结点,q指向p的下一个结点
		//make the data pointer to by the head empty and reinsert it;
		//令head指向的数据为空,对其进行重新插入
		node* p = head->next;
		node* q;
		head->next = NULL;
	
		while (p)
		{
			q = p->next;
			p->next=head->next;
			head->next = p;
			p = q;
		}
	}
	void Output(node*& head)
	{
		for (node* p = head->next; p; p = p->next)
			cout << p->data;
		cout << endl;
	}
	void Add(node*& L1, node*& L2, node*& L3)
	{
		L3 = new node;
		L3->next = NULL;
		node* p = L1->next;
		node* q = L2->next;
		int carry = 0;//when the number of added digits exceeds 10,one bit  forward is achieved(相加位数超过十时,实现向前移一位)
		while (p && q)
		{
			node* temp = new node;
			temp->data = (p->data + q->data + carry) % 10;
			carry = (p->data + q->data + carry) / 10;
			temp->next=L3->next;
			L3->next = temp;
			p = p->next;
			q = q->next;
		}
		//when jumping of the loop,only one linked list is empty ,copy the data of the non empty linked list
		//当跳出循环只有一条链表为空时,将不为空的链表复制下来
		if (p)q = p;//in this way ,we don't need to discuss which p or q is empty separately(这样就不用分开讨论p,q哪个为空)
		while (q)
		{
			node* temp = new node;
			temp->data = (q->data + carry) % 10;
			carry = (q->data + carry) / 10;
			temp->next = L3->next;
			L3->next = temp;
			q = q->next;
		}
		if (carry)
		{
			node* temp = new node;
			temp->data = carry;
			temp->next = L3->next;
			L3->next = temp;
		}
	}
};
int main()
{
	char a[10] = "987231556";
	char b[8] = "9854430";
	node* L1;
	node* L2;
	node* L3;
	Operator o;
	o.Establish(L1, a, 9);
	
	o.Establish(L2, b, 7);
	
	o.Add(L1, L2, L3);
	o.Overturn(L1);
	o.Overturn(L2);
	cout << "L1 ";

	o.Output(L1);
	cout<<endl << "L2 ";
	o.Output(L2);
	
	cout << endl << "相加得L3"<<endl;
	
	o.Output(L3);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、设计目的 本课程设计旨在通过实现一个大整数计算器,帮助学生深入理解数据结构的相关知识,掌握大整数的存储、运算等基本操作,提高编程能力。 二、设计内容 1.需求分析 大整数计算器需要支持以下功能: (1)大整数的输入输出 (2)大整数的加、减、乘、除运算 (3)大整数的比较大小 (4)大整数的求绝对值 (5)大整数的取模运算 2.数据结构设计 (1)大整数的存储 由于大整数可能会超出计算机所能表示的范围,因此需要使用链表来存储大整数。每个节点存储一个位数,节点之间通过指针连接。 (2)大整数的加、减、乘、除运算 加法:从低位到高位分别相加,若有进位则向高位一位加1。 减法:从低位到高位分别相减,若有借位则向高位一位减1。 乘法:将一个大数分解成若干个数字,每个数字与另一个大数相乘,然后将结果相加。 除法:将被除数和除数转换为整数,然后进行长除法运算。 (3)大整数的比较大小 从高位到低位比较每一位,若某一位不同则可以确定大小关系。 (4)大整数的求绝对值 将负数取反。 (5)大整数的取模运算 将被除数除以除数,求出余数。 3.算法设计 (1)大整数的加法 1.从低位到高位分别相加,若有进位则向高位一位加1; 2.若两数位数不等,则将短数的高位补0,使两数位数相同; 3.若最高位有进位,则需增加一位,将进位加在最高位。 (2)大整数的减法 1.从低位到高位分别相减,若有借位则向高位一位减1; 2.若被减数小于减数,则需向高位借位; 3.若减数的最高位为0,则需去除前导0。 (3)大整数的乘法 1.将一个大数分解成若干个数字,每个数字与另一个大数相乘,然后将结果相加; 2.从低位到高位依次计算,每次计算结果存储在一个临时变量中; 3.每次计算结束后,将结果加到最终结果中。 (4)大整数的除法 1.将被除数和除数转换为整数,然后进行长除法运算; 2.从高位到低位依次计算,将计算结果存储在一个临时变量中; 3.每次计算结束后,将结果加到最终结果中。 三、实现方法 1.使用C++语言实现。 2.使用链表存储大整数,每个节点存储一个位数,节点之间通过指针连接。 3.实现大整数的加、减、乘、除运算,比较大小和求绝对值等基本操作。 4.使用测试用例验证程序的正确性和鲁棒性。 四、总结 通过本课程设计,我深入理解了数据结构的相关知识,掌握了大整数的存储、运算等基本操作,提高了编程能力。同时,也感受到了编写高效、鲁棒的程序的重要性,这对我今后的学习和工作都有很大的帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值