Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.03 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Runtime.InteropServices;
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         int[] arr = Console.ReadLine()
  10.                         .Split(' ')
  11.                         .Select(int.Parse)
  12.                         .ToArray();
  13.         string command = Console.ReadLine();
  14.         int currentPosition = 0;
  15.         int arrHelper = currentPosition;
  16.         string operations = "&|^+-*/";
  17.         while (command != "stop")
  18.         {
  19.             var line = command.Split(' ');
  20.             int offset = int.Parse(line[0]);
  21.             string symbol = line[1];
  22.             int operand = int.Parse(line[2]);
  23.             arrHelper += offset; //0 + 2
  24.             /*
  25.                 1 2 3 4 5
  26.                 2 * 2    position = 2
  27.                 2 / 2    position = 4
  28.                 1 – 2    position = 0
  29.                 -2 & 1   position = 3
  30.                 stop
  31.             */
  32.             if (arrHelper >= arr.Length && offset >= 0) //2 > 5
  33.             {
  34.                 currentPosition = arrHelper - arr.Length;
  35.                 GetValue(symbol, arr, currentPosition, operand);
  36.                 if (arr[currentPosition] < 0)
  37.                 {
  38.                     arr[currentPosition] = 0;
  39.                 }
  40.                 arrHelper = currentPosition;
  41.             }
  42.             else if (arrHelper < 0 && offset < 0)
  43.             {
  44.                 currentPosition = arrHelper + arr.Length;
  45.                 GetValue(symbol, arr, currentPosition, operand);
  46.                 if (arr[currentPosition] < 0)
  47.                 {
  48.                     arr[currentPosition] = 0;
  49.                 }
  50.                 arrHelper = currentPosition;
  51.             }
  52.             else
  53.             {
  54.                 currentPosition = arrHelper;
  55.                 GetValue(symbol, arr, currentPosition, operand);
  56.                 if (arr[currentPosition] < 0)
  57.                 {
  58.                     arr[currentPosition] = 0;
  59.                 }
  60.                 arrHelper = currentPosition;
  61.             }
  62.  
  63.             command = Console.ReadLine();
  64.         }
  65.         Console.Write("[");
  66.         Console.Write(string.Join(", ", arr));
  67.         Console.Write("]");
  68.         Console.WriteLine(  );
  69.     }
  70.  
  71.     private static void GetValue(string symbol, int[] arr, int currentPosition, int operand)
  72.     {
  73.         switch (symbol)
  74.         {
  75.             case "&":
  76.                 arr[currentPosition] &= operand;
  77.                 break;
  78.             case "|":
  79.                 arr[currentPosition] |= operand;
  80.                 break;
  81.             case "^":
  82.                 arr[currentPosition] ^= operand;
  83.                 break;
  84.             case "+":
  85.                 arr[currentPosition] += operand;
  86.                 break;
  87.             case "-":
  88.                 arr[currentPosition] -= operand;
  89.                 break;
  90.             case "*":
  91.                 arr[currentPosition] *= operand;
  92.                 break;
  93.             case "/":
  94.                 arr[currentPosition] /= operand;
  95.                 break;
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement