Advertisement
Levi0227

6. heti házi

Oct 23rd, 2023 (edited)
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace PMPHF005_P99J7Y
  9. {
  10.     internal class PMPHF005_P99J7Y
  11.     {
  12.         static int GetOperandValue(string operand, int[] registers)
  13.         {
  14.             if (Char.IsLetter(operand[0]))
  15.             {
  16.                 return registers[operand[0] - 'A'];
  17.             }
  18.             else
  19.             {
  20.                 return int.Parse(operand);
  21.             }
  22.         }
  23.         static void Main(string[] args)
  24.         {
  25.             int[] registers = new int[4];
  26.  
  27.             string[] lines = File.ReadAllLines("input.txt");
  28.             string[] initialRegisters = lines[0].Split(',');
  29.  
  30.             for (int i = 0; i < 4; i++)
  31.             {
  32.                 registers[i] = int.Parse(initialRegisters[i]);
  33.             }
  34.  
  35.             int currentLine = 1;
  36.  
  37.             while (currentLine < lines.Length)
  38.             {
  39.                 string line = lines[currentLine];
  40.                 string[] parts = line.Split(' ');
  41.  
  42.                 string command = parts[0];
  43.  
  44.                 if (command == "MOV")
  45.                 {
  46.                     char destReg = parts[1][0];
  47.                     string src = parts[2];
  48.  
  49.                     int value = GetOperandValue(src, registers);
  50.  
  51.                     registers[destReg - 'A'] = value;
  52.                 }
  53.                 else if (command == "ADD")
  54.                 {
  55.                     char destReg = parts[1][0];
  56.                     string src1 = parts[2];
  57.                     string src2 = parts[3];
  58.  
  59.                     int value1 = GetOperandValue(src1, registers);
  60.                     int value2 = GetOperandValue(src2, registers);
  61.  
  62.                     registers[destReg - 'A'] = value1 + value2;
  63.                 }
  64.                 else if (command == "SUB")
  65.                 {
  66.                     char destReg = parts[1][0];
  67.                     string src1 = parts[2];
  68.                     string src2 = parts[3];
  69.  
  70.                     int value1 = GetOperandValue(src1, registers);
  71.                     int value2 = GetOperandValue(src2, registers);
  72.  
  73.                     registers[destReg - 'A'] = value1 - value2;
  74.                 }
  75.                 else if (command == "JNE")
  76.                 {
  77.                     int jumpLine = int.Parse(parts[1]);
  78.                     char srcReg1 = parts[2][0];
  79.                     string srcReg2 = parts[3];
  80.  
  81.                     int value1 = registers[srcReg1 - 'A'];
  82.                     int value2 = GetOperandValue(srcReg2, registers);
  83.  
  84.                     if (value1 != value2)
  85.                     {
  86.                         currentLine = jumpLine;
  87.                     }
  88.                 }
  89.  
  90.                 currentLine++;
  91.             }
  92.  
  93.             string output = string.Join(",", registers);
  94.             File.WriteAllText("output.txt", output);
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement