Example #1
0
// 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))
}
Example #2
0
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")
}
Example #3
0
func TestEval(t *testing.T) {
	for i, test := range testsEval {
		ret, err := evaler.Eval(test.in)
		if ret == nil && test.out == nil {
			// ok, do nothing
		} else if ret == nil || test.out == nil {
			t.Errorf("#%d: %s: unexpected nil result: %v vs %v", i, test.in, ret, test.out)
		} else if ret.Cmp(test.out) != 0 {
			t.Errorf("#%d: %s: bad result: got %v expected %v", i, test.in, ret, test.out)
		}
		if (err == nil) != test.ok {
			t.Errorf("#%d: %s: unexpected err result: %t vs %t", i, test.in, (err == nil), test.ok)
		}
	}
}
Example #4
0
func heristic(expression string, total int) (int, int) {
	result, err := evaler.Eval(expression)
	if err != nil {
		return 10000, -1
	}
	value, _ := result.Float64()

	if int(value) < 0 {
		return 100000, int(value)
	} else if total-int(value) >= 0 && int(value)-total >= 0 && total-int(value) < int(value)-total {
		return total - int(value), int(value)
	} else if int(value)-total >= 0 {
		return int(value) - total, int(value)
	} else {
		return 1000000000, int(value)
	}

}
Example #5
0
func main() {
	stdin := bufio.NewReader(os.Stdin)
	for true {
		numbers := generate_numbers()
		fmt.Println("Your numbers: ", strings.Join(numbers[:], " "))

		line, _, _ := stdin.ReadLine()
		if valid_expression(string(line), numbers) {
			result, err := evaler.Eval(string(line))
			if err != nil {
				fmt.Println(ERROR_MSG)
			} else if r, _ := evaler.BigratToInt(result); r == 24 {
				fmt.Println("You got it!")
			} else {
				fmt.Println("That wasn't correct :(")
			}

		} else {
			fmt.Println(ERROR_MSG)
		}
	}
}