yanchevilian

JaggedArray Manipulator Vol. 2

Aug 29th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace jaggedArrayManipulator
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int rows = int.Parse(Console.ReadLine());
  12.             double[][] array = new double[rows][];
  13.  
  14.             for (int row = 0; row < rows; row++)
  15.             {
  16.                 array[row] = ConsoleParse();
  17.             }
  18.  
  19.             for (int row = 0; row < rows - 1; row++)
  20.             {
  21.                 if (array[row].Length == array[row + 1].Length)
  22.                 {
  23.                     array[row] = Multiplier(array[row]);
  24.                     array[row + 1] = Multiplier(array[row + 1]);
  25.                 }
  26.                 else if (array[row].Length != array[row + 1].Length)
  27.                 {
  28.                     array[row] = Divider(array[row]);
  29.                     array[row + 1] = Divider(array[row + 1]);
  30.                 }
  31.             }
  32.  
  33.             string command = Console.ReadLine();
  34.  
  35.             while (true)
  36.             {
  37.                 string[] cmdSplit = command
  38.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  39.                 if (cmdSplit[0] == "Add")
  40.                 {
  41.                     int row = int.Parse(cmdSplit[1]);
  42.                     int col = int.Parse(cmdSplit[2]);
  43.                     double value = double.Parse(cmdSplit[3]);
  44.                     if ((row >= 0 && row < array.Length) && (col >= 0 && col < array[row].Length))
  45.                     {
  46.                         array[row][col] = AddOperator(array[row][col], value);
  47.                     }
  48.                 }
  49.                 else if (cmdSplit[0] == "Subtract")
  50.                 {
  51.                     int row = int.Parse(cmdSplit[1]);
  52.                     int col = int.Parse(cmdSplit[2]);
  53.                     double value = double.Parse(cmdSplit[3]);
  54.                     if ((row >= 0 && row < array.Length) && (col >= 0 && col < array[row].Length))
  55.                     {
  56.                         array[row][col] = SubOperator(array[row][col], value);
  57.                     }
  58.                 }
  59.                 else if (cmdSplit[0] == "End")
  60.                 {
  61.                     foreach (var row in array)
  62.                     {
  63.                         Console.Write(string.Join(" ", row));
  64.                         Console.WriteLine();
  65.                     }
  66.                     break;
  67.                 }
  68.  
  69.                 command = Console.ReadLine();
  70.             }
  71.         }
  72.  
  73.         static double AddOperator(double number, double value) =>
  74.             number += value;
  75.  
  76.         static double SubOperator(double number, double value) =>
  77.             number -= value;
  78.  
  79.         static double[] ConsoleParse() =>
  80.             Console.ReadLine()
  81.             .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  82.             .Select(double.Parse)
  83.             .ToArray();
  84.  
  85.         static double[] Multiplier(double[] array) =>
  86.             array = array
  87.             .Select(x => x * 2)
  88.             .ToArray();
  89.  
  90.         static double[] Divider(double[] array) =>
  91.             array = array
  92.             .Select(x => x / 2)
  93.             .ToArray();
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment