-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_array.c
More file actions
137 lines (109 loc) · 2.81 KB
/
Copy pathadd_array.c
File metadata and controls
137 lines (109 loc) · 2.81 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int add(int a[], int len1, int b[], int len2, int r[], int len3)
{
// p[]: keep the gregrouping digits
int *p = (int *) malloc(len3 * 4);
// initialize to all 0s
for (int i=0; i<len3; ++i)
{
p[i] = 0;
}
/* Add the digits in the shorter array:
* a[] = {1,2,3,4,5,6}
* b[] = {9,7,5}
* len = 3
* r[] = {0,0,9}
*/
int len = (len1 < len2) ? len1 : len2;
for (int i=0; i<len; ++i)
{
int t = a[i] + b[i] + p[i];
if (t >= 10)
{
p[i+1] = 1; // regroup 1
}
r[i] = t % 10; // unit digit of t
}
// x: point to the longer array
int *x = (len1 > len2) ? a : b;
/* Work on the rest of the longer array:
* add each digit with the regrouping digit, if any.
*/
for (int i=len; i<len3-1; ++i)
{
int t = x[i] + p[i];
if (t >= 10)
{
p[i+1] = 1;
}
r[i] = t % 10;
}
// Deal with the highest digit
r[len3-1] = p[len3-1];
free(p); p = NULL;
return 0;
}
int format_print(int a[], int len, int max_len)
{
/* if a[] = [3,4,5], which means the number is 543.
* len = 3; max_len = 10;
* the format and print result: " 543".
*/
char buffer[8];
sprintf(buffer, "%%%dd", max_len - len + 1);
printf(buffer, a[len-1]);
for (int i=len-2; i>=0; --i)
{
printf("%d",a[i]);
}
printf("\n");
return 0;
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("Please input 2 numbers!\n");
return 1;
}
// 1) copy argv[1] to a[]
int length1 = strlen(argv[1]);
int *a = (int *) malloc(length1 * 4);
for (int i=0; i<length1; ++i)
{
/* argv[1][] = {'3','4','5','6'}
* a[] = {6, 5, 4, 3}
*
* a[0] = argv[1][3];
* a[1] = argv[1][2];
*/
a[i] = argv[1][length1-1-i] - '0';
}
// 2) copy argv[2] to b[]
int length2 = strlen(argv[2]);
int *b = (int *) malloc(length2 * 4);
for (int i=0; i<length2; ++i)
{
/* argv[2] = {'4', '3', '0', '9'}
* b[] = {9, 0, 3, 4}
*/
b[i] = argv[2][length2-1-i] - '0';
}
// length3: larger of length1 and length2, and plus 1.
int length3 = (length1 > length2) ? length1+1 : length2+1;
int *r = (int *) malloc(length3 * 4);
add(a, length1, b, length2, r, length3);
printf("The calculation:\n");
format_print(a, length1, length3+1);
printf("+ ");
format_print(b, length2, length3-1);
for(int i=0; i<=length3; ++i) { printf("-"); }
printf("\n");
format_print(r, length3, length3+1);
free(a); a = NULL;
free(b); b = NULL;
free(r); r = NULL;
return 0;
}