Advertisement
RamGaal

Homework 4 ex.6

May 23rd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.22 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. namespace ConsoleApp9
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.  
  14.             string[] weapons = new string[0];
  15.             string[] bag = new string[0];
  16.             bool run = true;
  17.             string vvod = " ";
  18.             string[] command = new string[0];
  19.  
  20.             Console.WriteLine("Введите ADD перед названием оружия, чтобы добавить его в сумку или DEL чтобы удалить из сумки" +
  21.     "\nВведите LIST чтобы просмотреть содержимое сумки");
  22.             while (run)
  23.             {
  24.                 vvod = Console.ReadLine();
  25.  
  26.                     command = vvod.Split(' ');
  27.                     switch (command[0].ToLower())
  28.                     {
  29.                         case "add":
  30.                             AddWeapon(ref command, ref weapons);
  31.                             break;
  32.                         case "del":
  33.                             DelWeapons(ref command,ref  weapons);
  34.                             break;
  35.                         case "list":
  36.                             ListBag(ref bag, ref weapons);
  37.                             break;
  38.                         case "exit":
  39.                             run = false;
  40.                             break;
  41.                         default:
  42.                             Console.WriteLine("Невозможно выполнить операцию! Для выхода наберите EXIT.");
  43.                             break;
  44.                     }
  45.             }
  46.             Console.ReadKey();
  47.         }
  48.         public static string [] AddWeapon(ref string [] command, ref string [] weapons)
  49.         {
  50.             int contain = -1;
  51.             for (int j = 0; j < weapons.Length; j++)
  52.             {
  53.                 if (weapons[j].ToLower() == command[1].ToLower())
  54.                 {
  55.                     contain = j;
  56.                 }
  57.             }
  58.  
  59.             if (contain == -1)
  60.             {
  61.                 string[] tempweapons = new string[weapons.Length + 1];
  62.                 for (int i = 0; i < weapons.Length; i++)
  63.                 {
  64.                     tempweapons[i] = weapons[i];
  65.                 }
  66.                 tempweapons[weapons.Length] = command[1];
  67.                 weapons = tempweapons;
  68.                 Console.WriteLine("Оружие " + weapons[weapons.Length-1] + " добавлено в сумку!");
  69.             }
  70.             else
  71.             {
  72.                 Console.WriteLine("Такоe оружие уже есть в сумке!");
  73.             }
  74.            
  75.             return weapons;
  76.         }
  77.  
  78.         public static string[] DelWeapons(ref string[] command, ref string[] weapons)
  79.         {
  80.             int contain = -1;
  81.             for (int j = 0; j < weapons.Length; j++)
  82.             {
  83.                 if (weapons[j].ToLower() == command[1].ToLower())
  84.                 {
  85.                     contain = j;
  86.                 }
  87.             }
  88.  
  89.             if (contain == -1)
  90.             {
  91.                 Console.WriteLine("Такого оружия нет в сумке!");
  92.             }
  93.             else
  94.             {
  95.                 Console.WriteLine("Оружие " + weapons[contain] + " удалено из сумки!");
  96.                 for (int i = contain; i < weapons.Length - 1; i++)
  97.                 {
  98.                     weapons[i] = weapons[i + 1];
  99.                 }
  100.  
  101.                 string[] tempweapons = new string[weapons.Length - 1];
  102.                 for (int i = 0; i < tempweapons.Length; i++)
  103.                 {
  104.                     tempweapons[i] = weapons[i];
  105.                 }
  106.                 weapons = tempweapons;
  107.             }
  108.  
  109.             return weapons;
  110.  
  111.         }
  112.         public static string[] ListBag(ref string[] bag, ref string[] weapons)
  113.         {
  114.             Console.WriteLine("В сумке лежит оружие:");
  115.             bag = weapons;
  116.             for (int i = 0; i < bag.Length; i++)
  117.             {
  118.                 Console.WriteLine(i + 1 + "." + bag[i]);
  119.             }
  120.             return bag;
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement