-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
sumlarge2.c
69 lines (68 loc) · 1.33 KB
/
sumlarge2.c
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define swap(x,y) int t=x;x=y;y=t;
void mystrrev(char*num)
{
for(int i=0;i<strlen(num)/2;i++)
{
swap(num[i],num[strlen(num)-1-i]);
}
}
void strswap(char**num1,char**num2)
{
char*temp=*num1;
*num1=*num2;
*num2=temp;
}
void findsum(char*num1,char*num2,int size1,int size2)
{
int carry=0;
char*result=malloc(255*sizeof(char));
for(int i=0;i<size1;i++)
{
carry=carry+(num1[i]-'0')+(num2[i]-'0');
result[i]=carry%10;
carry=carry/10;
}
for(int i=size1;i<size2;i++)
{
carry=carry+(num2[i]-'0');
result[i]=carry%10;
carry=carry/10;
}
if(carry)
{
result[size2]=carry;
}
if(result[size2])
{
for(int i=size2;i>=0;i--)
{
printf("%d",result[i]);
}
}
else{
for(int i=size2-1;i>=0;i--)
{
printf("%d",result[i]);
}
}
}
int main(void)
{
char*num1=malloc(255*sizeof(char));
char*num2=malloc(255*sizeof(char));
scanf("%s %s",num1,num2);
if (strlen(num1)>strlen(num2))
{
strswap(&num1,&num2);
}
int size1=strlen(num1),size2=strlen(num2);
mystrrev(num1);
mystrrev(num2);
findsum(num1,num2,size1,size2);
free(num1);
free(num2);
return 0;
}