c++大数加减法的实现

输入

第一行,输入一个正整数 T (1<=T<=30)

然后有T行,每行两个整数 a 和 b


输出

对于每对 a和b,输出 a+b 的结果

样例输入

4
1 2
-5 3
1 -1
-1111111111111 2222222222222

样例输出

3
-2
0
1111111111111

题目来源



源代码:

#include 
#include 
using namespace std;
int str2num(string str,int *a)
{
	int len,len1,i;
	if(str[0]!='-')
{
	len=str.length(); 
	len1=0;
	for(i=len-1;i>=0;i--) 
	{
	a[len1++]=str[i]-'0';
	}
} 
else
{
	len=str.length(); 
	len1=0;
	for(i=len-1;i>=1;i--) 
	{
	a[len1++]=str[i]-'0';
	}
}
	return len1;
}
void subtract(int *a,int *b,int len,string str1)
{
	int index;
	int i;
	for(i=0;i=b[i])
				a[i]-=b[i];
			else
			{
				a[i]=a[i]+10-b[i];
				a[i+1]-=1;
			}
			a[len-1]-=b[len-1];
			
			index=len-1;//
			while(a[index]==0&&len>0)index--;//
			if(str1[0]=='-')
				cout<<'-';
			for(i=index;i>=0;i--) 
				cout<>T;
while(T--)
{
memset(a,0,sizeof(a)); 
memset(b,0,sizeof(b)); 
cin>>str1>>str2; 
len1=str2num(str1,a);
len2=str2num(str2,b);
if(str1[0]!='-'&&str2[0]!='-'||str1[0]=='-'&&str2[0]=='-')
{
	len=(len1>len2?len1:len2); 
	for(i=0;i0)index--;//

	if(str1[0]=='-'&&str2[0]=='-')
		cout<<'-';
	
	for(i=index;i>=0;i--) 
	cout<len2?len1:len2); 
	int flag=3;
	for(i=len-1;i>=0;i--)
	{
		if(a[i]>b[i])
		{
		flag=1;
		break;
		}
		else if(a[i]


你可能感兴趣的:(c++)