user-0 提问: C/C++语言求两个较大整数的和

回复:
user: user-1
content: 仅供参考：#include <stdio.h> #include <string.h> #define MAXLEN 1000 char a1[MAXLEN]; char a2[MAXLEN]; static int v1[MAXLEN]; static int v2[MAXLEN]; static int v3[MAXLEN]; int i,j,n,L,z; void main(void) {     scanf("%d",&n);     for (j=0;j<n;j++) {         scanf("%s%s",a1,a2);          L=strlen(a1);         for (i=0;i<L;i++) v1[i]=a1[L-1-i]-'0';          L=strlen(a2);         for (i=0;i<L;i++) v2[i]=a2[L-1-i]-'0';          for (i=0;i<MAXLEN;i++) v3[i]=v1[i]+v2[i];          for (i=0;i<MAXLEN;i++) {             if (v3[i]>=10) {                 v3[i+1]+=v3[i]/10;                 v3[i]=v3[i]%10;             }         }          printf("Case %d:\n", j+1);         printf("%s + %s = ", a1, a2);          z=0;         for (i=MAXLEN-1;i>=0;i--) {             if (z==0) {                 if (v3[i]!=0) {                     printf("%d",v3[i]);                     z=1;                 }             } else {                 printf("%d",v3[i]);             }         }         if (z==0) printf("0");          printf("\n");     } } //Sample Input //3 //0 0 //1 2 //112233445566778899 998877665544332211 // //Sample Output //Case 1: //0 + 0 = 0 //Case 2: //1 + 2 = 3 //Case 3: //112233445566778899 + 998877665544332211 = 1111111111111111110

user: user-2
content: 自己定义一个BigNumber类，重载几个需要的运算符即可，下面是一个简单的例子，仅提供思路，只实现了部分代码。  class BigNumber { public: 	BigNumber(int bytes=8);  	BigNumber(const BigNumber& num); 	BigNumber& operator=(const BigNumber& num); 	BigNumber operator+(const BigNumber& num); 	string ToString(); private: 	UInt32 m_Size; 	UInt8* m_pData; }; BigNumber::BigNumber(int bytes) { 	m_Size = bytes+1; //结果有可能进位 	m_pData= new UInt8[m_Size]; 	for(int i=0;i<=m_Size;i++){ 		m_pData[i]=0; 	} } BigNumber BigNumber::operator +(const BigNumber& num) { 	UInt16 sum=0; 	UInt8 low=0,high=0;		 	BigNumber res(num);	 	for(int i=0;i<m_Size;i++){ 		sum=this->m_pData[i]+num.m_pData[i]+high; 		low=sum&0xFF;	//低位作为该字节的计算结果 		high=sum&0xFF00;//高位要参加下一字节的计算 		res.m_pData[i]=low;  	} 	res.m_pData[m_Size]=high; // 最后要加上整体即最后一字节的进位值 	return res; }

user: user-3
content: 这样可好：  #include <stdio.h> #include <string.h> #define MAXLEN 256 char num1[MAXLEN]; char num2[MAXLEN]; int i, j, n, r; int L1, L2, L; char *sum; void reverse(char *s) { 	n = strlen(s); 	i = 0, j = n - 1; 	while(i < j) 	{ 		char c = s[i]; 		s[i] = s[j]; 		s[j] = c; 		i++, j--; 	} } int main(void) { 	r = n = 0; 	scanf("%256s", num1); 	scanf("%256s", num2); 	reverse(num1); 	reverse(num2); 	L1 = strlen(num1), L2 = strlen(num2); 	L = L1 > L2 ? L1 : L2; 	L += 2; 	sum = (char*)malloc(sizeof(char)*L); 	memset(sum, 0, L); 	L = L1 < L2 ? L1 : L2; 	for (i = 0; i < L; i++) 	{ 		n = num1[i] - '0' + num2[i] - '0'; 		sum[i] = (r + n) % 10 + '0'; 		r = (r + n) / 10; 	} 	if (L1 <= L2) 	{ 		while (i < L2) 		{ 			n = num2[i] - '0'; 			sum[i] = (r + n) % 10 + '0'; 			r = (r + n) / 10; 			i++; 		} 	} 	else 	{ 		while (i < L1) 		{ 			n = num1[i] - '0'; 			sum[i] = (r + n) % 10 + '0'; 			r = (r + n) / 10; 			i++; 		} 	} 	if (r) 		sum[i] = r + '0'; 	sum[i + 1] = 0; 	reverse(sum); 	printf("sum=%s\n", sum); 	free(sum); 	return 0; }

user: user-4
content: 表示没看懂楼主想要干什么

user: user-5
content: 用字符串实现加法吧

user: user-6
content: 不是使用字符串？

user: user-0
content: 速度啊  自己写了一个但是验证的时候出现错误了 快要崩溃了

user: user-7
content: 主要是把大的证书化整为零进行处理，相关代码可以问度娘