Created
December 7, 2018 03:59
-
-
Save codenoid/ff01f1f432adfe39d98c24d7922842fb to your computer and use it in GitHub Desktop.
Golang Div Calc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some improvement :