11.用链表模拟大整数加法运算

本文介绍如何利用链表数据结构模拟大整数的加法运算,通过递归方式从前向后进行计算,例如999 + 1 的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例如:9->9->9->NULL

+                      1->NULL

      1->0->0->0->NULL


思路:

使用递归,能够实现从前往后计算。


// LinkTable.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

//链表的结构体
struct node
{
	char val;
	node * next;
};

//建立链表
struct node * create( string & str_link )
{
	int len = str_link.length();

	struct node * phead = new node();     //带有表头的链表,表头中不存储任何元素
	struct node * preNode = phead;
	for( int i=0; i<len; i++ )
	{
		struct node * pNode = new node();
		pNode->val = str_link[i];
		pNode->next = NULL;
		preNode->next = pNode;
		preNode = pNode;
	}
	return phead;
}

//输出链表
void out_link( struct node * phead )
{
	if( phead == NULL )
		return;
	struct node * pNode = phead->next;
	while( pNode )
	{
		cout <<pNode->val;
		pNode = pNode->next;
	}
	cout << endl;
}

//求无表头链表的长度
//返回-1为链表不存在
int link_length( struct node* pNode )
{
	if(!pNode) return -1;

	int len=0;
	while( pNode )
	{
		pNode = pNode->next;
		len++;
	}
	return len;
}


//大数相加递归算法
//pNode1, pNode2为两个中间运算结点,但不是头结点
struct node * add( struct node * pNode1, struct node * pNode2, int & carry )
{
	if( !pNode1  ) return pNode2;
	if( !pNode2  ) return pNode1;

	//为了参数简洁,这里增大了计算量,可以将链表长度作为参数传进来
	int len1 = link_length( pNode1 );
	int len2 = link_length( pNode2 );


	if( len1 == len2 )
	{
		if( len1==1 )             //递归终止条件
		{
			struct node * pNode = new node();
			int sum = (pNode1->val - '0' ) + ( pNode2->val -'0');
			carry = sum/10;
			pNode->val = sum%10 + '0';
			pNode->next = NULL;
			return pNode;
		}
		else
		{
			int carry_cur=0;
			struct node * pNode = new node();
			struct node * pNext = add( pNode1->next, pNode2->next, carry_cur );
			int sum = (pNode1->val - '0' ) + ( pNode2->val -'0') + carry_cur;
			carry = sum/10;
			pNode->val = sum%10 + '0';
			pNode->next = pNext;
			return pNode;
		}
	}

	if( len1>len2 )
	{
		int carry_cur=0;
			struct node * pNode = new node();
			struct node * pNext = add( pNode1->next, pNode2, carry_cur );
			int sum = (pNode1->val - '0' ) + carry_cur;
			carry = sum/10;
			pNode->val = sum%10 + '0';
			pNode->next = pNext;
			return pNode;
	}

	if( len1<len2 )
	{
		int carry_cur=0;
			struct node * pNode = new node();
			struct node * pNext = add( pNode1, pNode2->next, carry_cur );
			int sum = (pNode2->val - '0' ) + carry_cur;
			carry = sum/10;
			pNode->val = sum%10 + '0';
			pNode->next = pNext;
			return pNode;
	}

	return NULL;
}

struct node * add( struct node * phead1, struct node * phead2 )
{
	if( !phead1 || !phead1->next ) return phead2;
	if( !phead2 || !phead2->next ) return phead1;

	int carry = 0;
	struct node * pNode = add( phead1->next, phead2->next, carry );

	if( carry > 0 )                     //有进位,则需要多一个结点
	{
		struct node * pCarry = new node();
		pCarry->val = '0' + carry;
		pCarry->next = pNode;
		pNode = pCarry;
	}

	struct node * phead = new node();
	phead->next = pNode;
	return phead;
}

void test()
{
	string str;
	cout << "Input the first link:"<<endl;
	cin >> str;
	struct node *phead1 = create( str );
	
	cout << "Input the second link:"<<endl;
	cin >> str;
	struct node *phead2 = create( str );

	struct node * phead = add( phead1, phead2);
	
	cout<< "The result is:" <<endl;
	out_link( phead );

}

int _tmain(int argc, _TCHAR* argv[])
{
	test();
	return 0;
}


1.问题描述 密码学分为两类密码:对称密码和非对称密码。对称密码主要用于数据的加/解密,而非对称密码则主要用于认证、数字签名等场合。非对称密码在加密和解密时,是把加密的数据当作一个大的正整数来处理,这样就涉及到大整数的加、减、乘、除和指数运算等,同时,还需要对大整数进行输出。请采用相应的数据结构实现大整数的加、减、乘、除和指数运算,以及大整数的输入和输出。 2.实验基本要求 要求采用链表来实现大整数的存储和运算,不允许使用标准模板类的链表类(list)和函数。同时要求可以从键盘输入大整数,也可以文件输入大整数大整数可以输出至显示器,也可以输出至文件。大整数的存储、运算和显示,可以同时支持二进制和十进制,但至少要支持十进制。大整数输出显示时,必须能清楚地表达出整数的位数。测试时,各种情况都需要测试,并附上测试截图;要求测试例子要比较详尽,各种极限情况也要考虑到,测试的输出信息要详细易懂,表明各个功能的执行正确; 1. 要求大整数的长度可以不受限制,即大整数的十进制位数不受限制,可以为十几位的整数,也可以为500多位的整数,甚至更长;大整数运算和显示时,只需要考虑正的大整数。如果可能的话,请以秒为单位显示每次大整数运算的时间; 2. 要求采用类的设计思路,不允许出现类以外的函数定义,但允许友元函数。主函数中只能出现类的成员函数的调用,不允许出现对其它函数的调用。 3. 要求采用多文件方式:.h文件存储类的声明,.cpp文件存储类的实现,主函数main存储在另外一个单独的cpp文件中。如果采用类模板,则类的声明和实现都放在.h文件中。 4. 不强制要求采用类模板,也不要求采用可视化窗口;要求源程序中有相应注释; 5. 要求采用Visual C++ 6.0及以上版本进行调试; 3.实现提示 1. 大整数的加减运算可以分解为普通整数运算来实现;而大整数的乘、除和指数运算,可以分解为大整数的加减运算。 2. 大整数的加、减、乘、除和指数运算,一般是在求两大整数在取余操作下的加、减、乘、除和指数运算,即分别求 (a +b) mod n, (a - b) mod n, (a * b) mod n, (a / b) mod n 和(a ^ b) mod n。其中a ^ b 是求a的b次方,而n称之为模数。说明:取余操作(即mod操作)是计算相除之后所得的余数,不同于除法运算的是,取余操作得到的是余数,而不是除数。如7 mod 5 = 2。模数n的设定,可以为2m 或10m,m允许每次计算时从键盘输入。模数n的取值一般为2512(相当于十进制150位左右),21024(相当于十进制200~300位),22048(相当于十进制300~500位)。为了测试,模数n也可以为2256, 2128等值。 3. 需要设计主要类有:链表类和大整数类。链表类用于处理链表的相关操作,包括缺省构造函数、拷贝构造函数、赋值函数、析构函数、链表的创建、插入、删除和显示等;而大整数类则用于处理大整数的各种运算和显示等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值