aboutsummaryrefslogtreecommitdiff
path: root/tests/sysy_lvX/bigintadd.sy
blob: 3dfac88b969040dcf1f758fa3635ad960b043b40 (plain)
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

int BigIntarr[2][100];
int BigIntLength[2];
int Result[200];

int min(int a, int b) {
	if (a > b) {
		return b;
	}
	else {
		return a;
	}
}

int add() {
	int a[100];//加数
	int b[100];//被加数
	int c[100] = { 0 };//和
	int i, j = 0, k;//i用来循环,j表示进位 ,两加数中较小加数的长度放到k里
	int l1, l2, d;
	l1 = BigIntLength[0];
	l2 = BigIntLength[1];
	k = min(l1, l2);
	i = 0;
	while (i < BigIntLength[0]) {
		a[i] = BigIntarr[0][l1 - 1 - i];
		i = i + 1;
	}
	i = 0;
	while (i < BigIntLength[1]) {
		b[i] = BigIntarr[1][l2 - 1 - i];
		i = i + 1;
	}
	i = 0;
	while(i<k){
		d = a[i] + b[i] + j;
		j = d / 10;
		c[i] = d % 10;
		i = i + 1;
	}
	while (i < l1) {
		d = a[i] + j;
		j = d / 10;
		c[i] = d % 10;
		i = i + 1;
	}
	while (i < l2) {
		d = b[i] + j;
		j = d / 10;
		c[i] = d % 10;
		i = i + 1;
	}
	if (j > 0) { c[i] = j; i=i+1; }
	j = 0;
	k = i - 1;
	while (k > -1) {
		Result[j] = c[k];
		k = k - 1;
		j = j + 1;
	}	
	return j;
}

int main()
{
	int n;
	int i = 0;
    BigIntLength[0] = getarray(BigIntarr[0]);
    BigIntLength[1] = getarray(BigIntarr[1]);
	n = add();
    i = 0;
    while (i < n) {
        putint(Result[i]);
        i = i + 1;
	}
	return 0;
}