Advertisement
junniorrkaa

Объединение в одну коллекцию

Jul 5th, 2025 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.66 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSLight
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             const string CommandInputData = "1";
  11.             const string CommandSumData = "2";
  12.             const string CommandRestart = "3";
  13.  
  14.             List<string> lines = new List<string>();
  15.  
  16.             string[] firstArray = new string[0];
  17.             string[] secondArray = new string[0];
  18.  
  19.             bool isWorking = true;
  20.  
  21.             Console.CursorVisible = false;
  22.  
  23.             while (isWorking)
  24.             {
  25.                 if (lines.Count != 0)
  26.                 {
  27.                     OutputSum(lines);
  28.                 }
  29.  
  30.                 if (firstArray.Length != 0)
  31.                 {
  32.                     OutputData(firstArray);
  33.                     Console.WriteLine("\n");
  34.                     OutputData(secondArray);
  35.                     Console.WriteLine("\n");
  36.                 }
  37.  
  38.                 Console.WriteLine($"{CommandInputData} - Добавить данные");
  39.                 Console.WriteLine($"\n{CommandSumData} - Объединить данные");
  40.                 Console.WriteLine($"\n{CommandRestart} - Очистить данные");
  41.                 Console.Write("\nВведите команду: ");
  42.  
  43.                 switch (Console.ReadLine())
  44.                 {
  45.                     case CommandInputData:
  46.                         InputData(ref firstArray, ref secondArray);
  47.                         break;
  48.  
  49.                     case CommandSumData:
  50.                         SumNumbers(lines, firstArray);
  51.                         SumNumbers(lines, secondArray);
  52.                         break;
  53.  
  54.                     case CommandRestart:
  55.                         firstArray = new string[0];
  56.                         secondArray = new string[0];
  57.                         lines.Clear();
  58.                         break;
  59.  
  60.                     default:
  61.                         Console.WriteLine("\nТакой команды нет.");
  62.                         isWorking = false;
  63.                         break;
  64.                 }
  65.  
  66.                 Console.ReadKey();
  67.                 Console.Clear();
  68.             }
  69.         }
  70.  
  71.         static void InputData(ref string[] firstArray, ref string[] secondArray)
  72.         {
  73.             string[] tempFirstArray = new string[firstArray.Length + 1];
  74.             string[] tempSecondArray = new string[secondArray.Length + 1];
  75.  
  76.             int minNumber = 0;
  77.             int maxNumber = 10;
  78.  
  79.             Random rand = new Random();
  80.  
  81.             for (int i = 0; i < tempFirstArray.Length; i++)
  82.             {
  83.                 tempFirstArray[i] = Convert.ToString(rand.Next(minNumber, maxNumber));
  84.                 tempSecondArray[i] = Convert.ToString(rand.Next(minNumber, maxNumber));
  85.             }
  86.  
  87.             firstArray = tempFirstArray;
  88.             secondArray = tempSecondArray;
  89.         }
  90.  
  91.         static void SumNumbers(List<string> lines, string[] array)
  92.         {
  93.             for (int i = 0; i < array.Length; i++)
  94.             {
  95.                 if (lines.Contains(array[i]) == false)
  96.                 {
  97.                     lines.Add(array[i]);
  98.                 }
  99.             }
  100.         }
  101.  
  102.         static void OutputData(string[] array)
  103.         {
  104.             for (int i = 0; i < array.Length; i++)
  105.             {
  106.                 Console.Write($"{array[i]} ");
  107.             }
  108.         }
  109.  
  110.         static void OutputSum(List<string> lines)
  111.         {
  112.             Console.Write("Сумма данных - ");
  113.  
  114.             foreach (var line in lines)
  115.                 Console.Write($"{line} ");
  116.  
  117.             Console.WriteLine("\n");
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement