7-5 学生管理系统

实现学生小型管理系统

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

class Student
{
    // 使用 readonly 关键字使得子段只能在构造函数中被初始化, 之后不再修改
    public readonly string sno;
    public readonly string name;
    public readonly int chinese;
    public readonly int math;
    public readonly int english;
    public readonly int sumScore;

    public Student(string sno, string name, int chinese, int math, int english)
    {
        this.sno = sno;
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
        this.sumScore = chinese + math + english;
    }

}

class SchoolClass
{
    private List<Student> students; // 存储学生信息

    public SchoolClass(List<Student> students)
    {
        this.students = students;
    }

    public void SumScore(string sno) // 输出学号为 sno 的学生的总成绩
    {
        var student = students.Find(s => s.sno == sno);
        if (student == null)
        {
            Console.WriteLine("No student.");
        }
        else
        {
            Console.WriteLine($"SumScore={student.sumScore}");
        }
    }


    public void AvgScore() // 计算所有学生每科的总成绩
    {
        double avgChinese = students.Average(s => s.chinese);
        double avgMath = students.Average(s => s.math);
        double avgEnglish = students.Average(s => s.english);
        Console.WriteLine($"AvgChinese={avgChinese};AvgMath={avgMath};AvgEnglish={avgEnglish}");
    }

    public void MaxScore() // 计算所有学生单科的最大成绩
    {
        int maxChinese = students.Max(s => s.chinese);
        int maxMath = students.Max(s => s.math);
        int maxEnglish = students.Max(s => s.english);
        Console.WriteLine($"MaxChinese={maxChinese};MaxMath={maxMath};MaxEnglish={maxEnglish}");
    }

    public void Fail(string cname)
    {
        List<string> failedStudents = new List<string>();
        foreach (var student in students)
        {
            switch (cname)
            {
                case "chinese":
                    if (student.chinese < 60)
                        failedStudents.Add(student.name);
                    break;
                case "math":
                    if (student.math < 60)
                        failedStudents.Add(student.name);
                    break;
                case "english":
                    if (student.english < 60)
                        failedStudents.Add(student.name);
                    break;
                default:
                    Console.WriteLine("Inputting illegal characters.");
                    return;
            }
        }
        Console.WriteLine(string.Join(";", failedStudents));
    }

    public void Paragraph(string cname)
    {
        int numStudents = students.Count;
        int num90Above = students.Count(s => GetScore(s, cname) >= 90);
        int num80To90 = students.Count(s => GetScore(s, cname) >= 80 && GetScore(s, cname) < 90);
        int num70To80 = students.Count(s => GetScore(s, cname) >= 70 && GetScore(s, cname) < 80);
        int num60To70 = students.Count(s => GetScore(s, cname) >= 60 && GetScore(s, cname) < 70);
        int numBelow60 = students.Count(s => GetScore(s, cname) < 60);

        double percentage90Above = (double)num90Above / numStudents * 100;
        double percentage80To90 = (double)num80To90 / numStudents * 100;
        double percentage70To80 = (double)num70To80 / numStudents * 100;
        double percentage60To70 = (double)num60To70 / numStudents * 100;
        double percentageBelow60 = (double)numBelow60 / numStudents * 100;

        Console.WriteLine($"{percentage90Above:F0}% {percentage80To90:F0}% {percentage70To80:F0}% {percentage60To70:F0}% {percentageBelow60:F0}%");
    }

    public void TopThree()
    {
        var sortedStudents = students.OrderByDescending(s => s.sumScore).ThenBy(s => students.IndexOf(s)).ToList();
        List<string> topThree = new List<string>();

        for (int i = 0; i < Math.Min(3, sortedStudents.Count); i++)
        {
            topThree.Add(sortedStudents[i].name);
        }

        for (int i = 3; i < sortedStudents.Count; i++)
        {
            if (sortedStudents[i].sumScore == sortedStudents[2].sumScore)
            {
                topThree.Add(sortedStudents[i].name);
            }
            else
            {
                break;
            }
        }

        Console.WriteLine(string.Join(";", topThree));
    }

    private int GetScore(Student student, string cname)
    {
        switch (cname)
        {
            case "chinese":
                return student.chinese;
            case "math":
                return student.math;
            case "english":
                return student.english;
            default:
                throw new ArgumentException("Invalid course name");
        }
    }

}

class SolutionOf7_5
{
    static void Main(string[] args)
    {
        int n = int.Parse(Console.ReadLine());
        if (n <= 0)
        {
            Console.WriteLine("Inputting illegal characters.");
        }
        else
        {
            List<Student> students = new List<Student>();
            for (int i = 0; i < n; i++)
            {
                string[] input = Console.ReadLine().Split();
                string sno = input[0];
                string name = input[1];
                int chinese = int.Parse(input[2]);
                int math = int.Parse(input[3]);
                int english = int.Parse(input[4]);
                students.Add(new Student(sno, name, chinese, math, english));
            }

            SchoolClass schoolClass = new SchoolClass(students);

            string option = Console.ReadLine();
            switch (option[0])
            {
                case '1':
                    string sno = option.Split()[1];
                    schoolClass.SumScore(sno);
                    break;
                case '2':
                    schoolClass.AvgScore();
                    break;
                case '3':
                    schoolClass.MaxScore();
                    break;
                case '4':
                    string cname = option.Split()[1];
                    schoolClass.Fail(cname);
                    break;
                case '5':
                    cname = option.Split()[1];
                    if (cname != "chinese" && cname != "math" && cname != "english")
                    {
                        Console.WriteLine("Inputting illegal characters.");
                        break;
                    }
                    schoolClass.Paragraph(cname);
                    break;
                case '6':
                    schoolClass.TopThree();
                    break;
                default:

                    Console.WriteLine("Inputting illegal characters.");
                    break;
            }
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值