Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ee.taltech.iti0202.cpu;
- import java.util.HashMap;
- import java.util.Map;
- public class Cpu {
- private static final int STATEMENT_CHOP = 7;
- private static final int NUMBER_TO_COMPARE = 6;
- public static Map<String, Integer> compute(String instructions) {
- HashMap<String, Integer> answer = new HashMap<>();
- String[] lines = instructions.split("\\r?\\n");
- for (String onlyLine : lines) {
- System.out.println(answer);
- String[] splitedString = onlyLine.split(" ", STATEMENT_CHOP);
- String registerToCheck = splitedString[4];
- String registerToPut = splitedString[0];
- if (!answer.containsKey(registerToPut)) {
- answer.put(registerToPut, 0);
- }
- if (!answer.containsKey(registerToCheck)) {
- answer.put(registerToCheck, 0);
- }
- String condition = splitedString[4] + splitedString[5] + splitedString[NUMBER_TO_COMPARE];
- int valueToCompare = Integer.parseInt(splitedString[NUMBER_TO_COMPARE]);
- int valueToCheck = answer.get(registerToCheck);
- int valueToPut = answer.get(registerToPut);
- int whatTosum = Integer.parseInt(splitedString[2]);
- switch (splitedString[5]) {
- case "==" :
- if (valueToCheck == valueToCompare) {
- answer.put(registerToPut, function(valueToPut, whatTosum, splitedString[1]));
- }
- break;
- case ">" :
- if (valueToCheck > valueToCompare) {
- answer.put(registerToPut, function(valueToPut, whatTosum, splitedString[1]));
- }
- break;
- case "<" :
- if (valueToCheck < valueToCompare) {
- answer.put(registerToPut, function(valueToPut, whatTosum, splitedString[1]));
- }
- break;
- case ">=" :
- if (valueToCheck >= valueToCompare) {
- answer.put(registerToPut, function(valueToPut, whatTosum, splitedString[1]));
- }
- break;
- case "<=" :
- if (valueToCheck <= valueToCompare) {
- answer.put(registerToPut, function(valueToPut, whatTosum, splitedString[1]));
- }
- break;
- case "!=" :
- if (valueToCheck != valueToCompare) {
- answer.put(registerToPut, function(valueToPut, whatTosum, splitedString[1]));
- }
- break;
- default:
- if (true) {
- System.out.println("Add for stylecheck");
- }
- }
- }
- return answer;
- }
- private static Integer function(Integer valueToPut, Integer valutToSum, String whichFunc) {
- // depend on func should do something, if inc so +, if dec so -,
- if (whichFunc.equals("inc")) {
- int main = valueToPut + valutToSum;
- return main;
- } else if (whichFunc.equals("dec")) {
- int main = (valueToPut - valutToSum);
- return main;
- }
- return 0;
- }
- public static void main(String[] args) {
- Map<String, Integer> res = compute(
- "b inc 5 if a > 1\n"
- +
- "a inc 1 if b < 5\n"
- +
- "c dec -10 if a >= 1\n"
- +
- "c inc -20 if c == 10"
- );
- System.out.println(res); // {a=1, b=0, c=-10}
- res = compute(
- "b inc 7 if a > 4\n"
- +
- "a inc 1 if c < 13\n"
- +
- "c dec -10 if a >= 1\n"
- +
- "c inc -20 if c == 10\n"
- +
- "abc inc 100 if a != -23\n"
- +
- "a inc 2 if a <= 0"
- );
- System.out.println(res); // {a=1, b=0, c=-10, abc=100}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement