-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigNum_Add.cpp
More file actions
49 lines (48 loc) · 930 Bytes
/
BigNum_Add.cpp
File metadata and controls
49 lines (48 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#define MAX_LEN 200
unsigned an1[MAX_LEN+10];
unsigned an2[MAX_LEN+10];
unsigned aResult[MAX_LEN*2+10];
char szLine1[MAX_LEN+10];
char szLine2[MAX_LEN+10];
int main()
{
gets(szLine1);
gets(szLine2);
int i,j;
int nLen1=strlen(szLine1);
memset(an1,0,sizeof(an1));
memset(an2,0,sizeof(an2));
memset(aResult,0,sizeof(aResult));
j=0;
for(i=nLen1-1;i>=0;i--)
an1[j++]=szLine1[i]-'0';
int nLen2=strlen(szLine2);
j=0;
for(i=nLen2-1;i>=0;i--)
an2[j++]=szLine2[i]-'0';
for(i=0;i<nLen2;i++)
{
for(j=0;j<nLen1;j++)
aResult[i+j]+=an2[i]*an1[j];
}
for(i=0;i<MAX_LEN*2;i++)
{
if(aResult[i]>=10){
aResult[i+1]+=aResult[i]/10;
aResult[i] %=10;
}
}
bool bStartOutput=false;
for(i=MAX_LEN*2;i>=0;i--)
if(bStartOutput)
printf("%d",aResult[i]);
else if(aResult[i]){
printf("%d",aResult[i]);
bStartOutput=true;
}
if(!bStartOutput)
printf("0");
return 0;
}