-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic.go
113 lines (107 loc) · 1.89 KB
/
arithmetic.go
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
package main
import (
"strconv"
)
func AdditionBigNumbers(a string, b string) string {
for len(b) < len(a) {
b = "0" + b
}
for len(a) < len(b) {
a = "0" + a
}
result := ""
c := 0
for i := len(a) - 1; i >= 0; i-- {
localResult := int(a[i]-48) + int(b[i]-48) + c
c = 0
if localResult > 9 {
localResult -= 10
c = 1
}
result += strconv.Itoa(localResult)
}
if c == 1 {
result += "1"
}
fresult := ""
for _, s := range result {
fresult = string(s) + fresult
}
return fresult
}
func SubtractionBigNumbers(a string, b string) string {
k := false
if len(a) < len(b) {
a, b = b, a
k = true
}
ai, _ := strconv.Atoi(a)
bi, _ := strconv.Atoi(b)
if ai < bi {
a, b = b, a
k = true
}
for len(b) < len(a) {
b = "0" + b
}
for len(a) < len(b) {
a = "0" + a
}
result := ""
c := 0
for i := len(a) - 1; i >= 0; i-- {
localResult := int(a[i]-48) - int(b[i]-48) - c
c = 0
if localResult < 0 {
localResult += 10
c = 1
}
result += strconv.Itoa(localResult)
}
if c == 1 {
result += "1"
}
for result[len(result)-1] == '0' {
result = result[:len(result)-1]
}
fresult := ""
for _, s := range result {
fresult = string(s) + fresult
}
if k {
fresult = "-" + fresult
}
return fresult
}
func MultiplicationBigNumbers(a string, b string) string {
result := ""
c := 0
for i := len(b) - 1; i >= 0; i-- {
localResult := ""
c = 0
for j := len(a) - 1; j >= 0; j-- {
localLocalResult := int(a[j]-48)*int(b[i]-48) + c
c = 0
for localLocalResult > 9 {
localLocalResult -= 10
c++
}
localResult += strconv.Itoa(localLocalResult)
}
if c > 0 {
localResult += strconv.Itoa(c)
c = 0
}
flocalResult := ""
for _, s := range localResult {
flocalResult = string(s) + flocalResult
}
for f := 0; f < len(b)-i-1; f++ {
flocalResult += "0"
}
result = AdditionBigNumbers(result, flocalResult)
}
return result
}
func main() {
}