论坛里某朋友提出一首题,如下:
輸入同學人數(最多15人): 10
輸入第1位同學小考成績: 80, 70, 60
輸入第2位同學小考成績: 60, 70, 60
輸入第3位同學小考成績: 80, 70, 90
...
輸入第10位同學小考成績: 80, 75, 90
====================================
第1位同學平均: 73
第2位同學平均: 63
第3位同學平均: 80
...
第10位同學平均: 82
====================================
第1高分為第10位同學
第2高分為第3位同學
第3高分為第1位同學
这应该是一个控制台的应用程序了,自己初看起来觉得蛮简单的,但是做起来才知道郁闷,本以为十分钟搞掂的,结果弄了一晚上,下面是我自己写的程序,基本功能是应该实现了
using
System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestApplication
{
class Program
{
static List < Student > stus = new List < Student > (); // 存储学生的索引及其平均分
/// <summary>
/// 程序入口
/// </summary>
static void Main( string [] args)
{
Console.Write( " 输入同学人数(最多15人): " );
string num = Console.ReadLine();
int count;
if ( int .TryParse(num, out count))
{
if (count < 1 || count > 15 )
{
Console.WriteLine( " 请输入1~15之间的数字,程序退出! " );
Console.ReadLine();
return ;
}
string [] scours = new string [count]; // 存储每位同学的成绩字符串,如:80, 70, 60
// 循环输入成绩
for ( int i = 0 ; i < count; i ++ )
{
Console.Write( " 输入第 " + (i + 1 ) + " 位同学小考成绩: " );
scours[i] = Console.ReadLine();
}
Console.WriteLine( " =============================================== " );
// 循环输出平均成绩
for ( int i = 0 ; i < count; i ++ )
{
Console.WriteLine( " 第 " + (i + 1 ) + " 位同学平均: " + CalcAvg(scours[i], i + 1 ));
}
Console.WriteLine( " =============================================== " );
// 输出分数排名
StudentComparer sc = new StudentComparer();
stus.Sort(sc);
for ( int i = 0 ; i < stus.Count; i ++ )
{
Console.WriteLine( " 第 " + (i + 1 ) + " 高分为第 " + stus[i].Index + " 位同学: " + stus[i].Avg);
}
}
else
{
Console.WriteLine( " 格式输入错误,请输入数字,程序退出! " );
}
Console.Read();
}
/// <summary> 计算成绩字符串的平均值
///
/// </summary>
/// <param name="p"> 成绩字符串,如:80,70,50 </param>
/// <param name="index"> 该同学的索引 </param>
/// <returns></returns>
private static string CalcAvg( string p, int index)
{
string [] ss = p.Split( new char [] { ' , ' });
if (ss.Length == 0 )
{
return " 成绩输入错误,请按“80,70,50”的格式输入! " ;
}
else
{
int i = 0 ;
float total = 0f;
foreach ( string str in ss)
{
float f;
if ( float .TryParse(str, out f))
{
i ++ ;
total += f;
}
else
{
return " 成绩输入错误,请按“80,70,50”的格式输入! " ;
}
}
float avg = total / i; // 平均分
stus.Add( new Student(index, avg)); // 把当前同学的平均分及其索引加到List中
return avg.ToString();
}
}
}
/// <summary>
/// 学生实体类
/// </summary>
public class Student
{
private int _index;
/// <summary>
/// 索引
/// </summary>
public int Index
{
get { return _index; }
set { _index = value; }
}
private float _avg;
/// <summary>
/// 平均分
/// </summary>
public float Avg
{
get { return _avg; }
set { _avg = value; }
}
public Student( int index, float avg)
{
this ._index = index;
this ._avg = avg;
}
}
/// <summary>
/// 学生实体类的比较,按学生的平均分从高到低排列
/// </summary>
public class StudentComparer : IComparer < Student >
{
public int Compare(Student x, Student y)
{
if (x == null )
{
if (y == null )
{
// 如果二者都为空,那么他们则是相等的.
return 0 ;
}
else
{
// 如果X为空而Y不为空,则Y大
return - 1 ;
}
}
else
{
if (y == null )
// 如果X不为空而Y为空,则X大
{
return 1 ;
}
else
{
// 二者都不为空,则比较其中的avg属性
float x_avg = x.Avg;
float y_avg = y.Avg;
return y_avg.CompareTo(x_avg); // 此为从大到小,若是 x_avg.CompareTo(y_avg)则是从小到大
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestApplication
{
class Program
{
static List < Student > stus = new List < Student > (); // 存储学生的索引及其平均分
/// <summary>
/// 程序入口
/// </summary>
static void Main( string [] args)
{
Console.Write( " 输入同学人数(最多15人): " );
string num = Console.ReadLine();
int count;
if ( int .TryParse(num, out count))
{
if (count < 1 || count > 15 )
{
Console.WriteLine( " 请输入1~15之间的数字,程序退出! " );
Console.ReadLine();
return ;
}
string [] scours = new string [count]; // 存储每位同学的成绩字符串,如:80, 70, 60
// 循环输入成绩
for ( int i = 0 ; i < count; i ++ )
{
Console.Write( " 输入第 " + (i + 1 ) + " 位同学小考成绩: " );
scours[i] = Console.ReadLine();
}
Console.WriteLine( " =============================================== " );
// 循环输出平均成绩
for ( int i = 0 ; i < count; i ++ )
{
Console.WriteLine( " 第 " + (i + 1 ) + " 位同学平均: " + CalcAvg(scours[i], i + 1 ));
}
Console.WriteLine( " =============================================== " );
// 输出分数排名
StudentComparer sc = new StudentComparer();
stus.Sort(sc);
for ( int i = 0 ; i < stus.Count; i ++ )
{
Console.WriteLine( " 第 " + (i + 1 ) + " 高分为第 " + stus[i].Index + " 位同学: " + stus[i].Avg);
}
}
else
{
Console.WriteLine( " 格式输入错误,请输入数字,程序退出! " );
}
Console.Read();
}
/// <summary> 计算成绩字符串的平均值
///
/// </summary>
/// <param name="p"> 成绩字符串,如:80,70,50 </param>
/// <param name="index"> 该同学的索引 </param>
/// <returns></returns>
private static string CalcAvg( string p, int index)
{
string [] ss = p.Split( new char [] { ' , ' });
if (ss.Length == 0 )
{
return " 成绩输入错误,请按“80,70,50”的格式输入! " ;
}
else
{
int i = 0 ;
float total = 0f;
foreach ( string str in ss)
{
float f;
if ( float .TryParse(str, out f))
{
i ++ ;
total += f;
}
else
{
return " 成绩输入错误,请按“80,70,50”的格式输入! " ;
}
}
float avg = total / i; // 平均分
stus.Add( new Student(index, avg)); // 把当前同学的平均分及其索引加到List中
return avg.ToString();
}
}
}
/// <summary>
/// 学生实体类
/// </summary>
public class Student
{
private int _index;
/// <summary>
/// 索引
/// </summary>
public int Index
{
get { return _index; }
set { _index = value; }
}
private float _avg;
/// <summary>
/// 平均分
/// </summary>
public float Avg
{
get { return _avg; }
set { _avg = value; }
}
public Student( int index, float avg)
{
this ._index = index;
this ._avg = avg;
}
}
/// <summary>
/// 学生实体类的比较,按学生的平均分从高到低排列
/// </summary>
public class StudentComparer : IComparer < Student >
{
public int Compare(Student x, Student y)
{
if (x == null )
{
if (y == null )
{
// 如果二者都为空,那么他们则是相等的.
return 0 ;
}
else
{
// 如果X为空而Y不为空,则Y大
return - 1 ;
}
}
else
{
if (y == null )
// 如果X不为空而Y为空,则X大
{
return 1 ;
}
else
{
// 二者都不为空,则比较其中的avg属性
float x_avg = x.Avg;
float y_avg = y.Avg;
return y_avg.CompareTo(x_avg); // 此为从大到小,若是 x_avg.CompareTo(y_avg)则是从小到大
}
}
}
}
}