silvana1303

numbers array

Jun 16th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Threading;
  6.  
  7. namespace ConsoleApp1
  8. {
  9.     class Exam
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
  14.  
  15.             string[] command = Console.ReadLine().Split().ToArray();
  16.  
  17.             while (command[0] != "End")
  18.             {
  19.                 if (command[0] == "Switch")
  20.                 {
  21.                     Switch(numbers, command);
  22.                 }
  23.                 if (command[0] == "Change")
  24.                 {
  25.                     Change(numbers, command);
  26.                 }
  27.                 if (command[1] == "Negative")
  28.                 {
  29.                     Console.WriteLine(numbers.Where(x => x < 0).Sum());
  30.                 }
  31.                 if (command[1] == "Positive")
  32.                 {
  33.                     Console.WriteLine(numbers.Where(x => x > 0).Sum());
  34.                 }
  35.                 if (command[1] == "All")
  36.                 {
  37.                     Console.WriteLine(numbers.Sum());
  38.                 }
  39.  
  40.                 command = Console.ReadLine().Split().ToArray();
  41.             }
  42.  
  43.             numbers.RemoveAll(x => x < 0);
  44.  
  45.             Console.WriteLine(string.Join(" ", numbers));
  46.         }
  47.  
  48.         private static void Change(List<int> numbers, string[] command)
  49.         {
  50.             int index = int.Parse(command[1]);
  51.             int number = int.Parse(command[2]);
  52.  
  53.             if (index >= 0 && index < numbers.Count)
  54.             {
  55.                 numbers[index] = number;
  56.             }
  57.         }
  58.  
  59.         private static void Switch(List<int> numbers, string[] command)
  60.         {
  61.             int index = int.Parse(command[1]);
  62.  
  63.             if (index >= 0 && index < numbers.Count)
  64.             {
  65.                 numbers[index] *= -1;
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment