1. 程式人生 > >【C#作業】學生成績新增並排序,錯誤則丟擲異常

【C#作業】學生成績新增並排序,錯誤則丟擲異常

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

namespace ConsoleApp12
{
    public 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;
        }
    }
    public class ChineseCompare : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return y.Chinese.CompareTo(x.Chinese);
        }
    }
    class IDException : Exception
    {
        public IDException(string str)
        {
            Console.WriteLine(str);
        }
    }
    class Program
    {
        static List<Student> L1 = new List<Student>();
        static void Main(string[] args)
        {
            ////預設資料
            string[] s = new string[5];
            string str = "";
            StreamReader sr1 = new StreamReader("input.txt");
            while ((str = sr1.ReadLine()) != null)
            {
                s = str.Split('\t');
                L1.Add(new Student(s));
            }
            sr1.Close();

            while (true)
            {
                Console.WriteLine("------------------------------------");
                Console.WriteLine("1.顯示所有學生");
                Console.WriteLine("2.對成績進行排序");
                Console.WriteLine("3.新增學生");
                Console.WriteLine("4.退出");
                Console.Write("請選擇要進行的操作:");
                int choice = 0;
                try
                {
                    choice = int.Parse(Console.ReadLine());
                }
                catch (Exception)
                {
                    Console.WriteLine("輸入的序號錯誤,請重新輸入!");
                    continue;
                }
                switch (choice)
                {
                    case 1:
                        Console.WriteLine("學號\t姓名\t語文\t英語\t數學\t總分");
                        foreach (Student stu in L1)
                        {
                            Console.WriteLine(stu);
                        }
                        break;
                    case 2:
                        char choice2=' ';
                        Console.WriteLine("請選擇排序的標準:a.總分\tb.語文");
                        try
                        {
                            choice2 = Convert.ToChar(Console.ReadLine());
                        }catch (Exception)
                        {
                            Console.WriteLine("輸入有誤,請重新操作!");
                        }
                        if (choice2 == 'a')
                        {
                            L1.Sort();
                            Console.WriteLine("------------------------根據總分排序------------------------");
                            Console.WriteLine("學號\t姓名\t語文\t英語\t數學\t總分");

                        }
                        else if (choice2=='b')
                        {
                            L1.Sort(new ChineseCompare());
                            Console.WriteLine("------------------------根據語文成績排序------------------------");
                            Console.WriteLine("學號\t姓名\t語文\t英語\t數學\t總分");
                        }
                        else
                        {
                            Console.WriteLine("輸入有誤,請重新操作!");
                            break;
                        }
                        foreach (Student stu in L1)
                        {
                            Console.WriteLine(stu);
                        }
                        break;
                    case 3:
                        Console.WriteLine("請按照\"學號\\t姓名\\t語文\\t英語\\t數學\"這樣的格式輸入需要新增的學生資訊");
                        try
                        {
                            string strInput = Console.ReadLine();
                            Add(strInput);
                        }catch(IDException e)
                        {
                            Console.WriteLine(e.Message);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("輸入有誤,請重新操作!");
                        }
                        break;
                    case 4:
                        System.Environment.Exit(0);break;
                    default:
                        Console.WriteLine("請輸入對應的序號!");
                        break;
                }

            }
        }
        static void Add(string str)
        {
            string[] s = new string[5];
            //while ((str = Console.ReadLine()) != "#")
            //{
                s = str.Split('\t');
                Checked(s[0]);
                L1.Add(new Student(s));
           // }
        }
        static void Checked(string s)
        {
            for(int i = 0; i < L1.Count; i++)
            {
                if (L1[i].StuNum.Equals(s))
                {
                    throw new IDException("待新增的學生學號已存在!");
                }
            }
        }
    }
}