|
|
|
@ -14,7 +14,17 @@ func Calc(math string) (float64, error) {
|
|
|
|
|
if err := check(math); err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
return calc(math)
|
|
|
|
|
result,err:=calc(math)
|
|
|
|
|
if err!=nil {
|
|
|
|
|
return 0,err
|
|
|
|
|
}
|
|
|
|
|
return floatRound(result,15),nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func floatRound(f float64, n int) float64 {
|
|
|
|
|
format := "%." + strconv.Itoa(n) + "f"
|
|
|
|
|
res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func check(math string) error {
|
|
|
|
@ -37,11 +47,12 @@ func check(math string) error {
|
|
|
|
|
if string([]rune{v}) != "+" && string([]rune{v}) != "-" {
|
|
|
|
|
return fmt.Errorf("err at position %d.Reason is sign %s not correct", k, string([]rune{v}))
|
|
|
|
|
}
|
|
|
|
|
signReady = false
|
|
|
|
|
} else {
|
|
|
|
|
signReady = true
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
signReady = false
|
|
|
|
|
}
|
|
|
|
|
if bracketSum != 0 {
|
|
|
|
|
return fmt.Errorf("Error:right bracket is not equal as left bracket")
|
|
|
|
@ -188,16 +199,16 @@ func calcPool(numPool []float64, operPool []string) (float64, error) {
|
|
|
|
|
func calcSigFloat(floatA float64, b string, floatC float64) (float64, error) {
|
|
|
|
|
switch b {
|
|
|
|
|
case "+":
|
|
|
|
|
return floatA + floatC, nil
|
|
|
|
|
return floatRound(floatA + floatC,15), nil
|
|
|
|
|
case "-":
|
|
|
|
|
return floatA - floatC, nil
|
|
|
|
|
return floatRound(floatA - floatC,15), nil
|
|
|
|
|
case "*":
|
|
|
|
|
return floatA * floatC, nil
|
|
|
|
|
return floatRound(floatA * floatC,15), nil
|
|
|
|
|
case "/":
|
|
|
|
|
if floatC == 0 {
|
|
|
|
|
return 0, errors.New("Divisor cannot be 0")
|
|
|
|
|
}
|
|
|
|
|
return floatA / floatC, nil
|
|
|
|
|
return floatRound(floatA / floatC,15), nil
|
|
|
|
|
case "^":
|
|
|
|
|
return math.Pow(floatA, floatC), nil
|
|
|
|
|
}
|
|
|
|
|