private static void AddElement(List <int> lst) { Console.WriteLine(); lst.Add(InputNumber("Ievadiet elementu!: ")); FileOperations.Write(lst); Console.WriteLine(); }
private static void AddElement(List <int> lst) { Console.WriteLine(); Console.WriteLine("Ievadiet elementu!"); try { int number = Convert.ToInt32(Console.ReadLine()); lst.Add(number); FileOperations.Write(lst); } catch { Console.WriteLine("Nepareiza ievade elements netika pievienots"); } }
static void Task1() { List <int> lst = FileOperations.Read(); String choice = " "; while (choice != "0") { Console.WriteLine("1- Pievienot"); Console.WriteLine("2- Dzēst"); Console.WriteLine("0- Iziet"); Console.WriteLine("3- Saskaitīt visus skaitļus"); Console.WriteLine("4 - izrēķināt vidējo aritmētisko"); choice = Console.ReadLine(); switch (choice) { case "1": AddElement(lst); break; case "2": DeleteElement(lst); break; case "0": FileOperations.Write(lst); break; case "3": Console.WriteLine(); Console.WriteLine(sumElements(lst)); Console.WriteLine(); break; case "4": Console.WriteLine(); Console.WriteLine(averageSum(lst)); Console.WriteLine(); break; default: Console.WriteLine("Nepareiza ievade"); break; } } }
private static void RemoveElement(List <int> lst) { Console.WriteLine(); PrintLst(lst); if (lst.Count == 0) { Console.WriteLine("Saraksts ir tukss!"); return; } Console.WriteLine("Kuru elementu velaties dzest?"); int toDelete = InputNumberBetween("Kuru elementu velaties dzest?: ", 0, lst.Count); lst.RemoveAt(toDelete - 1); FileOperations.Write(lst); Console.WriteLine(); }
private static void DeleteElement(List <int> lst) { if (lst.Count == 0) { Console.WriteLine("Saraksts ir tukšs!"); } else { Console.WriteLine("Ievadiet pozīciju, kuru gribat izdzēst!"); try { int pos = Convert.ToInt32(Console.ReadLine()); lst.RemoveAt(pos); FileOperations.Write(lst); } catch { Console.WriteLine("Nepareiza pozīcija!"); DeleteElement(lst); } } }
static void Task01() { List <int> values = FileOperations.Read(); String choice = ""; double sum = 0; double avg = 0; while (choice != "Exit") { Console.Write("Choose an action - Add, Show, Exit, Remove, Count, Average: "); choice = Console.ReadLine(); if (choice == "Add") { Console.Write("How many values would you like to add? : "); int amount = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < amount; i++) { try { Console.Write("Insert value [" + i + "]: "); values.Add(Convert.ToInt32(Console.ReadLine())); FileOperations.Write(values); } catch { Console.WriteLine("Wrong input"); i--; } } } else if (choice == "Show") { if (values.Count == 0) { Console.WriteLine("List contains no values!"); } for (int i = 0; i < values.Count; i++) { Console.WriteLine("Value [" + i + "] is " + values[i]); } } else if (choice == "Exit") { break; } else if (choice == "Remove") { Console.Write("Which index would you like to remove? : "); int remove = Convert.ToInt32(Console.ReadLine()); try { values.RemoveAt(remove); Console.WriteLine(); } catch { Console.WriteLine("Wrong input"); } FileOperations.Write(values); } else if (choice == "Count") { for (int i = 0; i < values.Count; i++) { sum += values[i]; } Console.Write("Sum of all values is: " + sum); Console.WriteLine(); sum = 0; } else if (choice == "Average") { for (int i = 0; i < values.Count; i++) { sum += values[i]; if (i < values.Count) { avg = sum / values.Count; } } Console.WriteLine("Average of all index values is: " + avg); avg = 0; sum = 0; } } }