数据结构使用 C# [关闭]

问题描述 投票:0回答:0

嗨,这是我的作业,我需要帮助

您必须使用 C# 定义数据结构。数据结构应至少包含字段 在下一点提到。此外,您必须定义任何其他函数、属性、事件和 您认为有必要的例外情况。你必须使用数组或链表。

数据结构: a) 名称:CustomDataList b) 数据项:Student {string FirstName, string LastName, string StudentNumber, float AverageScore} 数据结构属性: a) 长度(项数) b) 首先 c) 最后 方法: a)添加(元素) b) 获取元素--> 索引 c) RemoveByIndex -->索引 d) 先移除 e) 删除最后一个 f) DisplayList——显示数组的所有元素 g) LoadFromFile_name——假设每个项目的数据都在文本文件中的不同行上

有人能告诉我应该如何编写这段代码吗?

using System;
using System.IO;

public class Student
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string StudentNumber { get; set; }
    public float AverageScore { get; set; }

    public override string ToString()
    {
        return $"{FirstName} {LastName} ({StudentNumber}): {AverageScore}";
    }
}

public class CustomDataList
{
    private Student[] _dataList;
    private int _length;

    public CustomDataList(int capacity = 10)
    {
        _dataList = new Student[capacity];
        _length = 0;
    }

    public int Length => _length;

    public Student First => _length > 0 ? _dataList[0] : null;

    public Student Last => _length > 0 ? _dataList[_length - 1] : null;

    public void Add(Student element)
    {
        if (_length == _dataList.Length)
        {
            Array.Resize(ref _dataList, _dataList.Length * 2);
        }
        _dataList[_length] = element;
        _length++;
    }

    public Student GetElement(int index)
    {
        if (index >= 0 && index < _length)
        {
            return _dataList[index];
        }
        else
        {
            throw new IndexOutOfRangeException();
        }
    }

    public void RemoveByIndex(int index)
    {
        if (index >= 0 && index < _length)
        {
            for (int i = index; i < _length - 1; i++)
            {
                _dataList[i] = _dataList[i + 1];
            }
            _length--;
        }
        else
        {
            throw new IndexOutOfRangeException();
        }
    }

    public void RemoveFirst()
    {
        RemoveByIndex(0);
    }

    public void RemoveLast()
    {
        RemoveByIndex(_length - 1);
    }

    public void DisplayList()
    {
        Console.WriteLine("CustomDataList contents:");
        for (int i = 0; i < _length; i++)
        {
            Console.WriteLine(_dataList[i].ToString());
        }
    }

    public void LoadFromFile(string file_name)
    {
        using (StreamReader sr = new StreamReader(file_name))
        {
            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();
                string[] tokens = line.Split(',');
                if (tokens.Length == 4)
                {
                    Student s = new Student();
                    s.FirstName = tokens[0].Trim();
                    s.LastName = tokens[1].Trim();
                    s.StudentNumber = tokens[2].Trim();
                    s.AverageScore = float.Parse(tokens[3].Trim());
                    Add(s);
                }
            }
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // create a new CustomDataList object
        CustomDataList dataList = new CustomDataList();

        // load data from a file
        dataList.LoadFromFile("students.txt");

        // display the list contents
        Console.WriteLine("Initial list:");
        dataList.DisplayList();

        // add a new student
        Student newStudent = new Student { FirstName = "John", LastName = "Doe", StudentNumber = "123456", AverageScore = 85.0f };
        dataList.Add(newStudent);

        // display the list contents
        Console.WriteLine("After adding a student:");
        dataList.DisplayList();

        // remove the first student
        dataList.RemoveFirst();

        // display the list contents
        Console.WriteLine("After removing the first student:");
        dataList.DisplayList();

        // remove a student by index
        dataList.RemoveByIndex(2);

        // display the list contents
        Console.WriteLine("After removing a student by index:");
        dataList.DisplayList();

        // get a student by index
        int index = 1;
        Student student = dataList.GetElement(index);
        Console.WriteLine($"Student at index {index}: {student}");

        // display the first and last students
        Console.WriteLine($"First student: {dataList.First}");
        Console.WriteLine($"Last student: {dataList.Last}");

        Console.ReadKey();
    }
}
c# data-structures custom-data-attribute datalist
© www.soinside.com 2019 - 2024. All rights reserved.