func TestBigratToFloat(t *testing.T) { for i, test := range testsBigratToFloat { ret := evaler.BigratToFloat(test.in) if ret != test.out { t.Errorf("#%d: got %d expected %d", i, ret, test.out) } } }
// calc function takes an arithmetic expression in string-form, and using // 'evaler.Eval', it evaluates that expression and displays the result of // it to the user in the interactive-mode. func calc(expression string) { result, err := evaler.Eval(expression) if err != nil { fmt.Println("Error while evaluating expression.", err) return } fmt.Printf("Result of expression: %f\n", evaler.BigratToFloat(result)) }
func main() { if len(os.Args) != 5 { fmt.Println("Error: Exactly four numerical arguments are expected.") return } input_numbers := []int{} operations := []string{"+", "-", "*", "/"} for _, num := range os.Args[1:] { if num, err := strconv.Atoi(num); err != nil { fmt.Println("Error: Exactly four numerical arguments are expected") return } else { input_numbers = append(input_numbers, num) } } digit_permutations := permutate(input_numbers) operation_permutations := repetative_choose(3, []int{0, 1, 2, 3}) expressions := []string{} //combine every digit permutation with every operation permutation and with every bracket permutation for _, digits := range digit_permutations { for _, operation_indices := range operation_permutations { //operation index -> operation string //cause no generic permutation :( ops := make([]string, 3) for i, operation_index := range operation_indices { ops[i] = operations[operation_index] } expressions = append(expressions, generate_possible_expressions(digits, ops)...) } } fmt.Println("Got", len(expressions), "expressions. Now calculating") c := 0 duplicates := make(map[string]bool) for _, expr := range expressions { if duplicates[expr] { continue } if res, err := evaler.Eval(expr); err == nil { if res2 := evaler.BigratToFloat(res); res2 == 24.0 { fmt.Println(expr) duplicates[expr] = true c++ } } } fmt.Println("Got", c, "solutions") }