6.15奇特大数加法

代码如下:

#include <stdio.h>
#include <string.h>

int buf[6]={0,2,3,5,7,11};

void Swap(char &a,char &b)
{
	char c=a;
	a=b;
	b=c;
}
void ReverseString(char *s)
{
	int i=0;
	int j=strlen(s)-1;
	while (i<j)
	{
		Swap(s[i],s[j]);
		++i;--j;
	}
}
void Add(char *A,char *B,char *C)
{
	char AA[100];
	char BB[100];
	char CC[100];
	strcpy(AA,A);
	strcpy(BB,B);
	ReverseString(AA);
	ReverseString(BB);
	int carry=0;
	int i;
	int step = 1;
	int lenCC=0;
	for (i=0;i<strlen(AA) && i<strlen(BB);++i)
	{
		if (AA[i]==',') continue;
		int tmp=carry;
		if(AA[i]=='A') tmp+=10;
		else tmp+=(AA[i]-'0');
		if(BB[i]=='A') tmp+=10;
		else tmp+=(BB[i]-'0');
		carry = tmp/buf[step];
		int tmp2 = tmp%buf[step];
		if(tmp2>=10) CC[lenCC++]='A';
		else CC[lenCC++]=tmp2+'0';

		step++;
		
	}
	while (i<strlen(AA))
	{
		if (AA[i]==',') 
		{
			++i;
			continue;
		}
		int tmp=carry;
		if(AA[i]=='A') tmp+=10;
		else tmp+=(AA[i]-'0');
		carry = tmp/buf[step];
		int tmp2 = tmp%buf[step];
		if(tmp2>=10) CC[lenCC++]='A';
		else CC[lenCC++]=tmp2+'0';

		step++;
		++i;

	}
	while (i<strlen(BB))
	{
		if (BB[i]==',') 
		{
			++i;
			continue;
		}
		int tmp=carry;
		if(BB[i]=='A') tmp+=10;
		else tmp+=(BB[i]-'0');

		carry = tmp/buf[step];

		int tmp2 = tmp%buf[step];
		if(tmp2>=10) CC[lenCC++]='A';
		else CC[lenCC++]=tmp2+'0';

		step++;
		++i;
	}
	if (carry)
	{
		if (carry>=10)
		{
			CC[lenCC++]='A';
		} 
		else
		{
			CC[lenCC++]=carry+'0';
		}
	}
	CC[lenCC]=0;
	ReverseString(CC);
	//printf("%s\n",CC);

	int lenC=0;
	bool first=true;
	for (int i =0;i<strlen(CC);++i)
	{
		if(first) 
		{
			C[lenC++] = CC[i];
			first =false;
		}
		else
		{
			C[lenC++] = ',';
			C[lenC++] = CC[i];
		}
	}
	C[lenC]=0;

}

int main()
{
	freopen("in.txt","r",stdin);
	char s[20];
	char s1[20];
	char s2[20];
	char s3[20];
	//scanf("%s+%s",s1,s2);
	scanf("%s",s);
	int pos=0;
	for (int i=0;i<strlen(s);++i)
	{
		if(s[i]=='+') 
		{
			pos = i;
			break;
		}
	}
	for (int i=0;i<pos;++i)
	{
		s1[i] = s[i];
	}
	s1[pos] = 0;
	for (int i=pos+1;i<strlen(s);++i)
	{
		s2[i-pos-1] = s[i];
	}
	s2[strlen(s)-pos-1] = 0;

	Add(s1,s2,s3);

	printf("%s\n",s3);


	return 0;

}


你可能感兴趣的:(ACM)