alexbancheva

Number_Array_MidExam_30thOfJune2019

Apr 13th, 2020
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Number_Array_MidExam_30thOfJune2019
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] input = Console.ReadLine()
  11.                 .Split()
  12.                 .Select(int.Parse)
  13.                 .ToArray();
  14.  
  15.             string command = Console.ReadLine();
  16.  
  17.             while (command != "End")
  18.             {
  19.                
  20.                 string[] splitedCommand = command.Split(" ");
  21.                 string commandOne = splitedCommand[0];
  22.                 int sumNegative = 0;
  23.                 int sumPositive = 0;
  24.                 int sumOfAll = 0;
  25.  
  26.                 if (commandOne == "Switch")
  27.                 {
  28.                     int index = int.Parse(splitedCommand[1]);
  29.  
  30.                     for (int i = 0; i < input.Length; i++)
  31.                     {
  32.                         if (i == index)
  33.                         {
  34.                             input[i] *= -1;
  35.                         }
  36.                     }
  37.                 }
  38.                 else if (commandOne == "Change")
  39.                 {
  40.                     int index = int.Parse(splitedCommand[1]);
  41.                     int value = int.Parse(splitedCommand[2]);
  42.  
  43.                     for (int i = 0; i < input.Length; i++)
  44.                     {
  45.                         if (i == index)
  46.                         {
  47.                             input[i] = value;
  48.                         }
  49.                     }
  50.                 }
  51.                 else if (commandOne == "Sum")
  52.                 {
  53.                     string secretCommand = splitedCommand[1];
  54.  
  55.                     if (secretCommand == "Negative")
  56.                     {
  57.                         // int sumNegative = 0;
  58.  
  59.                         for (int i = 0; i < input.Length; i++)
  60.                         {
  61.                             if (input[i] < 0)
  62.                             {
  63.                                 sumNegative += input[i];
  64.                             }
  65.                         }
  66.  
  67.                         Console.WriteLine(sumNegative);
  68.                     }
  69.                     else if (secretCommand == "Positive")
  70.                     {
  71.                       //  int sumPositive = 0;
  72.  
  73.                         for (int i = 0; i < input.Length; i++)
  74.                         {
  75.                             if (input[i] >= 0)
  76.                             {
  77.                                 sumPositive += input[i];
  78.                             }
  79.                         }
  80.  
  81.                         Console.WriteLine(sumPositive);
  82.                     }
  83.                     else if (secretCommand == "All")
  84.                     {
  85.                        // int sumOfAll = 0;
  86.  
  87.                         for (int i = 0; i < input.Length; i++)
  88.                         {
  89.                             sumOfAll += input[i];
  90.                         }
  91.  
  92.                         Console.WriteLine(sumOfAll);
  93.                     }
  94.                 }
  95.  
  96.                 command = Console.ReadLine();
  97.             }
  98.  
  99.             if (command == "End")
  100.             {
  101.                 //for (int i = 0; i < input.Length; i++)
  102.                 //{
  103.                 //    Console.WriteLine(string.Join(" ", input[i]+ " "));
  104.                 //}
  105.  
  106.                 foreach (int num in input)
  107.                 {
  108.                     if (num >= 0)
  109.                     {
  110.                         Console.Write(num + " ");
  111.                     }
  112.                 }
  113.             }
  114.  
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment