From 323fd70370921ff0d54b06704f6b994c84d5165c Mon Sep 17 00:00:00 2001 From: Gregory Date: Wed, 15 Dec 2021 23:45:29 +0200 Subject: [PATCH] fix evaluator --- .../exrecises/math_evaluator/evaluator.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/fundamentals/exrecises/math_evaluator/evaluator.go b/fundamentals/exrecises/math_evaluator/evaluator.go index 0a46810..ab85d7e 100644 --- a/fundamentals/exrecises/math_evaluator/evaluator.go +++ b/fundamentals/exrecises/math_evaluator/evaluator.go @@ -10,8 +10,8 @@ import ( // Dijkstra's two stack algorithm for equation evaluation func Evaluate(exression string) int { - operations := stack.NewStack() - values := stack.NewStack() + operations := stack.NewStack[string]() + values := stack.NewStack[int]() for _, part := range strings.Split(exression, " ") { isOperation := part == "+" || part == "-" || part == "*" || part == "/" || part == "sqrt" @@ -20,17 +20,17 @@ func Evaluate(exression string) int { continue } else if part == ")" { lastOperation := operations.Pop() - lastValue := values.Pop().(int) + lastValue := values.Pop() switch lastOperation { case "+": - lastValue = values.Pop().(int) + lastValue + lastValue = values.Pop() + lastValue case "-": - lastValue = values.Pop().(int) - lastValue + lastValue = values.Pop() - lastValue case "*": - lastValue = values.Pop().(int) * lastValue + lastValue = values.Pop() * lastValue case "/": - lastValue = values.Pop().(int) / lastValue + lastValue = values.Pop() / lastValue case "sqrt": lastValue = int(math.Sqrt(float64(lastValue))) } @@ -43,5 +43,5 @@ func Evaluate(exression string) int { } } - return values.Pop().(int) + return values.Pop() }