1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package main
- import (
- "fmt"
- "github.com/Knetic/govaluate"
- "math"
- "strings"
- )
- func main() {
- //str := strings.ReplaceAll("(140<怀孕天数 && 怀孕天数<170?0.6/0.33:(169<怀孕天数 && 怀孕天数<198?1.1/0.5:(197<怀孕天数 && 怀孕天数<226?1.9/0.5:(225<怀孕天数 && 怀孕天数<254?3.1/0.5:(253<怀孕天数 && 怀孕天数<281?4.7/0.5:0)))))","怀孕天数","28")
- //str := `(温度>20?(0.085*Math.pow(体重,0.75)*(1-(温度-20)*0.005922)):(温度<5?(0.085*Math.pow(体重,0.75)/(1-(5-1*温度)*0.004644)):(0.085*Math.pow(体重,0.75))))`
- str := "(温度>20?(0.085*Math.pow(体重,0.75)*(1-(温度-20)*0.005922)):(温度<5?(0.085*Math.pow(体重,0.75)/(1-(5-1*温度)*0.004644)):(0.085*Math.pow(体重,0.75))))"
- //
- str = strings.ReplaceAll(str, "日增重", "1")
- str = strings.ReplaceAll(str, "体重", "640")
- str = strings.ReplaceAll(str, "怀孕天数", "200")
- str = strings.ReplaceAll(str, "产量", "30")
- str = strings.ReplaceAll(str, "乳脂率", "1")
- str = strings.ReplaceAll(str, "乳蛋白率", "1000")
- str = strings.ReplaceAll(str, "乳糖率", "1000")
- str = strings.ReplaceAll(str, "标准乳", "29.55")
- str = strings.ReplaceAll(str, "泌乳天数", "140")
- str = strings.ReplaceAll(str, "月龄", "1000")
- str = strings.ReplaceAll(str, "温度", "25")
- str = strings.ReplaceAll(str, "胎次", "1000")
- str = strings.ReplaceAll(str,"Math.pow","Mathpow")
- str = strings.ReplaceAll(str,"Math.round","Mathround")
- str = strings.ReplaceAll(str,"Math.log","Mathlog")
- str = strings.ReplaceAll(str,"Math.exp","Mathexp")
- //str = `(1000>30||50<100 ? (50<25 ? 5:6) : (1000>=25&&1000<=30 ? 7:1000<25 ? 8:"-"))`
- functions := map[string]govaluate.ExpressionFunction {
- "strlen": func(args ...interface{}) (interface{}, error) {
- length := len(args[0].(string))
- return (float64)(length), nil
- },
- "Mathpow" : func(args ...interface{}) (interface{}, error) {
- c:=math.Pow( args[0].(float64) , args[1].(float64))
- return (float64)(c), nil
- },
- "Mathround" : func(args ...interface{}) (interface{}, error) {
- c:=math.Round( args[0].(float64))
- return (float64)(c), nil
- },
- "Mathlog" : func(args ...interface{}) (interface{}, error) {
- c:=math.Log( args[0].(float64))
- return (float64)(c), nil
- },
- "Mathexp" : func(args ...interface{}) (interface{}, error) {
- c:=math.Exp( args[0].(float64))
- return (float64)(c), nil
- },
- }
- println(str)
- expression, err := govaluate.NewEvaluableExpressionWithFunctions(str, functions)
- if err != nil{
- println(err.Error())
- }
- result, err := expression.Evaluate(nil)
- if err != nil{
- println(err.Error())
- }
- fmt.Printf("== %+v\n", result)
- }
|