Fix bug in Scala calculator

This commit is contained in:
Alexey Andreev 2015-10-21 21:09:44 +03:00
parent 8702f7ee33
commit db364f2adc
3 changed files with 6 additions and 4 deletions

View File

@ -7,7 +7,7 @@ object Calculator {
case Add(a, b) => eval(a) + eval(b) case Add(a, b) => eval(a) + eval(b)
case Subtract(a, b) => eval(a) - eval(b) case Subtract(a, b) => eval(a) - eval(b)
case Multiply(a, b) => eval(a) * eval(b) case Multiply(a, b) => eval(a) * eval(b)
case Divide(a, b) => eval(a) * eval(b) case Divide(a, b) => eval(a) / eval(b)
case Negate(n) => -eval(n) case Negate(n) => -eval(n)
case Number(v) => v case Number(v) => v
} }

View File

@ -19,8 +19,10 @@ object Client {
case (None, _) => Window.alert("Error parsing expression"); case (None, _) => Window.alert("Error parsing expression");
case (Some(x), Nil) => { case (Some(x), Nil) => {
resultList.insertBefore(doc.createElement("div", (elem : HTMLElement) => { resultList.insertBefore(doc.createElement("div", (elem : HTMLElement) => {
elem.withChild("span").withAttr("class", "plan").withText(print(x) + " = ") elem.withChild("span", (child : HTMLElement) =>
elem.withChild("span").withAttr("class", "result").withText(eval(x).toString) child.withAttr("class", "plan").withText(print(x) + " = "))
elem.withChild("span", (child : HTMLElement) =>
child.withAttr("class", "result").withText(eval(x).toString))
}), resultList.getFirstChild) }), resultList.getFirstChild)
} }
case (_, err) => Window.alert("Error parsing expression: " + err); case (_, err) => Window.alert("Error parsing expression: " + err);

View File

@ -47,7 +47,7 @@ object Rule {
} }
rule(chars => { rule(chars => {
val (result, rem) = iter(chars) val (result, rem) = iter(chars)
(Some(result.reverse), rem) (Some(result), rem)
}) })
} }
def firstOf[T](first : Rule[T], second : => Rule[T]) : Rule[T] = { def firstOf[T](first : Rule[T], second : => Rule[T]) : Rule[T] = {