package main
import (
"fmt"
"strconv"
)
func HpAdd(a string, b string) [] int {
aslice := make([]int, 0, len(a))
bslice := make([]int, 0, len(b))
c := make([]int, 0)
for i:=len(a)-1; i>=0; i-- {
ai, _ := strconv.Atoi(string(a[i]))
aslice = append(aslice, ai)
}
for i:=len(b)-1; i>=0; i-- {
bi, _ :=strconv.Atoi(string(b[i]))
bslice = append(bslice, bi)
}
t := 0
for i:=0;i<len(aslice) || i <len(bslice); i++ {
if i <len(aslice) {
t += aslice[i]
}
if i < len(bslice) {
t += bslice[i]
}
c = append(c, t%10)
t = t / 10
}
if t != 0 {
c = append(c, t)
}
fmt.Println(c)
return c
}
func HpSub(a string, b string) []int {
// 123-23
aslice := make([]int, 0, len(a))
bslice := make([]int, 0, len(b))
c := make([]int, 0)
for i:=len(a)-1; i>=0; i-- {
ai, _ := strconv.Atoi(string(a[i]))
aslice = append(aslice, ai)
}
for i:=len(b)-1; i>=0; i-- {
bi, _ :=strconv.Atoi(string(b[i]))
bslice = append(bslice, bi)
}
t := 0
// 32 211
// 112 - 23 = 89
for i:=0; i<len(aslice); i++ {
t = aslice[i] - t
if (i < len(bslice)) {
t = t - bslice[i]
}
c = append(c, (t + 10) % 10)
if (t >= 0) {
t = 0
} else {
t = 1
}
}
for i:=len(c) - 1;len(c) > 1 && c[len(c) - 1] == 0;i-- {
c = c[:i]
}
fmt.Println(c)
return c
}
func HpMulti(a string, b int) []int {
aslice := make([]int, 0, len(a))
c := make([]int, 0)
for i:=len(a)-1; i>=0; i-- {
ai, _ := strconv.Atoi(string(a[i]))
aslice = append(aslice, ai)
}
// 12 * 7
// 21 7
t := 0
for i:= 0;i<len(aslice) || t != 0;i++ {
if (i < len(aslice)) {
t += aslice[i]*b
}
c = append(c, t % 10)
t /= 10
}
fmt.Println(c)
return c
}
func HpDiv(a string, b int) []int {
aslice := make([]int, 0, len(a))
c := make([]int, 0)
for i:=len(a)-1; i>=0; i-- {
ai, _ := strconv.Atoi(string(a[i]))
aslice = append(aslice, ai)
}
t := 0
for i:= len(aslice) - 1;i>=0;i-- {
t = t * 10 + aslice[i]
c = append(c, t/b)
t = t % b
}
for i:=0;len(c) > 1 && c[i] == 0;i++ {
c = c[i+1:]
}
fmt.Println(c)
return c
}
func main() {
HpAdd("123","923")
HpSub("1023","934")
HpMulti("423", 3)
HpDiv("123",3)
}