N
N
NikaBestyjeva2020-05-30 15:32:45
C++ / C#
NikaBestyjeva, 2020-05-30 15:32:45

How to competently implement a structure named WORKER with the ability to view the list, delete an element by last name, insert it in an arbitrary place in the list?

Describe a structure named WORKER, with fields: last name, first name, position, year of employment. Form an array. Implement the ability to view the list, delete an element by last name, insert a new element in an arbitrary place in the list.
I DID NOT UNDERSTAND THE SUBJECT OF LISTS AND APPLICATION I don’t understand how to apply specifically in my case, in order to work with the list as in the task. Help, please.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public struct worker
    {
        public string fio;
        public string dol;
        public int god;
        public int staj;
    }
    class Program
    {
        static void Main(string[] args)
        {
            worker[] w = new worker[5];
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Введите данные {0}-го работника:", i + 1);
                Console.Write("Ф.И.О.: ");
                w[i].fio = Console.ReadLine();
                Console.Write("Должность: ");
                w[i].dol = Console.ReadLine();
                Console.Write("Год начала  работы: ");
                w[i].god = Convert.ToInt32(Console.ReadLine());
                w[i].staj = 2011 - w[i].god;
                Console.WriteLine("--------------------------------");
            }
           
       
            Console.ReadLine();
            Console.ReadKey();
            
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bitddeveloper, 2020-05-31
@NikaBestyjeva

You need to work with sheets or another collection
Something like this Now let's deal with deletion by name
List<Worker> workers = new List<Worker>();

public static void DeleteWorkerByName(List<Worker> workers, string name)
        {
            foreach (var worker in workers)
                if (worker.fio == name)
                    workers.Remove(worker);
        }
Перебираем всех сотрудников,и если окажется нужным,то удаляем
        public static void ShowAllWorkers(List<Worker> workers)
        {
            foreach (var worker in workers)
            {
                Console.WriteLine(worker.fio);
                Console.WriteLine(worker.dol);
                Console.WriteLine(worker.god);
                Console.WriteLine(worker.staj);
            }
        }
Этот же метод выводит на экран всех воркеров

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question