面试题之自创 实现两个整型大数(都是正数)的相加操作

本文介绍了一种使用字符串处理方法解决大整数相加问题的算法,详细阐述了思路、实验和代码实现,旨在解决传统整型数据类型无法满足的需求。

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

非常大的正整数相加求和

  • 真言

对于得不到的,很羡慕;对于得到的,很不满足。恋爱了,感觉真好,这也是一种动力。just fight for my life,for me,for her,for us and our family.

  • 引言

随着大数据时代的到来,现存的一些操作日渐满足不了我们的需求。我这篇文章只是针对非常大的正整数相加的题目,还没有写小数以及负数的等等,相继给出。

  • 思路

int,long,long long 现在已经满足不了我们的需求,如果给出 4564561261641561646146 + 564213

我们怎么办?我个人觉得用字符串可以解决,如果大家有更好的想法,可以留言,欢迎你哟。

我的思路:

把每个大数放在字符串里,然后去模拟数相加对每一位进行相加及进位操作。

  • 实验

  • 代码
test.cpp(更新过)
#include <iostream>
#include <string>
using namespace std;


// extra the class of string
class String:public string
{
public:


	// mode the add of int
	static string Add(string a,string b)
	{
		// exception of input
		if( a.empty() )
			return b;
		else if( b.empty() )
			return "0";
		if(!check_all_number(a) || !check_all_number(b))
		{
			return "exception of input add";
		}
		Standardization(a);
		Standardization(b);
		string::size_type i = a.size()-1 ,j = b.size()-1 , k = 0 ;
		string c = "";
		int jinwei = 0;
		while( i < a.size() && j < b.size() )
		{
			c = IntToChar( ( CharToNumber(a[i]) + CharToNumber(b[j]) + jinwei ) % 10 ) + c;
			jinwei = ( CharToNumber(a[i]) + CharToNumber(b[j]) + jinwei ) / 10;
			j--;i--;
		}
		while( j < b.size()  )
		{
			c =  IntToChar( ( CharToNumber(b[j]) + jinwei ) % 10 ) + c;
			jinwei = ( jinwei + CharToNumber(b[j]) ) / 10;	
			j--;
		}
		while( i < a.size() )
		{
			c =  IntToChar( ( CharToNumber(a[i]) + jinwei ) % 10 ) + c;
			jinwei = ( jinwei + CharToNumber(a[i]) ) / 10;	
			i--;
		}
		if( jinwei )
			c = IntToChar(  jinwei  ) + c;
		return c;
	}


	// make char to the int number
	static int CharToNumber(char c)
	{
		if( c >= '0' && c <= '9' )
			return int(c - '0');
		else 
		{
			cout<<"exception of input CharToNumber "<<endl;
			system("pause");
			return 0;
		}
	}


	// make int to the model char
	static char IntToChar(int i)
	{
		if( i >= 0 && i <= 9 )
		{
			return char(i+48);
		}
		else
		{
			cout<<i<<" exception of input IntToChar"<<endl;
			system("pause");
		}
	}


	// check whether the string is legal 
	static bool check_all_number(string a)
	{
		if(a.empty())
			return true ;
		string::size_type L = a.size(),i = 0;
		while( i < L )
		{
			if( a[i] < '0' || a[i] > '9')
				return false;
			i++; 
		}
		return true ;
	}


	// make a-b mode int a - b;
	static string Multiplies(string a,string b)
	{
		// exception of input
		if(!check_all_number(a) || !check_all_number(b))
			return "exception of input Multiplies";
		Standardization(a);
		Standardization(b);
		// particular string of input
		if(a.empty())
		{
			if(b.empty())
				return "0";
			else
				return "-"+b;
		}
		else if(b.empty())
		{
			return a;
		}


		// normal number
		string c = "";
		bool check = true ;
		if(Compare(a,b) == '=')
			return "0";
		else if(Compare(a,b) == '<')
		{
			c = a ;
			a = b ;
			b = c ;
			c = "";
			check = false ;
		}




		string::size_type i = a.size()-1, j = b.size()-1;
		int jiewei = 0,now;
		
		while(i < a.size() && j < b.size())
		{
			now = CharToNumber(a[i]) - CharToNumber(b[j]) - jiewei ;
			
			if( now < 0 )
			{
				jiewei = 1;
				now = 10 + now ;
			}
			else jiewei = 0;
			c = IntToChar(now)  + c ;
			i--;j--;
		}
		while(i < a.size())
		{
			now = CharToNumber(a[i]) - jiewei ;
			if( now < 0 )
			{
				jiewei = 1;
				now = 10 + now ;
			}
			else jiewei = 0;
			c = IntToChar( now )  + c ;
			i--;
		}
		Standardization(c);
		if(!check)
			c = '-' + c;
		
		return c; 
	}


	// compare string a and b
	static char Compare(string a,string b)
	{
		if(a.empty() || b.empty())
		{
			cout<<"error of imput compare";
			return 'e';
		}
		else
		{
			if(a.size() > b.size())
				return '>' ;
			else if(a.size() == b.size())
			{
				for(string::size_type i = 0;i < a.size(); i++)
				{
					if(a[i] > b[i])
						return '>';
					if(a[i] < b[i])
						return '<';
				}
				return '=';
			}
			
			return '<';
		}
	}


	static void Standardization(string &a)
	{
		if(!check_all_number(a))
		{
			cout<<a<<" exception of input Standardization"<<endl;


		}
		string::size_type i = 0;
		while(i < a.size())
		{
			if(a[i] != '0')
				break;
			i++;
		}
		a = a.substr(i,a.size());
	}
};

// main function
int main()
{
	string a ;
	string b ;
	cin>>a>>b;
	cout<<"a="<<a<<",b="<<b<<endl;
	cout<<String::Add(a,b)<<endl;
	system("pause");
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值