-
Notifications
You must be signed in to change notification settings - Fork 1
/
LongArithmetic.cpp
50 lines (39 loc) · 1.11 KB
/
LongArithmetic.cpp
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
#include <stdio.h>
#define max(x, y) (((x) > (y)) ? (x) : (y))
int b[4] = {1, 2, 3, 5};
int a[4] = {4, 5, 3, 1};
void sum();
int main(){
sum();
}
//a + b = c
void sum(){
int len_of_a = sizeof(a) / sizeof(int);
int len_of_b = sizeof(b) / sizeof(int);
int max_len = max(len_of_a, len_of_b);
int new_b[max_len] = { 0 };
int new_a[max_len] = { 0 };
int c[max_len + 1] = { 0 };
if (len_of_a > len_of_b){
for (int i = len_of_b - 1; i >= 0; i--)
new_b[len_of_a - len_of_b + i] = b[i];
for (int i = len_of_a - 1; i >= 0; i--)
new_a[i] = a[i];
}else{
for (int i = len_of_a - 1; i >= 0; i--)
new_a[len_of_b - len_of_a + i] = a[i];
for (int i = len_of_b - 1; i >= 0; i--)
new_b[i] = b[i];
}
int div_ = 0;
for (int i = max_len - 1; i >= 0; i--){
int mod_ = (new_a[i] + new_b[i]) % 10;
c[i+1] = div_ + mod_;
div_ = (new_a[i] + new_b[i]) / 10;
}
if (div_ != 0){
c[0] += div_;
}
for (int i = 0; i < max_len + 1; i++)
printf("%d ", c[i]);
}