24 game done

This commit is contained in:
Dmitry Jemerov
2011-05-27 19:10:56 +04:00
parent 78247520c7
commit b2df0879b3
+52 -27
View File
@@ -3,39 +3,64 @@ namespace TwentyFourGame;
import java.util.* import java.util.*
import java.io.* import java.io.*
fun takeFirst(expr: StringBuilder): Char { fun StringBuilder.takeFirst(): Char {
val c = expr.charAt(0) if (this.length() == 0) return '\0'
expr.deleteCharAt(0) val c = this.charAt(0)
this.deleteCharAt(0)
return c return c
} }
class Evaluator(val expr: StringBuilder, val numbers: ArrayList<Int>) {
fun evaluateArg(expr: StringBuilder, numbers: ArrayList<Int>): Int { fun checkFirst(expect: Char): Boolean {
if (expr.length() == 0) throw new Exception("Syntax error: Character expected"); if (expr.length() > 0 && expr.charAt(0) == expect) {
val c = takeFirst(expr) expr.deleteCharAt(0)
if (c >= '0' && c <= '9') { return true
val n = c - '0' }
if (!numbers.contains(n)) throw new Exception("You used incorrect number: " + n) return false
numbers.remove(n)
return n
} }
throw new Exception("Syntax error: Unrecognized character " + c)
}
fun evaluateAdd(expr: StringBuilder, numbers: ArrayList<Int>): Int {
val lhs = evaluateArg(expr, numbers)
if (expr.length() > 0) {
fun evaluateArg(): Int {
val c = expr.takeFirst()
when(c) {
in '0'..'9' => {
val n = c - '0'
val index = numbers.indexOf(n)
if (index < 0) throw new Exception("You used incorrect number: " + n)
numbers.remove(index) // gotcha: conflict between remove(Object) and remove(int)
return n
}
'(' => {
val result = evaluate()
if (expr.takeFirst() != ')') throw new Exception(") expected")
return result
}
'\0' => throw new Exception("Syntax error: Character expected")
else => throw new Exception("Syntax error: Unrecognized character " + c)
}
} }
return lhs
}
fun evaluate(expr: StringBuilder, numbers: ArrayList<Int>): Int { fun evaluateMult(): Int {
val lhs = evaluateAdd(expr, numbers) val lhs = evaluateArg()
if (expr.length() > 0) { if (checkFirst('*'))
val c = expr.charAt(0) return lhs * evaluateMult()
expr.deleteCharAt(0) if (checkFirst('/'))
return lhs / evaluateMult()
return lhs
}
fun evaluate(): Int {
val lhs = evaluateMult()
if (checkFirst('+'))
return lhs + evaluate()
if (checkFirst('-'))
return lhs - evaluate()
return lhs
}
fun evaluateAll(): Int {
val result = evaluate()
if (expr.length() > 0) throw new Exception("unexpected text: " + expr)
return result
} }
return lhs
} }
fun main(args: Array<String>) { fun main(args: Array<String>) {
@@ -54,7 +79,7 @@ fun main(args: Array<String>) {
val reader = new BufferedReader(new InputStreamReader(System.`in`)) val reader = new BufferedReader(new InputStreamReader(System.`in`))
val expr = new StringBuilder(reader.readLine()) val expr = new StringBuilder(reader.readLine())
try { try {
val result = evaluate(expr, numbers) val result = new Evaluator(expr, numbers).evaluateAll()
if (result != 24) if (result != 24)
System.out?.println("Sorry, that's " + result) System.out?.println("Sorry, that's " + result)
else else