Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. package ee.taltech.iti0202.cpu;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class Cpu {
  7.  
  8.     private static final int STATEMENT_CHOP = 7;
  9.     private static final int NUMBER_TO_COMPARE = 6;
  10.  
  11.     public static Map<String, Integer> compute(String instructions) {
  12.         HashMap<String, Integer> answer = new HashMap<>();
  13.         String[] lines = instructions.split("\\r?\\n");
  14.         for (String onlyLine : lines) {
  15.             System.out.println(answer);
  16.             String[] splitedString = onlyLine.split(" ", STATEMENT_CHOP);
  17.             String registerToCheck = splitedString[4];
  18.             String registerToPut = splitedString[0];
  19.             if (!answer.containsKey(registerToPut)) {
  20.                 answer.put(registerToPut, 0);
  21.             }
  22.             if (!answer.containsKey(registerToCheck)) {
  23.                 answer.put(registerToCheck, 0);
  24.             }
  25.             String condition = splitedString[4] + splitedString[5] + splitedString[NUMBER_TO_COMPARE];
  26.  
  27.             int valueToCompare = Integer.parseInt(splitedString[NUMBER_TO_COMPARE]);
  28.             int valueToCheck = answer.get(registerToCheck);
  29.             int valueToPut = answer.get(registerToPut);
  30.             int whatTosum = Integer.parseInt(splitedString[2]);
  31.             switch (splitedString[5]) {
  32.                 case "==" :
  33.                     if (valueToCheck == valueToCompare) {
  34.                         answer.put(registerToPut, function(valueToPut, whatTosum, splitedString[1]));
  35.                     }
  36.                     break;
  37.  
  38.                 case ">" :
  39.                     if (valueToCheck > valueToCompare) {
  40.                         answer.put(registerToPut, function(valueToPut, whatTosum, splitedString[1]));
  41.                     }
  42.                     break;
  43.  
  44.                 case "<" :
  45.                     if (valueToCheck < valueToCompare) {
  46.                         answer.put(registerToPut, function(valueToPut, whatTosum, splitedString[1]));
  47.                     }
  48.                     break;
  49.  
  50.                 case ">=" :
  51.                     if (valueToCheck >= valueToCompare) {
  52.                         answer.put(registerToPut, function(valueToPut, whatTosum, splitedString[1]));
  53.                     }
  54.                     break;
  55.                 case "<=" :
  56.                     if (valueToCheck <= valueToCompare) {
  57.                         answer.put(registerToPut, function(valueToPut, whatTosum, splitedString[1]));
  58.                     }
  59.                     break;
  60.                 case "!=" :
  61.                     if (valueToCheck != valueToCompare) {
  62.                         answer.put(registerToPut, function(valueToPut, whatTosum, splitedString[1]));
  63.                     }
  64.                     break;
  65.                 default:
  66.                     if (true) {
  67.                         System.out.println("Add for stylecheck");
  68.                     }
  69.             }
  70.         }
  71.         return answer;
  72.     }
  73.  
  74.     private static Integer function(Integer valueToPut, Integer valutToSum, String whichFunc) {
  75. //        depend on func should do something, if inc so +, if dec so -,
  76.         if (whichFunc.equals("inc")) {
  77.             int main = valueToPut + valutToSum;
  78.             return main;
  79.         } else if (whichFunc.equals("dec")) {
  80.             int main = (valueToPut - valutToSum);
  81.             return main;
  82.         }
  83.         return 0;
  84.     }
  85.  
  86.  
  87.  
  88.  
  89.     public static void main(String[] args) {
  90.         Map<String, Integer> res = compute(
  91.                 "b inc 5 if a > 1\n"
  92.                         +
  93.                         "a inc 1 if b < 5\n"
  94.                         +
  95.                         "c dec -10 if a >= 1\n"
  96.                         +
  97.                         "c inc -20 if c == 10"
  98.         );
  99.         System.out.println(res); // {a=1, b=0, c=-10}
  100.         res = compute(
  101.                 "b inc 7 if a > 4\n"
  102.                         +
  103.                         "a inc 1 if c < 13\n"
  104.                         +
  105.                         "c dec -10 if a >= 1\n"
  106.                         +
  107.                         "c inc -20 if c == 10\n"
  108.                         +
  109.                         "abc inc 100 if a != -23\n"
  110.                         +
  111.                         "a inc 2 if a <= 0"
  112.         );
  113.         System.out.println(res); // {a=1, b=0, c=-10, abc=100}
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement