Skip to content

Instantly share code, notes, and snippets.

@codenoid
Created December 7, 2018 03:59
Show Gist options
  • Select an option

  • Save codenoid/ff01f1f432adfe39d98c24d7922842fb to your computer and use it in GitHub Desktop.

Select an option

Save codenoid/ff01f1f432adfe39d98c24d7922842fb to your computer and use it in GitHub Desktop.
Golang Div Calc
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
"strconv"
)
type IntSlice []int
func (p *IntSlice) Remove(i int) {
s := *p
s = append(s[:i], s[i+1:]...) // perfectly fine if i is the last element
*p = s
}
func main() {
fmt.Println("SOFTWARE PEMBAGIAN, HANYA BOLEH NYA BAGI (ex 2/4/2/6/7/3) DOANG,")
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter input: ")
text, _ := reader.ReadString('\n')
text = strings.TrimSuffix(text, "\n")
l := text[len(text)-1:]
if l == "/" {
text = text[:len(text)-1]
}
r, _ := regexp.Compile("([^0-9./]+)")
if r.MatchString(text) == true {
r := regexp.MustCompile("([0-9./]+)")
fmt.Println("\n[FAILED] Your input contain : " + r.ReplaceAllString(text, ""))
os.Exit(1)
}
split := strings.Split(text, "/")
if len(split) == 2 {
if split[1] == "0" {
fmt.Println("\n[FAILED] Cannot divide by Zero (0)")
os.Exit(1)
} else {
f, ferr := strconv.Atoi(split[0])
s, serr := strconv.Atoi(split[1])
if ferr == nil && serr == nil {
fmt.Println("\n[SUCCESS] Result : " + fmt.Sprintf("%f", float64(f)/float64(s)))
}
}
} else if len(split) > 2 {
f, ferr := strconv.Atoi(split[0])
s, serr := strconv.Atoi(split[1])
if ferr == nil && serr == nil {
fdiv := float64(f)/float64(s)
for index, element := range split {
if index > 1 {
if element != "0" {
parseInt, err := strconv.Atoi(element)
if err != nil {
continue
}
fdiv = fdiv/float64(parseInt)
} else {
fmt.Println("\n[FAILED] Cannot divide by Zero (0)")
os.Exit(1)
}
}
}
fmt.Println("\n[SUCCESS] Result : " + fmt.Sprintf("%f", fdiv))
}
}
}
@codenoid
Copy link
Author

codenoid commented Dec 7, 2018

Some improvement :

  1. split remove first element & check ada 0 ngga, kalo ada langsung remove aja dari atas
  2. map div after first div

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment