Teo_Yanchev

C# Fundamentals - 02. NumberArray - MidExam 30.06.2020 Group 1

Oct 8th, 2020 (edited)
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5.  
  6. namespace _02._NumberArray
  7. {
  8. class NumberArray
  9. {
  10. static void Main(string[] args)
  11. {
  12. List<int> numbers = Console.ReadLine()
  13. .Split()
  14. .Select(int.Parse)
  15. .ToList();
  16.  
  17. string command = Console.ReadLine();
  18. while (command != "End")
  19. {
  20. string[] currentCommands = command.Split();
  21. string instruction = currentCommands[0];
  22.  
  23. if (instruction == "Switch")
  24. {
  25. int index = int.Parse(currentCommands[1]);
  26.  
  27. if (0 <= index && index < numbers.Count)
  28. {
  29. int replace = numbers[index];
  30.  
  31. int result = replace * -1;
  32.  
  33. numbers.RemoveAt(index);
  34. numbers.Insert(index, result);
  35. }
  36.  
  37. command = Console.ReadLine();
  38. continue;
  39. }
  40.  
  41. else if (instruction == "Change")
  42. {
  43. int index = int.Parse(currentCommands[1]);
  44. int value = int.Parse(currentCommands[2]);
  45.  
  46. if (0 <= index && index < numbers.Count)
  47. {
  48. numbers.RemoveAt(index);
  49. numbers.Insert(index, value);
  50. }
  51.  
  52. command = Console.ReadLine();
  53. continue;
  54. }
  55.  
  56. else if (instruction == "Sum")
  57. {
  58. string options = currentCommands[1];
  59.  
  60. if (options == "Negative")
  61. {
  62. int sum = 0;
  63.  
  64. for (int i = 0; i < numbers.Count; i++)
  65. {
  66. if (numbers[i] < 0)
  67. {
  68. sum += numbers[i];
  69. }
  70. }
  71. Console.WriteLine(sum);
  72. }
  73.  
  74. else if (options == "Positive")
  75. {
  76. int sum = 0;
  77.  
  78.  
  79. for (int i = 0; i < numbers.Count; i++)
  80. {
  81. if (0 < numbers[i])
  82. {
  83. sum += numbers[i];
  84.  
  85. }
  86. }
  87.  
  88. Console.WriteLine(sum);
  89. }
  90.  
  91. else if (options == "All")
  92. {
  93. Console.WriteLine(numbers.Sum());
  94. }
  95.  
  96. command = Console.ReadLine();
  97. continue;
  98. }
  99. }
  100.  
  101. for (int i = 0; i < numbers.Count; i++)
  102. {
  103. if (0 <= numbers[i])
  104. {
  105. Console.Write($"{numbers[i]} ");
  106. }
  107. }
  108. }
  109. }
  110. }
  111.  
Add Comment
Please, Sign In to add comment