Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- namespace _02._NumberArray
- {
- class NumberArray
- {
- static void Main(string[] args)
- {
- List<int> numbers = Console.ReadLine()
- .Split()
- .Select(int.Parse)
- .ToList();
- string command = Console.ReadLine();
- while (command != "End")
- {
- string[] currentCommands = command.Split();
- string instruction = currentCommands[0];
- if (instruction == "Switch")
- {
- int index = int.Parse(currentCommands[1]);
- if (0 <= index && index < numbers.Count)
- {
- int replace = numbers[index];
- int result = replace * -1;
- numbers.RemoveAt(index);
- numbers.Insert(index, result);
- }
- command = Console.ReadLine();
- continue;
- }
- else if (instruction == "Change")
- {
- int index = int.Parse(currentCommands[1]);
- int value = int.Parse(currentCommands[2]);
- if (0 <= index && index < numbers.Count)
- {
- numbers.RemoveAt(index);
- numbers.Insert(index, value);
- }
- command = Console.ReadLine();
- continue;
- }
- else if (instruction == "Sum")
- {
- string options = currentCommands[1];
- if (options == "Negative")
- {
- int sum = 0;
- for (int i = 0; i < numbers.Count; i++)
- {
- if (numbers[i] < 0)
- {
- sum += numbers[i];
- }
- }
- Console.WriteLine(sum);
- }
- else if (options == "Positive")
- {
- int sum = 0;
- for (int i = 0; i < numbers.Count; i++)
- {
- if (0 < numbers[i])
- {
- sum += numbers[i];
- }
- }
- Console.WriteLine(sum);
- }
- else if (options == "All")
- {
- Console.WriteLine(numbers.Sum());
- }
- command = Console.ReadLine();
- continue;
- }
- }
- for (int i = 0; i < numbers.Count; i++)
- {
- if (0 <= numbers[i])
- {
- Console.Write($"{numbers[i]} ");
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment