黑马程序员技术交流社区
标题:
第九期的亲们让我们一起来完成这个程序吧@!
[打印本页]
作者:
§風過無痕§
时间:
2013-7-18 21:59
标题:
第九期的亲们让我们一起来完成这个程序吧@!
本帖最后由 §風過無痕§ 于 2013-7-27 11:34 编辑
自学C#基础有一段时间了 总感觉质量与效率都不咋滴!基本原因嘛 个人觉得平常写的都是那些最基本的代码 缺乏笼统性知识的代码这种程序吧!
第二嘛!我觉得我们第九期的版块活跃度一直都比较低!为了把气氛给带起来且同时想通过这个程序对所学知识的一个巩固,特邀请第九期的马友们共同完成这个程序! ! !谢谢!
这是个最简单的仿学生管理系统
把我们第九期当做一个团队实行分工合作方式共同来完成它。
马友们随时都可以在里面增加有意义的模块
每楼实现一个功能,按时间排序功能相同代码相同先完成的
请求版主加1分技术分或3个金币
(以巩固你们的知识为前提 否则就没意义咯)
我先上个人想到的大体代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 学生管理系统
{/*学生管理系统 具体功能如下:
1、录入学生成绩信息,包括学号 姓名 性别 语文数学外语三门成绩
2、实现查找功能,输入学号能查找出该学生成绩等相关信息
3、实现删除功能,输入学号能删除该学生成绩等相关信息
4、实现修改功能,输入学号能修改该学生除学号外的其它信息
5、实现插入功能,既输入要插入的位置,在该位置后进行插入操作
6、实现排序功能,按三门课的总成绩进行由高到低排序
7、统计学生总人数
8、..........*/
class Program
{
static void Main(string[] args)
{
Menu();
Console.WriteLine("请选择需要进行操作的序列号:");
int number = Convert.ToInt32(Console .ReadLine ());
switch (number)
{
case 1:
Input();//调用录入方法
break;
case 2:
Search();//调用查找方法
break;
case 3:
Deletion();//调用删除方法
break;
case 4:
Modification();//调用修改方法
break;
case 5:
Insert();//调用插入方法
break;
case 6:
Order();//调用排序方法
break;
case 7:
Total();//调用统计方法
break;
case 8:
Exit();//调用退出方法
break;
default :
break;
}
Console.ReadKey();
}
/// <summary>
/// 显示主菜单
/// </summary>
static void Menu()
{
Console.WriteLine("****************STUDENT****************");
Console.WriteLine("* 1、信息录入 *");
Console.WriteLine("* 2、查找信息 *");
Console.WriteLine("* 3、删除信息 *");
Console.WriteLine("* 4、修改信息 *");
Console.WriteLine("* 5、插入信息 *");
Console.WriteLine("* 6、排序操作 *");
Console.WriteLine("* 7、统计操作 *");
Console.WriteLine("* 8、退出系统 *");
Console.WriteLine("***************************************");
}
/// <summary>
/// 学生相关信息录入
/// </summary>
static void Input()
{
}
/// <summary>
/// 实现查找功能
/// </summary>
static void Search()
{
}
/// <summary>
/// 实现删除功能
/// </summary>
static void Deletion()
{ }
/// <summary>
/// 实现修改功能
/// </summary>
static void Modification()
{ }
/// <summary>
/// 实现插入功能
/// </summary>
static void Insert()
{ }
/// <summary>
/// 实现排序功能
/// </summary>
static void Order()
{ }
/// <summary>
/// 实现统计功能
/// </summary>
static void Total()
{ }
/// <summary>
/// 退出,关闭系统
/// </summary>
static void Exit()
{ }
}
}
复制代码
<FONT color=#000000 size=5></FONT>
复制代码
作者:
zhangcheng5468
时间:
2013-7-18 23:19
开动大家的智慧哦,奖励大大的!{:soso_e151:}
作者:
changvh
时间:
2013-7-19 08:01
/*
* 用户: Changweihua
* 日期: 2013/7/19
* 时间: 7:14
*
* changweihua@outlook.com
* http://www.cmono.net
*
*/
using System;
using System.Collections;
using System.Collections.Generic;
namespace StudentManagement
{
class Program
{
static List<Student> students = new List<Student>();
public static void Main(string[] args)
{
bool flag = true;
Menu();
do
{
Console.WriteLine("请选择需要进行操作的序列号:");
int number = Convert.ToInt32(Console .ReadLine ());
switch (number)
{
case 1:
Input();//调用录入方法
break;
case 2:
Search();//调用查找方法
break;
case 3:
Deletion();//调用删除方法
break;
case 4:
Modification();//调用修改方法
break;
case 5:
Insert();//调用插入方法
break;
case 6:
Order();//调用排序方法
break;
case 7:
Total();//调用统计方法
break;
case 8:
Exit();//调用退出方法
flag = false;
break;
default :
flag = false;
break;
}
}while(flag);
Console.ReadKey();
}
/// <summary>
/// 显示主菜单
/// </summary>
static void Menu()
{
Console.WriteLine("****************STUDENT****************");
Console.WriteLine("* 1、信息录入 *");
Console.WriteLine("* 2、查找信息 *");
Console.WriteLine("* 3、删除信息 *");
Console.WriteLine("* 4、修改信息 *");
Console.WriteLine("* 5、插入信息 *");
Console.WriteLine("* 6、排序操作 *");
Console.WriteLine("* 7、统计操作 *");
Console.WriteLine("* 8、退出系统 *");
Console.WriteLine("***************************************");
}
static void PrintStudent(List<Student> list)
{
foreach (var item in list)
{
Console.WriteLine(item.toXString());
}
}
/// <summary>
/// 学生相关信息录入
/// </summary>
static void Input()
{
Console.WriteLine("学号");
string id = Console.ReadLine();
Console.WriteLine("姓名");
string name = Console.ReadLine();
Console.WriteLine("语文");
double c = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("数学");
double m = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("英语");
double e = Convert.ToDouble(Console.ReadLine());
if(students.FindIndex(s => s.Id == id) != -1)
{
Console.WriteLine("已经存在");
return ;
}
students.Add(new Student { Id = id, Name = name, Chinese = c, Maths = m, English = e });
Console.WriteLine("添加成功");
PrintStudent(students);
}
/// <summary>
/// 实现查找功能
/// </summary>
static void Search()
{
Console.WriteLine("学号");
string id = Console.ReadLine();
if(students.Count == 0)
{
Console.WriteLine("找不到");
}
int index = students.FindIndex(s => s.Id == id);
if(index == -1)
{
Console.WriteLine("找不到");
}
else
{
Console.WriteLine(students[index].toXString());
}
}
/// <summary>
/// 实现删除功能
/// </summary>
static void Deletion()
{
Console.WriteLine("学号");
string id = Console.ReadLine();
if(students.Count == 0)
{
Console.WriteLine("找不到");
}
int index = students.FindIndex(s => s.Id == id);
if(index == -1)
{
Console.WriteLine("找不到");
}
else
{
students.RemoveAt(index);
Console.WriteLine("删除成功");
PrintStudent(students);
}
}
/// <summary>
/// 实现修改功能
/// </summary>
static void Modification()
{
Console.WriteLine("学号");
string id = Console.ReadLine();
if(students.Count == 0)
{
Console.WriteLine("找不到");
}
int index = students.FindIndex(s => s.Id == id);
if(index == -1)
{
Console.WriteLine("找不到");
}
else
{
Console.WriteLine("姓名");
string name = Console.ReadLine();
students[index].Name = name;
Console.WriteLine("修改成功");
PrintStudent(students);
}
}
/// <summary>
/// 实现插入功能
/// </summary>
static void Insert()
{
Console.WriteLine("位置");
int index = Convert.ToInt32( Console.ReadLine());
if(index > students.Count)
{
Console.WriteLine("超出学生数");
return ;
}
Console.WriteLine("学号");
string id = Console.ReadLine();
Console.WriteLine("姓名");
string name = Console.ReadLine();
Console.WriteLine("语文");
double c = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("数学");
double m = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("英语");
double e = Convert.ToDouble(Console.ReadLine());
if(students.FindIndex(s => s.Id == id) != -1)
{
Console.WriteLine("已经存在");
return ;
}
students.Insert(index - 1, new Student { Id = id, Name = name, Chinese = c, Maths = m, English = e });
Console.WriteLine("添加成功");
PrintStudent(students);
}
/// <summary>
/// 实现排序功能
/// </summary>
static void Order()
{
students.Sort(new StudentComparer());
PrintStudent(students);
}
/// <summary>
/// 实现统计功能
/// </summary>
static void Total()
{
Console.WriteLine("总人数:{0}", students.Count);
}
/// <summary>
/// 退出,关闭系统
/// </summary>
static void Exit()
{
Console.WriteLine("成功退出");
}
}
class Student : IComparable<Student>
{
public string Id { get; set; }
public string Name { get; set; }
public double Chinese { get; set; }
public double Maths { get; set; }
public double English { get; set; }
public string toXString()
{
return string.Format("学号: {0}, 姓名: {1}, 语文: {2}, 数学: {3}, 英语: {4}", Id, Name, Chinese, Maths, English);
}
#region IComparable<Student> Members
public int CompareTo(Student other)
{
return (this.English + this.Maths + this.Chinese).CompareTo((other.Chinese + other.Maths + other.English));
}
#endregion
}
class StudentComparer : IComparer<Student>
{
public int Compare( Student x, Student y )
{
return (x.Chinese + x.Maths + x.English).CompareTo((y.Chinese + y.Maths + y.English));
}
}
}
复制代码
作者:
changvh
时间:
2013-7-19 10:48
changvh 发表于 2013-7-19 08:01
除了最后一个比较有点难度,其它操作应该没有难度吧,就是简单的List的操作
作者:
changvh
时间:
2013-7-19 12:49
重新定义一个方法,用于查找
static int FindStudent(string id)
{
for (int i = 0; i < students.Count; i++)
{
if(students[i].Id == id)
{
return i;
}
}
return -1;
}
复制代码
根据语数外排序方法,使用冒泡排序
static void Order()
{
Student x, y;
for (int i = 0; i < students.Count; i++)
{
for (int j = 1; j < students.Count - i; j++)
{
x = students[j - 1];
y = students[j];
if((x.Chinese + x.Maths + x.English).CompareTo((y.Chinese + y.Maths + y.English)) > 0 )
{
students[j - 1] = y;
students[j] = x;
}
}
}
PrintStudent(students);
}
复制代码
作者:
changvh
时间:
2013-7-19 12:58
最终版(带关键注释)
/*
* 用户: Changweihua
* 日期: 2013/7/19
* 时间: 7:14
*
* changweihua@outlook.com
* http://www.cmono.net
*
*/
using System;
using System.Collections;
using System.Collections.Generic;
namespace StudentManagement
{
class Program
{
//学生信息,临时存储
static List<Student> students = new List<Student>();
public static void Main(string[] args)
{
bool flag = true;
Menu();
do
{
Console.WriteLine("请选择需要进行操作的序列号:");
int number = Convert.ToInt32(Console .ReadLine ());
switch (number)
{
case 1:
Input();//调用录入方法
break;
case 2:
Search();//调用查找方法
break;
case 3:
Deletion();//调用删除方法
break;
case 4:
Modification();//调用修改方法
break;
case 5:
Insert();//调用插入方法
break;
case 6:
BubbleOrder();//调用排序方法
break;
case 7:
Total();//调用统计方法
break;
case 8:
Exit();//调用退出方法
flag = false;
break;
default :
flag = false;
break;
}
}while(flag);
Console.ReadKey();
}
/// <summary>
/// 根据ID查找学生信息
/// </summary>
/// <param name="id">学号</param>
/// <returns>索引位置</returns>
static int FindStudent(string id)
{
for (int i = 0; i < students.Count; i++)
{
if(students[i].Id == id)
{
return i;
}
}
return -1;
}
/// <summary>
/// 显示主菜单
/// </summary>
static void Menu()
{
Console.WriteLine("****************STUDENT****************");
Console.WriteLine("* 1、信息录入 *");
Console.WriteLine("* 2、查找信息 *");
Console.WriteLine("* 3、删除信息 *");
Console.WriteLine("* 4、修改信息 *");
Console.WriteLine("* 5、插入信息 *");
Console.WriteLine("* 6、排序操作 *");
Console.WriteLine("* 7、统计操作 *");
Console.WriteLine("* 8、退出系统 *");
Console.WriteLine("***************************************");
}
/// <summary>
/// 输出所有学生
/// </summary>
/// <param name="list"></param>
static void PrintStudent(List<Student> list)
{
foreach (var item in list)
{
Console.WriteLine(item.toXString());
}
}
/// <summary>
/// 学生相关信息录入
/// </summary>
static void Input()
{
#region 输入学生信息
Console.WriteLine("学号");
string id = Console.ReadLine();
Console.WriteLine("姓名");
string name = Console.ReadLine();
Console.WriteLine("语文");
double c = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("数学");
double m = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("英语");
double e = Convert.ToDouble(Console.ReadLine());
#endregion
//判断学号是否已经重复
if(students.FindIndex(s => s.Id == id) != -1)
{
Console.WriteLine("已经存在");
return ;
}
//不重复,添加
students.Add(new Student { Id = id, Name = name, Chinese = c, Maths = m, English = e });
Console.WriteLine("添加成功");
PrintStudent(students);
}
/// <summary>
/// 实现查找功能
/// </summary>
static void Search()
{
//得到学号
Console.WriteLine("学号");
string id = Console.ReadLine();
//如果集合中没有学生
if(students.Count == 0)
{
Console.WriteLine("找不到");
}
// int index = students.FindIndex(s => s.Id == id);
//查找位置
int index = FindStudent(id);
if(index == -1)
{
Console.WriteLine("找不到");
}
else
{
Console.WriteLine(students[index].toXString());
}
}
/// <summary>
/// 实现删除功能
/// </summary>
static void Deletion()
{
//得到学号
Console.WriteLine("学号");
string id = Console.ReadLine();
if(students.Count == 0)
{
Console.WriteLine("找不到");
}
int index = FindStudent(id);
if(index == -1)
{
Console.WriteLine("找不到");
}
else
{
students.RemoveAt(index);
Console.WriteLine("删除成功");
PrintStudent(students);
}
}
/// <summary>
/// 实现修改功能
/// </summary>
static void Modification()
{
Console.WriteLine("学号");
string id = Console.ReadLine();
if(students.Count == 0)
{
Console.WriteLine("找不到");
}
int index = FindStudent(id);
if(index == -1)
{
Console.WriteLine("找不到");
}
else
{
Console.WriteLine("姓名");
string name = Console.ReadLine();
students[index].Name = name;
Console.WriteLine("修改成功");
PrintStudent(students);
}
}
/// <summary>
/// 实现插入功能
/// </summary>
static void Insert()
{
Console.WriteLine("位置");
int index = Convert.ToInt32( Console.ReadLine());
if(index > students.Count)
{
Console.WriteLine("超出学生数");
return ;
}
Console.WriteLine("学号");
string id = Console.ReadLine();
Console.WriteLine("姓名");
string name = Console.ReadLine();
Console.WriteLine("语文");
double c = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("数学");
double m = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("英语");
double e = Convert.ToDouble(Console.ReadLine());
if(int index = FindStudent(id) != -1)
{
Console.WriteLine("已经存在");
return ;
}
students.Insert(index - 1, new Student { Id = id, Name = name, Chinese = c, Maths = m, English = e });
Console.WriteLine("添加成功");
PrintStudent(students);
}
/// <summary>
/// 实现排序功能,看不懂拉倒
/// </summary>
static void Order()
{
students.Sort(new StudentComparer());
PrintStudent(students);
}
/// <summary>
/// 实现排序功能(使用冒泡排序)
/// </summary>
static void BubbleOrder()
{
Student x, y;
for (int i = 0; i < students.Count; i++)
{
for (int j = 1; j < students.Count - i; j++)
{
x = students[j - 1];
y = students[j];
if((x.Chinese + x.Maths + x.English).CompareTo((y.Chinese + y.Maths + y.English)) > 0 )
{
students[j - 1] = y;
students[j] = x;
}
}
}
PrintStudent(students);
}
/// <summary>
/// 实现统计功能
/// </summary>
static void Total()
{
Console.WriteLine("总人数:{0}", students.Count);
}
/// <summary>
/// 退出,关闭系统
/// </summary>
static void Exit()
{
Console.WriteLine("成功退出");
}
}
class Student : IComparable<Student>
{
public string Id { get; set; }
public string Name { get; set; }
public double Chinese { get; set; }
public double Maths { get; set; }
public double English { get; set; }
//输出学生信息
public string toXString()
{
return string.Format("学号: {0}, 姓名: {1}, 语文: {2}, 数学: {3}, 英语: {4}", Id, Name, Chinese, Maths, English);
}
#region IComparable<Student> Members
public int CompareTo(Student other)
{
return (this.English + this.Maths + this.Chinese).CompareTo((other.Chinese + other.Maths + other.English));
}
#endregion
}
class StudentComparer : IComparer<Student>
{
public int Compare( Student x, Student y )
{
return (x.Chinese + x.Maths + x.English).CompareTo((y.Chinese + y.Maths + y.English));
}
}
}
复制代码
作者:
§風過無痕§
时间:
2013-7-19 23:48
亲们!来吧 顶起来吧!并不需要你们必须完成整块功能!只要认真完成其中一部分功能的都有机会获得技术分!注意!代码的便于阅读 易于修改
作者:
张荣耀
时间:
2013-7-24 17:17
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
///C#控制台
static void Main(string[] args)
{
#region Student Text
AllStudent all = new AllStudent(3);
all.AddAllreslut();
all.RtAvg();
all.printStu();
all.sorting();
Console.WriteLine("(冒泡排序)排序后成绩如下:");
all.printStu();
#endregion
}
以下是单个学生类和整体学生类
#region Student类
public class Students
{
#region 构造函数
public Students()
{
}
public Students(string name, string number, float Cres, float Elys, float maths)
{
_name = name;
_number = number;
_Cres = Cres;
_ely = Elys;
_math = maths;
_avg = (Cres + Elys + maths) / 3;
}
#endregion
#region 字段
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _number;
public string Number
{
get { return _number; }
set { _number = value; }
}
private float _Cres;
public float Cres
{
get { return _Cres; }
set { _Cres = value; }
}
private float _ely;
public float Ely
{
get { return _ely; }
set { _ely = value; }
}
private float _math;
public float Math
{
get { return _math; }
set { _math = value; }
}
private float _avg;
public float Avg
{
get { return _avg; }
set { _avg = value; }
}
#endregion
}
#endregion
#region 全体学生类
public class AllStudent
{
#region 构造函数
public AllStudent(int cout)
{
_cout = cout;
_stuList = new List<Students>();
}
#endregion
#region 字段和属性
private int _cout;
public int Cout
{
get { return _cout; }
set { _cout = value; }
}
private List<Students> _stuList;
public List<Students> StuList
{
get { return _stuList; }
set { _stuList = value; }
}
#endregion
#region 学生成绩录入方法
public void AddAllreslut()
{
for (int i = 0; i < _cout; i++)
{
string[] strs = new string[5];
Console.WriteLine("请输入学生姓名:");
strs[0] = Console.ReadLine();
Console.WriteLine("请输入学生学号:");
strs[1] = Console.ReadLine();
Console.WriteLine("请输入学生C++成绩:");
strs[2] = Console.ReadLine();
if (!Isfloat(strs[2]))
{
Console.WriteLine("请输入正确的成绩:");
strs[2] = Console.ReadLine();
}
else
{
if (float.Parse(strs[2]) > 100)
{
Console.WriteLine("请输入正确的成绩:");
strs[2] = Console.ReadLine();
}
}
Console.WriteLine("请输入学生英语成绩:");
strs[3] = Console.ReadLine();
if (!Isfloat(strs[3]))
{
Console.WriteLine("请输入正确的成绩:");
strs[3] = Console.ReadLine();
}
else
{
if (float.Parse(strs[3]) > 100)
{
Console.WriteLine("请输入正确的成绩:");
strs[3] = Console.ReadLine();
}
}
Console.WriteLine("请输入学生数学成绩:");
strs[4] = Console.ReadLine();
if (!Isfloat(strs[4]))
{
Console.WriteLine("请输入正确的成绩:");
strs[4] = Console.ReadLine();
}
else
{
if (float.Parse(strs[4]) > 100)
{
Console.WriteLine("请输入正确的成绩:");
strs[4] = Console.ReadLine();
}
}
Students student = new Students(strs[0], strs[1], float.Parse(strs[2]), float.Parse(strs[3]), float.Parse(strs[4]));
Console.WriteLine(strs[0] + "同学的平均成绩为:" + student.Avg);
Console.WriteLine();
_stuList.Add(student);
}
}
#endregion
#region 按学号查询平均成绩
public void RtAvg()
{
Console.WriteLine("请输入要查询平均成绩学生的学号:");
string number = Console.ReadLine();
float avg = RtAvg(number);
if (avg != 0)
{
Console.WriteLine(number + "的平均成绩为:" + avg);
Console.ReadKey();
}
else
{
Console.WriteLine("没有该学号学生成绩!");
Console.ReadKey();
}
}
public float RtAvg(string number)
{
for (int i = 0; i < _stuList.Count; i++)
{
if (_stuList[i].Number.Trim() == number.Trim())
{
return _stuList[i].Avg;
}
}
return 0;
}
#endregion
#region 按平均分排序方法
public void sorting()
{
List<Students> list = new List<Students>();
for (int i = 0; i < _stuList.Count; i++)
{
Students stus = new Students();
for (int j = 0; j < _stuList.Count - i - 1; j++)
{
if (_stuList[j].Avg > _stuList[j + 1].Avg)
{
stus = _stuList[j];
_stuList[j] = _stuList[j + 1];
_stuList[j + 1] = stus;
}
else
{
stus = _stuList[j + 1];
}
}
if (i == _stuList.Count - 1)
{
stus = _stuList[0];
}
list.Add(stus);
}
_stuList = list;
}
#endregion
#region 输出所有学生成绩
public void printStu()
{
Console.WriteLine("一下是所有学生信息:");
for (int i = 0; i < _stuList.Count; i++)
{
Console.WriteLine(_stuList[i].Name + "同学的基本信息:");
Console.WriteLine("学号:" + _stuList[i].Number + " C++成绩为:" + _stuList[i].Cres + " 英语成绩为:" + _stuList[i].Ely + " 数学成绩为:" + _stuList[i].Math+" 平均成绩为:"+_stuList[i].Avg);
Console.WriteLine();
}
Console.ReadKey();
}
#endregion
#region 验证浮点数方法
public static bool Isfloat(string Input)
{
if (Input == null)
{
return false;
}
else
{
string pattern = "^(\\d*\\.)?\\d+$";
if (Regex.Match(Input, pattern, RegexOptions.Compiled).Success)
{
return true;
}
else
{
return false;
}
}
}
#endregion
}
#endregion
namespace TestAnthing
{
class Program
{
static void Main(string[] args)
{
Student.InPutAllScore();
Student.Students.Sort();
Student.ListAllInfo();
Console.ReadLine();
}
}
// 课目枚举
public enum ClassName {
CPP, Eng, Math
}
// 单科成绩类
public class Score
{
public ClassName ItemName { get; set; } // 科目
public Decimal ScoreNum { get; set; }
}
// 学生类
public class Student: IComparable
{
public string ID { get; set; }
public string Name { get; set; }
// 下面用一个字典来保存此学生的各课成绩
public Dictionary<ClassName, Score> Score = new Dictionary<ClassName, Score>();
// 只读属性:平均分
public Decimal AvgSocre
{
get
{
var a = (from s in this.Score.Values
select s.ScoreNum).Average();
return a;
}
}
// 为学生设置一科分数
public void SetScore(ClassName c, decimal dcScore)
{
this.Score[c] = new Score() { ItemName = c, ScoreNum = dcScore };
}
// 得到学生的某科分数
public decimal GetScore(ClassName c)
{
Score s = this.Score[c];
return s == null ? 0m : s.ScoreNum;
}
// 全局保存全班学生
public static List<Student> Students = new List<Student>();
// 输入全班分数
public static void InPutAllScore()
{
int i = 0;
while (true)
{
i++;
Console.WriteLine("请输入第{0}个学生的信息, 学号输入end表示结束", i);
Console.Write("学号:");
string sID = Console.ReadLine();
if (sID == "end")
break;
Student s = new Student();
s.ID = sID;
Console.Write("姓名:");
string sName = Console.ReadLine();
s.Name = sName;
Console.Write("C++成绩:");
string sScore = Console.ReadLine();
s.SetScore(ClassName.CPP, decimal.Parse(sScore));
s.Name = sName;
Console.Write("英语成绩:");
sScore = Console.ReadLine();
s.SetScore(ClassName.Eng, decimal.Parse(sScore));
s.Name = sName;
Console.Write("数学成绩:");
sScore = Console.ReadLine();
s.SetScore(ClassName.Math, decimal.Parse(sScore));
s.Name = sName;
Console.WriteLine("此学生的平均分数为:{0}", s.AvgSocre);
Students.Add(s);
}
}
#region IComparable 成员, 用于排序
int IComparable.CompareTo(object obj)
{
Student s = obj as Student;
return -(int)(this.AvgSocre - s.AvgSocre);
}
#endregion
// 输出全部
public static void ListAllInfo()
{
Console.WriteLine("");
for (int i=0; i< Students.Count; i ++)
{
Student s = Students[i];
Console.WriteLine(
"第{0}名:{1, -10} {2, -10} C++: {3,5:#.0} 英语: {4,5:#.0} 数学: {5,5:#.0} 平均分: {6,5:#.0}",
i+1, s.ID, s.Name, s.GetScore(ClassName.CPP),
s.GetScore(ClassName.Eng), s.GetScore(ClassName.Math), s.AvgSocre);
}
}
}
}
复制代码
作者:
张荣耀
时间:
2013-7-24 17:18
Java
#include<iostream>
#include<string.h>
using namespace std;
//----------------student类的声明部分开始-------------------
class Student{
public:
// ---------------三个重载构造函数的开始--------------------
Student();
Student(int NewId, char * NewName, int NewScore0,int NewScore1,int NewScore2);
Student(int NewId, char * NewName, int * NewScore);
//----------------三个重载函数声明结束----------------------
//----------------析构函数的内联定义------------------------
~Student()
{
cout<<endl<<name<<"被析构了!"<<endl;
}
void StuInput();
void StuOutput();
float AverScore();
void ScoreSum();
private:
static int stu_count;
int id;
char name[10];
float score[3];
float ScoSum;
};
//-------------------student类的声明部分结束----------------------------
//-------------------student类的实现部分开始----------------------------
int Student::stu_count=0;
//static型的数据成员必须全局性初始化
//-------------------三个重载构造函数定义开始---------------------------
Student::Student()
{
cout<<"没有参数的构造函数被调用!"<<endl;
stu_count++;
id=0;
strcpy(name,"aaaa");
score[0]=score[1]=score[2]=0;
ScoSum=0;
}
Student::Student(int NewId, char * NewName, int NewScore0,int NewScore1,int NewScore2)
{
cout<<"用三个整数成绩初始化数组的函数被构造函数调用了!"<<endl;
stu_count++;
id=NewId;
strcpy(name,NewName);
score[0]=NewScore0;
score[1]=NewScore1;
score[2]=NewScore2;
}
Student::Student(int NewId, char * NewName, int * NewScore)
{
cout<<"用成绩数组初始化的成绩数组被构造函数调用了!"<<endl;
stu_count++;
id=NewId;
strcpy(name,NewName);
for(int i = 0 ; i < 3 ; i ++){
score[i]= * NewScore++;
}
}
//---------------------三个重载构造函数的结束---------------------------
void Student::StuInput()
{
stu_count++;
int i;
cout<<"\n请输入整数编号,回车结束:\n";
cin>>id;
cout<<"\n请输入姓名字符串,回车结束:\n";
cin>>name;
cout<<"请输入三门课程成绩,每门回车结束!:\n";
for(i = 0; i < 3 ; i ++){
cin>>score[i];
}
cout<<endl;
}
void Student::StuOutput()
{
cout<<"现在共有"<<stu_count<<"名同学!"<<endl;
cout<<id<<'\t'<<name<<'\t'<<score[0]<<'\t'<<score[1]<<'\t'<<score[2]<<endl;
ScoreSum();
cout<<"总成绩是:"<<ScoSum<<endl;
cout<<"平均成绩是:"<<AverScore()<<endl;
}
void Student::ScoreSum()
{
ScoSum=score[0]+score[1]+score[2];
}
float Student::AverScore()
{
return ScoSum/3.0;
}
//---------------------------Student类的实现部分结束------------------------------
//---------------------------main函数的开始---------------------------------------
int main()
{
int temScore[]={100,100,100};
Student LiHong;
LiHong.StuOutput();
cout<<endl<<"----------------------输出第一条分割线----------------------------"<<endl<<endl;
Student ZhangJun(1001,"张军",89,79,69);
Student Wangxinye(1002,"王新野",temScore);
cout<<endl<<"----------------------输出第二条分割线----------------------------"<<endl<<endl;
ZhangJun.StuOutput();
cout<<endl<<"----------------------输出第三条分割线----------------------------"<<endl<<endl;
Wangxinye.StuOutput();
return 0;
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2