既然楼主要求用C语言,那就用经典的C指针吧
#include
#include
#include
char* BigIntAdd(char* x,char* y,char* z)
{
int lenx=strlen(x);
int leny=strlen(y);
char *pmax=x,*pmin=y,*pz=z,*p1,*p2,t;
int i,lenmax=lenx,lenmin=leny;
if (lenx < leny)
{
pmax=y;
pmin=x;
lenmax=leny;
lenmin=lenx;
}
p1=pmax+lenmax-1;
p2=pmin+lenmin-1;
while(p2>=pmin)
{
*pz = *p1 + *p2 -'0';
if (pz>z && *(pz-1)>='0'+10)
{
*(pz-1)-=10;
*pz+=1;
}
p1--;p2--;pz++;
}
for(i=0;i { *pz=*p1; if (pz>z && *(pz-1)>='0'+10) { *(pz-1)-=10; *pz+=1; } pz++;p1--; } pz--; if (*pz>='0'+10) { *(pz++)-=10; *pz='1'; } for(p1=z;p1 { t=*p1;*p1=*pz;*pz=t; } return z; } void main() { system("color 1e"); char x[1024*10]={0},y[1024]={0},z[1024]={0}; printf("Enter Large Integers 1:\n"); scanf("%s",x); printf("Enter Large Integers 2:\n"); scanf("%s",y); BigIntAdd(x,y,z); printf("\n\nBigInt Add:\n%s\n\t+\n%s\n\t=\n%s\n",x,y,z); system("pause"); } 主要想法是,判断两个数字的长短,找出最短的数字,从个位数开始与另一个数的相应位进行相加(注意判断向上进位),将结果逐个保存在结果字符串中。最后将长的那个数字 剩下的部分直接 放在结果字符串中,然后将结果字符串反转,得到结果
大整数不应该用double做为输入,应该用字符串,代码如下:
#include
#include
#include
using namespace std;
const int M = 4;
const int N = 99;
void Sort(int a[], string z)
{
int i = 0;
int zlen = 0;
while (z.size() > 0) {
zlen = z.size();
if (zlen <= 4) {
stringstream ss(z);
ss>>a[i++];
z = "";
} else {
string tmp = z.substr(zlen-M);
stringstream ss(tmp);
ss>>a[i++];
z = z.substr(0, zlen - M);
}
}
}
int main()
{
string x, y;
cin >> x >>y;
int a[N] = {0}, b[N] = {0}, c[N] = {0};
int carry = 0;
int tmp = 0;
int num = 0;
Sort(a, x);
Sort(b, y);
for (int i = 0; i < N; ++i) {
tmp = a[i] + b[i];
c[i] = (tmp + carry)%10000;
carry = tmp/10000;
if (c[i] > 0) num = i;
}
for (int i = num; i >= 0; --i) {
cout << c[i];
}
cout<
}
#include
#include
#include
void add(char a[],char b[],char back[]){
int i,j,k,up,x,y,z,l;
char *c;
if (strlen(a)>strlen(b))
l=strlen(a)+2;
else
l=strlen(b)+2;
c=(char *)malloc(l*sizeof(char));
i=strlen(a)-1,j=strlen(b)-1;
k=0,up=0;
while(i>=0||j>=0){
if(i<0) x='0'; else x=a[i];
if(j<0) y='0'; else y=b[j];
z=x-'0'+y-'0';
if(up) z+=1;
if(z>9) {up=1;z%=10;}else up=0;
c[k++]=z+'0';
i--,j--;
}
if(up) c[k++]='1';
c[k]=i=0;
for(k-=1;k>=0;k--)back[i++]=c[k];
back[i]='\0';
}
void main(){
#define MAX 5000
char a[MAX],b[MAX],c[MAX+1];
for(;;)
{
printf("★★★★(to Exit,Enter 'e' or 'E')★★★★★");
printf("\n★★Enter two numbers with [enter]-key:★★\n>>");
scanf("%s",&a);
if(a[0]=='e'||a[0]=='E')return;
printf(">>");
scanf("%s",&b);
add(a,b,c),printf("%s\n",c);
}
}
#include
#include
#include
using namespace std;
int main()
{
string s1,s2;
int arr1[10000]={0},arr2[10000]={0},temp[10001]={0},i,j,in=0;//10000位够多了。。
cin>>s1>>s2;
for(string::size_type s1Len=0,j=s1.size()-1;s1Len
for(string::size_type s2Len=0,j=s2.size()-1;s2Len
int Len=s1.size()>s2.size()? s1.size():s2.size();//Len是两个数中较长的长度
for(i=0;i
temp[i]+=arr1[i]+arr2[i];
if(temp[i]>=10)
{
temp[i]-=10;//若该位相加大于10,自减10
temp[i+1]++;//高位加1
in=1;//进位为1
}
else in=0;
}
if(in)
Len++;
for(i=Len-1;i>=0;i--)
cout<
return 0;
}
#include
#include
#define N 1000
#define L 100
#define SIZE 100000
char input[SIZE];
long nums[3][L];
int main()
{
int i, j, k, n, s, flag = 0, len;
long temp;
char strtemp[6] = {0};
while(!flag)
{
puts("请输入第一个加数");
scanf("%s",input);
len = strlen(input);
for(i = 0,flag = 1; i < len; i++)
{
if(input[i] < '0' || input[i] > '9')
{
flag = 0;
}
}
}
for(i = len - 5,n = L - 1; i > 0; i -= 5)
{
strncpy( strtemp, &input[i],5);
nums[0][n--] = atoi(strtemp);
}
if(i < 0)
{
memset(strtemp,0,sizeof(strtemp));
strncpy( strtemp, &input[0],5 + i);
nums[0][n] = atoi(strtemp);
}
flag = 0;
while(!flag)
{
puts("请输入第二个加数");
scanf("%s",input);
len = strlen(input);
for(i = 0,flag = 1; i < len; i++)
{
if(input[i] < '0' || input[i] > '9')
{
flag = 0;
}
}
}
for(i = len - 5,n = L - 1; i > 0; i -= 5)
{
strncpy( strtemp, &input[i],5);
nums[1][n--] = atoi(strtemp);
}
if(i < 0)
{
memset(strtemp,0,sizeof(strtemp));
strncpy( strtemp, &input[0],5 + i);
nums[1][n] = atoi(strtemp);
}
for(i = 2,j = L - 1; j > 0; j--) //大数加法,和小学学的竖式一个道理
{
temp = nums[i][j] + nums[i - 1][j] + nums[i - 2][j];
nums[i][j] = temp % SIZE;
nums[i][j - 1] = temp / SIZE + nums[i][j - 1];
}
for(j = 0,s = 2,flag = 0; j < L; j++)
{
if(flag)
{
for(k = SIZE/10; k > 0; k/=10)
{
if(nums[s][j] / k > 0)
break;
else
putchar('0');
}
printf("%d",nums[s][j]);
}
else
{
if(0 != nums[s][j])
{
flag = 1;
printf("%d",nums[s][j]);
}
}
}
putchar(10);
return 0;
}