此篇博客纯属娱乐,校友可以进来玩玩。- _<
武大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
中