add ini config parse and fix bug:math problem

master
Starainrt 4 years ago
parent c3a165e64a
commit c3ca057b96

@ -14,7 +14,17 @@ func Calc(math string) (float64, error) {
if err := check(math); err != nil { if err := check(math); err != nil {
return 0, err 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 { func check(math string) error {
@ -37,11 +47,12 @@ func check(math string) error {
if string([]rune{v}) != "+" && string([]rune{v}) != "-" { if string([]rune{v}) != "+" && string([]rune{v}) != "-" {
return fmt.Errorf("err at position %d.Reason is sign %s not correct", k, string([]rune{v})) return fmt.Errorf("err at position %d.Reason is sign %s not correct", k, string([]rune{v}))
} }
signReady = false
} else { } else {
signReady = true signReady = true
continue
} }
} }
signReady = false
} }
if bracketSum != 0 { if bracketSum != 0 {
return fmt.Errorf("Error:right bracket is not equal as left bracket") 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) { func calcSigFloat(floatA float64, b string, floatC float64) (float64, error) {
switch b { switch b {
case "+": case "+":
return floatA + floatC, nil return floatRound(floatA + floatC,15), nil
case "-": case "-":
return floatA - floatC, nil return floatRound(floatA - floatC,15), nil
case "*": case "*":
return floatA * floatC, nil return floatRound(floatA * floatC,15), nil
case "/": case "/":
if floatC == 0 { if floatC == 0 {
return 0, errors.New("Divisor cannot be 0") return 0, errors.New("Divisor cannot be 0")
} }
return floatA / floatC, nil return floatRound(floatA / floatC,15), nil
case "^": case "^":
return math.Pow(floatA, floatC), nil return math.Pow(floatA, floatC), nil
} }

@ -47,6 +47,18 @@ type SysNode struct {
lock sync.RWMutex lock sync.RWMutex
} }
func NewIni() *SysConf {
ini := NewSysConf("=")
ini.CommentCR = true
ini.CommentFlag = []string{"#", ";"}
ini.HaveSegMent = true
ini.SegStart = "["
ini.SegEnd = "]"
ini.SpaceStr = " "
ini.EscapeFlag = "\\"
return ini
}
func NewSysConf(EqualFlag string) *SysConf { func NewSysConf(EqualFlag string) *SysConf {
syscnf := new(SysConf) syscnf := new(SysConf)
syscnf.EqualFlag = EqualFlag syscnf.EqualFlag = EqualFlag

Loading…
Cancel
Save