Advertisement
RamGaal

Homework 4 ex.7

May 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string[] massive = new string[0];
  12.             string vvod;
  13.             bool run = true;
  14.             Random rnd = new Random();
  15.             while (run)
  16.  
  17.             {
  18.                 Console.Write("Введите слово, комманду SHUFFLE для перемешивания или EXIT для выхода:");
  19.                 vvod = Console.ReadLine();
  20.                 if (vvod.ToLower() == "exit")
  21.                 {
  22.                     run = false;
  23.                 }
  24.                 if (vvod.ToLower() == "shuffle")
  25.                 {
  26.                     Shuffle(ref rnd,ref vvod,ref massive);
  27.                     Console.WriteLine("Элементы в массиве перемешаны!");
  28.                     for (int i = 0; i<massive.Length; i++)
  29.                     {
  30.                         Console.WriteLine(massive[i]);
  31.                     }
  32.                 }
  33.                 else
  34.                 {
  35.                     Add(ref vvod,ref massive);
  36.                     Console.WriteLine("В массиве теперь есть следующие элементы:");
  37.                     for (int i = 0; i < massive.Length; i++)
  38.                     {
  39.                         Console.WriteLine(massive[i]);
  40.                     }
  41.                 }
  42.             }
  43.         }
  44.  
  45.         public static string[] Add(ref string vvod,ref string[] massive)
  46.         {
  47.             string[] tempMassive = new string[massive.Length + 1];
  48.             for (int i = 0; i < massive.Length; i++)
  49.             {
  50.                 tempMassive[i] = massive[i];
  51.             }
  52.             tempMassive[massive.Length] = vvod;
  53.             massive = tempMassive;
  54.             return massive;
  55.         }
  56.  
  57.         public static string[] Shuffle(ref System.Random rnd,ref string vvod,ref string[] massive)
  58.         {
  59.             string[] tempMassive = new string[massive.Length];
  60.             tempMassive = massive;
  61.             for (int i = 0; i< massive.Length;i++)
  62.             {
  63.                 string tempMem = tempMassive[i];
  64.                 int seed = rnd.Next(0, massive.Length);
  65.                 tempMassive[i] = massive[seed];
  66.                 massive[seed] = tempMem;
  67.             }
  68.             tempMassive = massive;
  69.             return massive;
  70.         }
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement