main.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/Knetic/govaluate"
  5. "math"
  6. "strings"
  7. )
  8. func main() {
  9. //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")
  10. //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))))`
  11. 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))))"
  12. //
  13. str = strings.ReplaceAll(str, "日增重", "1")
  14. str = strings.ReplaceAll(str, "体重", "640")
  15. str = strings.ReplaceAll(str, "怀孕天数", "200")
  16. str = strings.ReplaceAll(str, "产量", "30")
  17. str = strings.ReplaceAll(str, "乳脂率", "1")
  18. str = strings.ReplaceAll(str, "乳蛋白率", "1000")
  19. str = strings.ReplaceAll(str, "乳糖率", "1000")
  20. str = strings.ReplaceAll(str, "标准乳", "29.55")
  21. str = strings.ReplaceAll(str, "泌乳天数", "140")
  22. str = strings.ReplaceAll(str, "月龄", "1000")
  23. str = strings.ReplaceAll(str, "温度", "25")
  24. str = strings.ReplaceAll(str, "胎次", "1000")
  25. str = strings.ReplaceAll(str,"Math.pow","Mathpow")
  26. str = strings.ReplaceAll(str,"Math.round","Mathround")
  27. str = strings.ReplaceAll(str,"Math.log","Mathlog")
  28. str = strings.ReplaceAll(str,"Math.exp","Mathexp")
  29. //str = `(1000>30||50<100 ? (50<25 ? 5:6) : (1000>=25&&1000<=30 ? 7:1000<25 ? 8:"-"))`
  30. functions := map[string]govaluate.ExpressionFunction {
  31. "strlen": func(args ...interface{}) (interface{}, error) {
  32. length := len(args[0].(string))
  33. return (float64)(length), nil
  34. },
  35. "Mathpow" : func(args ...interface{}) (interface{}, error) {
  36. c:=math.Pow( args[0].(float64) , args[1].(float64))
  37. return (float64)(c), nil
  38. },
  39. "Mathround" : func(args ...interface{}) (interface{}, error) {
  40. c:=math.Round( args[0].(float64))
  41. return (float64)(c), nil
  42. },
  43. "Mathlog" : func(args ...interface{}) (interface{}, error) {
  44. c:=math.Log( args[0].(float64))
  45. return (float64)(c), nil
  46. },
  47. "Mathexp" : func(args ...interface{}) (interface{}, error) {
  48. c:=math.Exp( args[0].(float64))
  49. return (float64)(c), nil
  50. },
  51. }
  52. println(str)
  53. expression, err := govaluate.NewEvaluableExpressionWithFunctions(str, functions)
  54. if err != nil{
  55. println(err.Error())
  56. }
  57. result, err := expression.Evaluate(nil)
  58. if err != nil{
  59. println(err.Error())
  60. }
  61. fmt.Printf("== %+v\n", result)
  62. }