Created
September 27, 2012 15:19
-
-
Save steve-taylor/3794578 to your computer and use it in GitHub Desktop.
Semi (actual submission)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Problem1 { | |
static enum Oper { | |
PLUS, | |
MINUS | |
} | |
public static void main(String[] args) { | |
Oper lastOp = null; | |
Integer lastNum = null; | |
Integer res = null; | |
for (String s : Input.read().split(" ")) { | |
System.out.println("" + s + " " + res); | |
int n = num(s); | |
if (n > 0) { | |
if (res == null) { | |
res = n; | |
lastNum = n; | |
continue; | |
} | |
if (lastOp != null) { | |
switch (lastOp) { | |
case PLUS: | |
res += lastNum; | |
break; | |
case MINUS: | |
res -= lastNum; | |
break; | |
default: | |
// ?? | |
} | |
} | |
lastNum = n; | |
} | |
else if (s.equals("plus")) { | |
lastOp = Oper.PLUS; | |
} | |
else if (s.equals("minus")) { | |
lastOp = Oper.MINUS; | |
} | |
else { | |
// ??? | |
} | |
} | |
Output.write("" + res); | |
} | |
static int num(String input) { | |
if (input.equals("one")) return 1; | |
if (input.equals("two")) return 2; | |
if (input.equals("three")) return 3; | |
if (input.equals("four")) return 4; | |
if (input.equals("five")) return 5; | |
if (input.equals("six")) return 6; | |
if (input.equals("seven")) return 7; | |
if (input.equals("eight")) return 8; | |
if (input.equals("nine")) return 9; | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment