-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
116 lines (106 loc) · 2.89 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"flag"
"fmt"
"github.com/Knetic/govaluate"
"log"
"strconv"
"strings"
)
//type taskRecord struct {
// level string
// mission string
// result float32
// id int16
//}
//type mongoInfo struct {
// host string
// port string
// db string
// rs string
//}
//var mongoDSN mongoInfo
type task struct {
template string
factor int
divider int
a int
b int
c int
iter int
level int
result interface{}
mission string
}
var taskInfo task
var parameters = make(map[string]interface{}, 8)
func init() {
//flag.StringVar(&mongoDSN.host,"mongo.host","127.0.0.1","MongoDB host")
//flag.StringVar(&mongoDSN.port,"mongo.port","27017","MongoDB port")
//flag.StringVar(&mongoDSN.db,"mongo.db","kidscalc","MongoDB db name")
//flag.StringVar(&mongoDSN.rs,"mongo.rs","rs0","MongoDB replica set")
flag.StringVar(&taskInfo.template, "task.template", "", "Task template. Example a+m*(b-c)")
flag.IntVar(&taskInfo.level, "task.level", 1, "Task level")
flag.IntVar(&taskInfo.iter, "task.iter", 0, "Start task iteration number from")
flag.IntVar(&taskInfo.factor, "task.m", 1, "m - multiplier in template")
flag.IntVar(&taskInfo.divider, "task.d", 1, "d - divider in template")
flag.IntVar(&taskInfo.a, "task.a", 1, "parameter a")
flag.IntVar(&taskInfo.b, "task.b", 1, "parameter b")
flag.IntVar(&taskInfo.c, "task.c", 1, "parameter c")
flag.Parse()
}
func renderTemplate(params map[string]interface{}) string {
var r *strings.Replacer
// TODO: replace switch with if statement
switch (params["m"] == 1) && (params["d"] == 1) {
case true:
r = strings.NewReplacer(
"a", strconv.Itoa(params["a"].(int)),
"b", strconv.Itoa(params["b"].(int)),
"c", strconv.Itoa(params["c"].(int)),
"m*", "",
"*m", "",
"/d", "",
"d/", "",
)
default:
r = strings.NewReplacer(
"a", strconv.Itoa(params["a"].(int)),
"b", strconv.Itoa(params["b"].(int)),
"c", strconv.Itoa(params["c"].(int)),
"m", strconv.Itoa(params["m"].(int)),
"d", strconv.Itoa(params["d"].(int)),
)
}
return r.Replace(taskInfo.template)
}
func main() {
expression, err := govaluate.NewEvaluableExpression(taskInfo.template)
if err != nil {
log.Printf("Error: %s", err)
}
parameters["iter"] = taskInfo.iter
for d := 1; d <= taskInfo.divider; d++ {
parameters["d"] = d
for m := 1; m <= taskInfo.factor; m++ {
parameters["m"] = m
for a := 1; a <= taskInfo.a; a++ {
parameters["a"] = a
for b := 1; b <= taskInfo.b; b++ {
parameters["b"] = b
for c := 1; c <= taskInfo.c; c++ {
parameters["c"] = c
result, err := expression.Evaluate(parameters)
if err != nil {
log.Printf("Error: %s", err)
}
taskInfo.result = result
taskInfo.mission = renderTemplate(parameters)
parameters["iter"] = parameters["iter"].(int) + 1
fmt.Printf("%d\t%s=%v\n", parameters["iter"], taskInfo.mission, result)
}
}
}
}
}
}