1. 程式人生 > >C#作業 列表排序

C#作業 列表排序

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

namespace ConsoleApp11列表排序
{
    class Student : IComparable<Student>
    {
        public string StuNum;
        public string Name;
        public float Chinese;
        public float English;
        public float Math;
        public float Tot;

        public Student(string[] s)
        {
            StuNum = s[0];
            Name = s[1];
            Chinese = float.Parse(s[2]);
            English = float.Parse(s[3]);
            Math = float.Parse(s[4]);
            Tot = Chinese + English + Math;
        }
        public int CompareTo(Student other)
        {
            return (int)(other.Tot - this.Tot);
        }
        public override string ToString()
        {
            return StuNum + "\t" + Name + "\t" + Chinese + "\t" + English + "\t" + Math + "\t" + Tot;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = new string[5];
            string str="";
            List<Student> L1 = new List<Student>();
            while ((str = Console.ReadLine()) != "")
            {
                s = str.Split('\t');
                L1.Add(new Student(s));
            }
            L1.Sort();
            Console.WriteLine("------------------------總分排名------------------------");
            Console.WriteLine("學號\t姓名\t語文\t英語\t數學\t總分");
            foreach (Student stu in L1)
            {
                Console.WriteLine(stu);
            }

            Console.ReadLine();
        }
    }
}