C# :武大GPA 这样算

本文介绍了一种使用C#编程语言实现的武大GPA计算方法。通过定义课程类(CS),并提供学分、成绩和绩点的属性,以及绩点和平均绩点的计算方法。同时,设计了一个读文件类(Read),用于从文本文件中读取课程信息并转换为课程数组,方便计算。最后,提供了使用示例,展示了如何读取成绩文件并计算平均绩点和平均成绩。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


此篇博客纯属娱乐,校友可以进来玩玩。- _<
武大GPA 计算方法:
在这里插入图片描述
话不多说,直接开整:

1. 新搞个课程类

	public class CS
    {
        public double x;  //学分   
        public double y;  //成绩
        public double z;  //绩点
        public double X
        { get { return x; } set { x = value; } }
        public double Y
        { get { return y; } set { y = value; } }
        public double Z
        { get { return z; } set { z = value; } }
        public CS(double x = 0, double y = 0, double z = 0)
        {//构造函数1,将一串数据传到一个 Point 中
            this.x = x;
            this.y = y;
            this.z = getZ(y);
        }
        public CS(string str)
        {//构造函数1,将一字符串传到一个 Point 中
            string[] sArry = str.Split(new string[] { " ", ",", ".","\t" }, StringSplitOptions.RemoveEmptyEntries);
            this.x = double.Parse(sArry[0]);
            this.y = double.Parse(sArry[1]);
            this.z = getZ(y);
        }
        public CS(string[] sArry)
        {//构造函数3,重载 Point函数,将一个字符串数组传到 Point 中
            this.x = double.Parse(sArry[0]);
            this.y = double.Parse(sArry[1]);
            this.z = getZ(y);
        }
        public static double getZ(double y)
        {
            double z=0;
            if (y >= 90) z = 4.0;
            else if (y >= 85) z = 3.7;
            else if (y >= 82) z = 3.3;
            else if (y >= 78) z = 3.0;
            else if (y >= 75) z = 2.7;
            else if (y >= 72) z = 2.3;
            else if (y >= 68) z = 2.0;
            else if (y >= 64) z = 1.5;
            else if (y >= 60) z = 1.0;
            else z = 0;
            return z;
        }
        public static double getAveZ(CS[] Cs)
        {//计算一个课程数组的平均绩点,保研用
            int n = Cs.Length;
            double xSum=0,xzSum=0,xzAve=0;
            for (int i = 0; i < n; i++)
            {
                CS cs=Cs[i];
                xSum += cs.x;
                xzSum += cs.x * cs.z;
            }
            xzAve = xzSum / xSum;
            return xzAve;
        }
        public static double getAveY(CS[] Cs)
        {//计算一个课程数组的平均成绩,评奖用
            int n = Cs.Length;
            double xSum = 0, xySum = 0, xyAve = 0;
            for (int i = 0; i < n; i++)
            {
                CS cs = Cs[i];
                xSum += cs.x;
                xySum += cs.x * cs.y;
            }
            xyAve = xySum / xSum;
            return xyAve;
        }     
    }

2.再搞个读文件类

	public class Read
    {
    	/*用sDelim 将str 分割成字符串数组
         * @sDelim 可选参数,分隔符
         */ 
        public static string[] Split(string str, string sDelim = null)
        {
            if(sDelim==null)//默认分割
                return str.Split(new string[] { " ", ",", "?", "->", "   ","\t" }, StringSplitOptions.RemoveEmptyEntries);
            else//根据传入参数分割
                return str.Split(new string[] { sDelim }, StringSplitOptions.RemoveEmptyEntries);
        }
        /*得到一个文本文件的行数
         */ 
        public static int getTextRows(string txt)
        {
            int n = 0;
            StreamReader sR = new StreamReader(txt);
            string str = sR.ReadLine();
            while (str != null)
            {
                if (Split(str) != null) n++; 
                str = sR.ReadLine(); 
            }
            sR.Close();
            return n;
        }
        /*读取txt至课程数组中
         */
        public static CS[] T2Cs(string txt)
        {
            int i = 0, n = getTextRows(txt);
            CS[] Cs = new CS[n];
            StreamReader sR = new StreamReader(txt);
            string str = sR.ReadLine();
            while (str != null)
            {
                string[] sArray = Split(str);
                if (sArray != null)
                {
                    CS cs = new CS(sArray);
                    Cs[i] = cs; i++;
                }
                str = sR.ReadLine();
            }
            sR.Close();
            return Cs;
        }
    }

3.使用示例

			string fcs = @"C:\Users\Lenovo 110\Desktop\笔记本\grade\专选.txt";
            CS[] Cs = Read.T2Cs(fcs);
            Console.WriteLine("{0},{1}", CS.getAveZ(Cs), CS.getAveY(Cs));
            Console.Read();

注意:成绩文件第一列为学分,第二列为成绩。推荐先用Excel 编辑后复制到txt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流浪猪头拯救地球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值