黑马程序员技术交流社区

标题: 第九期的亲们让我们一起来完成这个程序吧@! [打印本页]

作者: §風過無痕§    时间: 2013-7-18 21:59
标题: 第九期的亲们让我们一起来完成这个程序吧@!
本帖最后由 §風過無痕§ 于 2013-7-27 11:34 编辑

自学C#基础有一段时间了  总感觉质量与效率都不咋滴!基本原因嘛 个人觉得平常写的都是那些最基本的代码  缺乏笼统性知识的代码这种程序吧!
第二嘛!我觉得我们第九期的版块活跃度一直都比较低!为了把气氛给带起来且同时想通过这个程序对所学知识的一个巩固,特邀请第九期的马友们共同完成这个程序!  !  !谢谢!
  
这是个最简单的仿学生管理系统
把我们第九期当做一个团队实行分工合作方式共同来完成它。
马友们随时都可以在里面增加有意义的模块
每楼实现一个功能,按时间排序功能相同代码相同先完成的请求版主加1分技术分或3个金币(以巩固你们的知识为前提 否则就没意义咯)


我先上个人想到的大体代码:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace 学生管理系统
  6. {/*学生管理系统 具体功能如下:
  7. 1、录入学生成绩信息,包括学号 姓名 性别 语文数学外语三门成绩
  8. 2、实现查找功能,输入学号能查找出该学生成绩等相关信息
  9. 3、实现删除功能,输入学号能删除该学生成绩等相关信息
  10. 4、实现修改功能,输入学号能修改该学生除学号外的其它信息
  11. 5、实现插入功能,既输入要插入的位置,在该位置后进行插入操作
  12. 6、实现排序功能,按三门课的总成绩进行由高到低排序
  13. 7、统计学生总人数
  14. 8、..........*/

  15. class Program
  16. {
  17. static void Main(string[] args)
  18. {
  19. Menu();
  20. Console.WriteLine("请选择需要进行操作的序列号:");
  21. int number = Convert.ToInt32(Console .ReadLine ());
  22. switch (number)
  23. {
  24. case 1:
  25. Input();//调用录入方法
  26. break;
  27. case 2:
  28. Search();//调用查找方法
  29. break;
  30. case 3:
  31. Deletion();//调用删除方法
  32. break;
  33. case 4:
  34. Modification();//调用修改方法
  35. break;
  36. case 5:
  37. Insert();//调用插入方法
  38. break;
  39. case 6:
  40. Order();//调用排序方法
  41. break;
  42. case 7:
  43. Total();//调用统计方法
  44. break;
  45. case 8:
  46. Exit();//调用退出方法
  47. break;
  48. default :
  49. break;

  50. }

  51. Console.ReadKey();
  52. }

  53. /// <summary>
  54. /// 显示主菜单
  55. /// </summary>
  56. static void Menu()
  57. {
  58. Console.WriteLine("****************STUDENT****************");
  59. Console.WriteLine("* 1、信息录入 *");
  60. Console.WriteLine("* 2、查找信息 *");
  61. Console.WriteLine("* 3、删除信息 *");
  62. Console.WriteLine("* 4、修改信息 *");
  63. Console.WriteLine("* 5、插入信息 *");
  64. Console.WriteLine("* 6、排序操作 *");
  65. Console.WriteLine("* 7、统计操作 *");
  66. Console.WriteLine("* 8、退出系统 *");
  67. Console.WriteLine("***************************************");
  68. }

  69. /// <summary>
  70. /// 学生相关信息录入
  71. /// </summary>
  72. static void Input()
  73. {

  74. }

  75. /// <summary>
  76. /// 实现查找功能
  77. /// </summary>
  78. static void Search()
  79. {
  80. }


  81. /// <summary>
  82. /// 实现删除功能
  83. /// </summary>
  84. static void Deletion()
  85. { }

  86. /// <summary>
  87. /// 实现修改功能
  88. /// </summary>
  89. static void Modification()
  90. { }


  91. /// <summary>
  92. /// 实现插入功能
  93. /// </summary>
  94. static void Insert()
  95. { }

  96. /// <summary>
  97. /// 实现排序功能
  98. /// </summary>
  99. static void Order()
  100. { }


  101. /// <summary>
  102. /// 实现统计功能
  103. /// </summary>
  104. static void Total()
  105. { }


  106. /// <summary>
  107. /// 退出,关闭系统
  108. /// </summary>
  109. static void Exit()
  110. { }
  111. }
  112. }
复制代码

  1. <FONT color=#000000 size=5></FONT>
复制代码



作者: zhangcheng5468    时间: 2013-7-18 23:19
开动大家的智慧哦,奖励大大的!{:soso_e151:}
作者: changvh    时间: 2013-7-19 08:01
  1. /*
  2. * 用户: Changweihua
  3. * 日期: 2013/7/19
  4. * 时间: 7:14
  5. *
  6. * changweihua@outlook.com
  7. * http://www.cmono.net
  8. *
  9. */
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;


  13. namespace StudentManagement
  14. {
  15.        
  16.        
  17.         class Program
  18.         {
  19.                 static List<Student> students = new List<Student>();
  20.                
  21.                 public static void Main(string[] args)
  22.                 {
  23.                         bool flag = true;
  24.                         Menu();
  25.                         do
  26.                         {
  27.                                 Console.WriteLine("请选择需要进行操作的序列号:");
  28.                                 int number = Convert.ToInt32(Console .ReadLine ());
  29.                                 switch (number)
  30.                                 {
  31.                                         case 1:
  32.                                                 Input();//调用录入方法
  33.                                                 break;
  34.                                         case 2:
  35.                                                 Search();//调用查找方法
  36.                                                 break;
  37.                                         case 3:
  38.                                                 Deletion();//调用删除方法
  39.                                                 break;
  40.                                         case 4:
  41.                                                 Modification();//调用修改方法
  42.                                                 break;
  43.                                         case 5:
  44.                                                 Insert();//调用插入方法
  45.                                                 break;
  46.                                         case 6:
  47.                                                 Order();//调用排序方法
  48.                                                 break;
  49.                                         case 7:
  50.                                                 Total();//调用统计方法
  51.                                                 break;
  52.                                         case 8:
  53.                                                 Exit();//调用退出方法
  54.                                                 flag = false;
  55.                                                 break;
  56.                                         default :
  57.                                                 flag = false;
  58.                                                 break;
  59.                                        
  60.                                 }
  61.                         }while(flag);
  62.                        
  63.                         Console.ReadKey();
  64.                 }
  65.                
  66.                
  67.                 /// <summary>
  68.                 /// 显示主菜单
  69.                 /// </summary>
  70.                 static void Menu()
  71.                 {
  72.                         Console.WriteLine("****************STUDENT****************");
  73.                         Console.WriteLine("* 1、信息录入 *");
  74.                         Console.WriteLine("* 2、查找信息 *");
  75.                         Console.WriteLine("* 3、删除信息 *");
  76.                         Console.WriteLine("* 4、修改信息 *");
  77.                         Console.WriteLine("* 5、插入信息 *");
  78.                         Console.WriteLine("* 6、排序操作 *");
  79.                         Console.WriteLine("* 7、统计操作 *");
  80.                         Console.WriteLine("* 8、退出系统 *");
  81.                         Console.WriteLine("***************************************");
  82.                 }
  83.                
  84.                 static void PrintStudent(List<Student> list)
  85.                 {
  86.                         foreach (var item in list)
  87.                         {
  88.                                 Console.WriteLine(item.toXString());
  89.                         }
  90.                 }

  91.                 /// <summary>
  92.                 /// 学生相关信息录入
  93.                 /// </summary>
  94.                 static void Input()
  95.                 {
  96.                         Console.WriteLine("学号");
  97.                         string id = Console.ReadLine();
  98.                         Console.WriteLine("姓名");
  99.                         string name = Console.ReadLine();
  100.                         Console.WriteLine("语文");
  101.                         double c = Convert.ToDouble(Console.ReadLine());
  102.                         Console.WriteLine("数学");
  103.                         double m = Convert.ToDouble(Console.ReadLine());
  104.                         Console.WriteLine("英语");
  105.                         double e = Convert.ToDouble(Console.ReadLine());
  106.                         if(students.FindIndex(s => s.Id == id) != -1)
  107.                         {
  108.                                 Console.WriteLine("已经存在");
  109.                                 return ;
  110.                         }
  111.                         students.Add(new Student { Id = id, Name = name, Chinese = c, Maths = m, English = e });
  112.                         Console.WriteLine("添加成功");
  113.                         PrintStudent(students);
  114.                 }
  115.                
  116.                 /// <summary>
  117.                 /// 实现查找功能
  118.                 /// </summary>
  119.                 static void Search()
  120.                 {
  121.                         Console.WriteLine("学号");
  122.                         string id = Console.ReadLine();
  123.                        
  124.                         if(students.Count == 0)
  125.                         {
  126.                                 Console.WriteLine("找不到");
  127.                         }
  128.                        
  129.                         int index = students.FindIndex(s => s.Id == id);
  130.                        
  131.                         if(index == -1)
  132.                         {
  133.                                 Console.WriteLine("找不到");
  134.                         }
  135.                         else
  136.                         {
  137.                                 Console.WriteLine(students[index].toXString());
  138.                         }
  139.                 }
  140.                
  141.                
  142.                 /// <summary>
  143.                 /// 实现删除功能
  144.                 /// </summary>
  145.                 static void Deletion()
  146.                 {
  147.                         Console.WriteLine("学号");
  148.                         string id = Console.ReadLine();
  149.                        
  150.                         if(students.Count == 0)
  151.                         {
  152.                                 Console.WriteLine("找不到");
  153.                         }
  154.                        
  155.                         int index = students.FindIndex(s => s.Id == id);
  156.                        
  157.                         if(index == -1)
  158.                         {
  159.                                 Console.WriteLine("找不到");
  160.                         }
  161.                         else
  162.                         {
  163.                                 students.RemoveAt(index);
  164.                                 Console.WriteLine("删除成功");
  165.                                 PrintStudent(students);
  166.                         }
  167.                 }
  168.                
  169.                 /// <summary>
  170.                 /// 实现修改功能
  171.                 /// </summary>
  172.                 static void Modification()
  173.                 {
  174.                         Console.WriteLine("学号");
  175.                         string id = Console.ReadLine();
  176.                        
  177.                         if(students.Count == 0)
  178.                         {
  179.                                 Console.WriteLine("找不到");
  180.                         }
  181.                        
  182.                         int index = students.FindIndex(s => s.Id == id);
  183.                        
  184.                         if(index == -1)
  185.                         {
  186.                                 Console.WriteLine("找不到");
  187.                         }
  188.                         else
  189.                         {
  190.                                 Console.WriteLine("姓名");
  191.                                 string name = Console.ReadLine();
  192.                                
  193.                                 students[index].Name = name;
  194.                                
  195.                                 Console.WriteLine("修改成功");
  196.                                
  197.                                 PrintStudent(students);
  198.                         }
  199.                 }
  200.                
  201.                
  202.                 /// <summary>
  203.                 /// 实现插入功能
  204.                 /// </summary>
  205.                 static void Insert()
  206.                 {
  207.                         Console.WriteLine("位置");
  208.                         int index = Convert.ToInt32( Console.ReadLine());
  209.                        
  210.                         if(index > students.Count)
  211.                         {
  212.                                 Console.WriteLine("超出学生数");
  213.                                 return ;
  214.                         }
  215.                        
  216.                         Console.WriteLine("学号");
  217.                         string id = Console.ReadLine();
  218.                         Console.WriteLine("姓名");
  219.                         string name = Console.ReadLine();
  220.                         Console.WriteLine("语文");
  221.                         double c = Convert.ToDouble(Console.ReadLine());
  222.                         Console.WriteLine("数学");
  223.                         double m = Convert.ToDouble(Console.ReadLine());
  224.                         Console.WriteLine("英语");
  225.                         double e = Convert.ToDouble(Console.ReadLine());
  226.                        
  227.                         if(students.FindIndex(s => s.Id == id) != -1)
  228.                         {
  229.                                 Console.WriteLine("已经存在");
  230.                                 return ;
  231.                         }
  232.                         students.Insert(index - 1, new Student { Id = id, Name = name, Chinese = c, Maths = m, English = e });
  233.                         Console.WriteLine("添加成功");
  234.                         PrintStudent(students);
  235.                 }
  236.                
  237.                 /// <summary>
  238.                 /// 实现排序功能
  239.                 /// </summary>
  240.                 static void Order()
  241.                 {
  242.                         students.Sort(new StudentComparer());
  243.                        
  244.                         PrintStudent(students);
  245.                 }
  246.                
  247.                
  248.                 /// <summary>
  249.                 /// 实现统计功能
  250.                 /// </summary>
  251.                 static void Total()
  252.                 {
  253.                         Console.WriteLine("总人数:{0}", students.Count);
  254.                 }


  255.                 /// <summary>
  256.                 /// 退出,关闭系统
  257.                 /// </summary>
  258.                 static void Exit()
  259.                 {
  260.                         Console.WriteLine("成功退出");
  261.                 }
  262.         }
  263.        
  264.         class Student : IComparable<Student>
  265.         {
  266.                 public string Id { get; set; }
  267.                 public string Name { get; set; }
  268.                
  269.                 public double Chinese { get; set; }
  270.                 public double Maths { get; set; }
  271.                 public double English { get; set; }
  272.                
  273.                 public string toXString()
  274.                 {
  275.                         return string.Format("学号: {0}, 姓名: {1}, 语文: {2}, 数学: {3}, 英语: {4}", Id, Name, Chinese, Maths, English);
  276.                 }
  277.                
  278.                  #region IComparable<Student> Members

  279.                 public int CompareTo(Student other)
  280.                 {
  281.                         return (this.English + this.Maths + this.Chinese).CompareTo((other.Chinese + other.Maths + other.English));
  282.                 }
  283.        
  284.                 #endregion
  285.         }
  286.        
  287.         class StudentComparer : IComparer<Student>
  288.         {
  289.                 public int Compare( Student x, Student y )  
  290.                 {
  291.                        
  292.                         return (x.Chinese + x.Maths + x.English).CompareTo((y.Chinese + y.Maths + y.English));
  293.               }
  294.         }
  295.        
  296. }
复制代码

作者: changvh    时间: 2013-7-19 10:48
changvh 发表于 2013-7-19 08:01

除了最后一个比较有点难度,其它操作应该没有难度吧,就是简单的List的操作
作者: changvh    时间: 2013-7-19 12:49
重新定义一个方法,用于查找
  1. static int FindStudent(string id)
  2.                 {
  3.                         for (int i = 0; i < students.Count; i++)
  4.                         {
  5.                                 if(students[i].Id == id)
  6.                                 {
  7.                                         return i;
  8.                                 }
  9.                         }
  10.                        
  11.                         return -1;
  12.                 }
复制代码
根据语数外排序方法,使用冒泡排序
  1. static void Order()
  2.                 {

  3.                         Student x, y;
  4.                        
  5.                         for (int i = 0; i < students.Count; i++)
  6.                         {
  7.                                 for (int j = 1; j < students.Count - i; j++)
  8.                                 {
  9.                                         x = students[j - 1];
  10.                                         y = students[j];
  11.                                         if((x.Chinese + x.Maths + x.English).CompareTo((y.Chinese + y.Maths + y.English)) > 0 )
  12.                                         {
  13.                                                 students[j - 1] = y;
  14.                                                 students[j] = x;
  15.                                         }
  16.                                 }
  17.                         }
  18.                        
  19.                         PrintStudent(students);
  20.                 }
复制代码

作者: changvh    时间: 2013-7-19 12:58
最终版(带关键注释)
  1. /*
  2. * 用户: Changweihua
  3. * 日期: 2013/7/19
  4. * 时间: 7:14
  5. *
  6. * changweihua@outlook.com
  7. * http://www.cmono.net
  8. *
  9. */
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;


  13. namespace StudentManagement
  14. {
  15.        
  16.        
  17.         class Program
  18.         {
  19.                 //学生信息,临时存储
  20.                 static List<Student> students = new List<Student>();
  21.                
  22.                 public static void Main(string[] args)
  23.                 {
  24.                         bool flag = true;
  25.                         Menu();
  26.                         do
  27.                         {
  28.                                 Console.WriteLine("请选择需要进行操作的序列号:");
  29.                                 int number = Convert.ToInt32(Console .ReadLine ());
  30.                                 switch (number)
  31.                                 {
  32.                                         case 1:
  33.                                                 Input();//调用录入方法
  34.                                                 break;
  35.                                         case 2:
  36.                                                 Search();//调用查找方法
  37.                                                 break;
  38.                                         case 3:
  39.                                                 Deletion();//调用删除方法
  40.                                                 break;
  41.                                         case 4:
  42.                                                 Modification();//调用修改方法
  43.                                                 break;
  44.                                         case 5:
  45.                                                 Insert();//调用插入方法
  46.                                                 break;
  47.                                         case 6:
  48.                                                 BubbleOrder();//调用排序方法
  49.                                                 break;
  50.                                         case 7:
  51.                                                 Total();//调用统计方法
  52.                                                 break;
  53.                                         case 8:
  54.                                                 Exit();//调用退出方法
  55.                                                 flag = false;
  56.                                                 break;
  57.                                         default :
  58.                                                 flag = false;
  59.                                                 break;
  60.                                        
  61.                                 }
  62.                         }while(flag);
  63.                        
  64.                         Console.ReadKey();
  65.                 }
  66.                
  67.                 /// <summary>
  68.                 /// 根据ID查找学生信息
  69.                 /// </summary>
  70.                 /// <param name="id">学号</param>
  71.                 /// <returns>索引位置</returns>
  72.                 static int FindStudent(string id)
  73.                 {
  74.                         for (int i = 0; i < students.Count; i++)
  75.                         {
  76.                                 if(students[i].Id == id)
  77.                                 {
  78.                                         return i;
  79.                                 }
  80.                         }
  81.                        
  82.                         return -1;
  83.                 }
  84.                
  85.                 /// <summary>
  86.                 /// 显示主菜单
  87.                 /// </summary>
  88.                 static void Menu()
  89.                 {
  90.                         Console.WriteLine("****************STUDENT****************");
  91.                         Console.WriteLine("* 1、信息录入 *");
  92.                         Console.WriteLine("* 2、查找信息 *");
  93.                         Console.WriteLine("* 3、删除信息 *");
  94.                         Console.WriteLine("* 4、修改信息 *");
  95.                         Console.WriteLine("* 5、插入信息 *");
  96.                         Console.WriteLine("* 6、排序操作 *");
  97.                         Console.WriteLine("* 7、统计操作 *");
  98.                         Console.WriteLine("* 8、退出系统 *");
  99.                         Console.WriteLine("***************************************");
  100.                 }
  101.                
  102.                 /// <summary>
  103.                 /// 输出所有学生
  104.                 /// </summary>
  105.                 /// <param name="list"></param>
  106.                 static void PrintStudent(List<Student> list)
  107.                 {
  108.                         foreach (var item in list)
  109.                         {
  110.                                 Console.WriteLine(item.toXString());
  111.                         }
  112.                 }

  113.                 /// <summary>
  114.                 /// 学生相关信息录入
  115.                 /// </summary>
  116.                 static void Input()
  117.                 {
  118.                         #region        输入学生信息
  119.                         Console.WriteLine("学号");
  120.                         string id = Console.ReadLine();
  121.                         Console.WriteLine("姓名");
  122.                         string name = Console.ReadLine();
  123.                         Console.WriteLine("语文");
  124.                         double c = Convert.ToDouble(Console.ReadLine());
  125.                         Console.WriteLine("数学");
  126.                         double m = Convert.ToDouble(Console.ReadLine());
  127.                         Console.WriteLine("英语");
  128.                         double e = Convert.ToDouble(Console.ReadLine());
  129.                         #endregion
  130.                        
  131.                         //判断学号是否已经重复
  132.                         if(students.FindIndex(s => s.Id == id) != -1)
  133.                         {
  134.                                 Console.WriteLine("已经存在");
  135.                                 return ;
  136.                         }
  137.                        
  138.                         //不重复,添加
  139.                         students.Add(new Student { Id = id, Name = name, Chinese = c, Maths = m, English = e });
  140.                         Console.WriteLine("添加成功");
  141.                         PrintStudent(students);
  142.                 }
  143.                
  144.                 /// <summary>
  145.                 /// 实现查找功能
  146.                 /// </summary>
  147.                 static void Search()
  148.                 {
  149.                         //得到学号
  150.                         Console.WriteLine("学号");
  151.                         string id = Console.ReadLine();
  152.                        
  153.                         //如果集合中没有学生
  154.                         if(students.Count == 0)
  155.                         {
  156.                                 Console.WriteLine("找不到");
  157.                         }
  158.                        
  159. //                        int index = students.FindIndex(s => s.Id == id);
  160.                        
  161.                         //查找位置
  162.                         int index = FindStudent(id);
  163.                        
  164.                         if(index == -1)
  165.                         {
  166.                                 Console.WriteLine("找不到");
  167.                         }
  168.                         else
  169.                         {
  170.                                 Console.WriteLine(students[index].toXString());
  171.                         }
  172.                 }
  173.                
  174.                
  175.                 /// <summary>
  176.                 /// 实现删除功能
  177.                 /// </summary>
  178.                 static void Deletion()
  179.                 {
  180.                         //得到学号
  181.                         Console.WriteLine("学号");
  182.                         string id = Console.ReadLine();
  183.                        
  184.                         if(students.Count == 0)
  185.                         {
  186.                                 Console.WriteLine("找不到");
  187.                         }
  188.                        
  189.                         int index = FindStudent(id);
  190.                        
  191.                         if(index == -1)
  192.                         {
  193.                                 Console.WriteLine("找不到");
  194.                         }
  195.                         else
  196.                         {
  197.                                 students.RemoveAt(index);
  198.                                 Console.WriteLine("删除成功");
  199.                                 PrintStudent(students);
  200.                         }
  201.                 }
  202.                
  203.                 /// <summary>
  204.                 /// 实现修改功能
  205.                 /// </summary>
  206.                 static void Modification()
  207.                 {
  208.                         Console.WriteLine("学号");
  209.                         string id = Console.ReadLine();
  210.                        
  211.                         if(students.Count == 0)
  212.                         {
  213.                                 Console.WriteLine("找不到");
  214.                         }
  215.                        
  216.                         int index = FindStudent(id);
  217.                        
  218.                         if(index == -1)
  219.                         {
  220.                                 Console.WriteLine("找不到");
  221.                         }
  222.                         else
  223.                         {
  224.                                 Console.WriteLine("姓名");
  225.                                 string name = Console.ReadLine();
  226.                                
  227.                                 students[index].Name = name;
  228.                                
  229.                                 Console.WriteLine("修改成功");
  230.                                
  231.                                 PrintStudent(students);
  232.                         }
  233.                 }
  234.                
  235.                
  236.                 /// <summary>
  237.                 /// 实现插入功能
  238.                 /// </summary>
  239.                 static void Insert()
  240.                 {
  241.                         Console.WriteLine("位置");
  242.                         int index = Convert.ToInt32( Console.ReadLine());
  243.                        
  244.                         if(index > students.Count)
  245.                         {
  246.                                 Console.WriteLine("超出学生数");
  247.                                 return ;
  248.                         }
  249.                        
  250.                         Console.WriteLine("学号");
  251.                         string id = Console.ReadLine();
  252.                         Console.WriteLine("姓名");
  253.                         string name = Console.ReadLine();
  254.                         Console.WriteLine("语文");
  255.                         double c = Convert.ToDouble(Console.ReadLine());
  256.                         Console.WriteLine("数学");
  257.                         double m = Convert.ToDouble(Console.ReadLine());
  258.                         Console.WriteLine("英语");
  259.                         double e = Convert.ToDouble(Console.ReadLine());
  260.                        
  261.                         if(int index = FindStudent(id) != -1)
  262.                         {
  263.                                 Console.WriteLine("已经存在");
  264.                                 return ;
  265.                         }
  266.                         students.Insert(index - 1, new Student { Id = id, Name = name, Chinese = c, Maths = m, English = e });
  267.                         Console.WriteLine("添加成功");
  268.                         PrintStudent(students);
  269.                 }
  270.                
  271.                 /// <summary>
  272.                 /// 实现排序功能,看不懂拉倒
  273.                 /// </summary>
  274.                 static void Order()
  275.                 {
  276.                         students.Sort(new StudentComparer());
  277.                        
  278.                         PrintStudent(students);
  279.                 }
  280.                
  281.                 /// <summary>
  282.                 /// 实现排序功能(使用冒泡排序)
  283.                 /// </summary>
  284.                 static void BubbleOrder()
  285.                 {
  286.                        
  287.                         Student x, y;
  288.                        
  289.                         for (int i = 0; i < students.Count; i++)
  290.                         {
  291.                                 for (int j = 1; j < students.Count - i; j++)
  292.                                 {
  293.                                         x = students[j - 1];
  294.                                         y = students[j];
  295.                                         if((x.Chinese + x.Maths + x.English).CompareTo((y.Chinese + y.Maths + y.English)) > 0 )
  296.                                         {
  297.                                                 students[j - 1] = y;
  298.                                                 students[j] = x;
  299.                                         }
  300.                                 }
  301.                         }
  302.                        
  303.                         PrintStudent(students);
  304.                 }
  305.                
  306.                 /// <summary>
  307.                 /// 实现统计功能
  308.                 /// </summary>
  309.                 static void Total()
  310.                 {
  311.                         Console.WriteLine("总人数:{0}", students.Count);
  312.                 }


  313.                 /// <summary>
  314.                 /// 退出,关闭系统
  315.                 /// </summary>
  316.                 static void Exit()
  317.                 {
  318.                         Console.WriteLine("成功退出");
  319.                 }
  320.         }
  321.        
  322.         class Student : IComparable<Student>
  323.         {
  324.                 public string Id { get; set; }
  325.                 public string Name { get; set; }
  326.                
  327.                 public double Chinese { get; set; }
  328.                 public double Maths { get; set; }
  329.                 public double English { get; set; }
  330.                
  331.                 //输出学生信息
  332.                 public string toXString()
  333.                 {
  334.                         return string.Format("学号: {0}, 姓名: {1}, 语文: {2}, 数学: {3}, 英语: {4}", Id, Name, Chinese, Maths, English);
  335.                 }
  336.                
  337.                  #region IComparable<Student> Members

  338.                 public int CompareTo(Student other)
  339.                 {
  340.                         return (this.English + this.Maths + this.Chinese).CompareTo((other.Chinese + other.Maths + other.English));
  341.                 }
  342.        
  343.                 #endregion
  344.         }
  345.        
  346.         class StudentComparer : IComparer<Student>
  347.         {
  348.                 public int Compare( Student x, Student y )  
  349.                 {
  350.                        
  351.                         return (x.Chinese + x.Maths + x.English).CompareTo((y.Chinese + y.Maths + y.English));
  352.               }
  353.         }
  354.        
  355. }
复制代码

作者: §風過無痕§    时间: 2013-7-19 23:48
亲们!来吧  顶起来吧!并不需要你们必须完成整块功能!只要认真完成其中一部分功能的都有机会获得技术分!注意!代码的便于阅读    易于修改
作者: 张荣耀    时间: 2013-7-24 17:17
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;

  5. ///C#控制台
  6. static void Main(string[] args)
  7. {
  8. #region Student Text
  9. AllStudent all = new AllStudent(3);
  10. all.AddAllreslut();
  11. all.RtAvg();
  12. all.printStu();
  13. all.sorting();
  14. Console.WriteLine("(冒泡排序)排序后成绩如下:");
  15. all.printStu();

  16. #endregion
  17. }
  18. 以下是单个学生类和整体学生类

  19. #region Student类
  20. public class Students
  21. {

  22. #region 构造函数
  23. public Students()
  24. {
  25. }

  26. public Students(string name, string number, float Cres, float Elys, float maths)
  27. {
  28. _name = name;
  29. _number = number;
  30. _Cres = Cres;
  31. _ely = Elys;
  32. _math = maths;
  33. _avg = (Cres + Elys + maths) / 3;
  34. }

  35. #endregion

  36. #region 字段
  37. private string _name;

  38. public string Name
  39. {
  40. get { return _name; }
  41. set { _name = value; }
  42. }

  43. private string _number;

  44. public string Number
  45. {
  46. get { return _number; }
  47. set { _number = value; }
  48. }

  49. private float _Cres;

  50. public float Cres
  51. {
  52. get { return _Cres; }
  53. set { _Cres = value; }
  54. }

  55. private float _ely;

  56. public float Ely
  57. {
  58. get { return _ely; }
  59. set { _ely = value; }
  60. }

  61. private float _math;

  62. public float Math
  63. {
  64. get { return _math; }
  65. set { _math = value; }
  66. }

  67. private float _avg;

  68. public float Avg
  69. {
  70. get { return _avg; }
  71. set { _avg = value; }
  72. }

  73. #endregion
  74. }
  75. #endregion

  76. #region 全体学生类
  77. public class AllStudent
  78. {

  79. #region 构造函数
  80. public AllStudent(int cout)
  81. {
  82. _cout = cout;
  83. _stuList = new List<Students>();
  84. }
  85. #endregion

  86. #region 字段和属性
  87. private int _cout;

  88. public int Cout
  89. {
  90. get { return _cout; }
  91. set { _cout = value; }
  92. }

  93. private List<Students> _stuList;

  94. public List<Students> StuList
  95. {
  96. get { return _stuList; }
  97. set { _stuList = value; }
  98. }
  99. #endregion

  100. #region 学生成绩录入方法
  101. public void AddAllreslut()
  102. {
  103. for (int i = 0; i < _cout; i++)
  104. {
  105. string[] strs = new string[5];
  106. Console.WriteLine("请输入学生姓名:");
  107. strs[0] = Console.ReadLine();
  108. Console.WriteLine("请输入学生学号:");
  109. strs[1] = Console.ReadLine();
  110. Console.WriteLine("请输入学生C++成绩:");
  111. strs[2] = Console.ReadLine();
  112. if (!Isfloat(strs[2]))
  113. {
  114. Console.WriteLine("请输入正确的成绩:");
  115. strs[2] = Console.ReadLine();
  116. }
  117. else
  118. {
  119. if (float.Parse(strs[2]) > 100)
  120. {
  121. Console.WriteLine("请输入正确的成绩:");
  122. strs[2] = Console.ReadLine();
  123. }
  124. }
  125. Console.WriteLine("请输入学生英语成绩:");
  126. strs[3] = Console.ReadLine();
  127. if (!Isfloat(strs[3]))
  128. {
  129. Console.WriteLine("请输入正确的成绩:");
  130. strs[3] = Console.ReadLine();
  131. }
  132. else
  133. {
  134. if (float.Parse(strs[3]) > 100)
  135. {
  136. Console.WriteLine("请输入正确的成绩:");
  137. strs[3] = Console.ReadLine();
  138. }
  139. }
  140. Console.WriteLine("请输入学生数学成绩:");
  141. strs[4] = Console.ReadLine();
  142. if (!Isfloat(strs[4]))
  143. {
  144. Console.WriteLine("请输入正确的成绩:");
  145. strs[4] = Console.ReadLine();
  146. }
  147. else
  148. {
  149. if (float.Parse(strs[4]) > 100)
  150. {
  151. Console.WriteLine("请输入正确的成绩:");
  152. strs[4] = Console.ReadLine();
  153. }
  154. }
  155. Students student = new Students(strs[0], strs[1], float.Parse(strs[2]), float.Parse(strs[3]), float.Parse(strs[4]));
  156. Console.WriteLine(strs[0] + "同学的平均成绩为:" + student.Avg);
  157. Console.WriteLine();
  158. _stuList.Add(student);
  159. }
  160. }
  161. #endregion

  162. #region 按学号查询平均成绩
  163. public void RtAvg()
  164. {
  165. Console.WriteLine("请输入要查询平均成绩学生的学号:");
  166. string number = Console.ReadLine();
  167. float avg = RtAvg(number);
  168. if (avg != 0)
  169. {
  170. Console.WriteLine(number + "的平均成绩为:" + avg);
  171. Console.ReadKey();
  172. }
  173. else
  174. {
  175. Console.WriteLine("没有该学号学生成绩!");
  176. Console.ReadKey();
  177. }
  178. }

  179. public float RtAvg(string number)
  180. {
  181. for (int i = 0; i < _stuList.Count; i++)
  182. {
  183. if (_stuList[i].Number.Trim() == number.Trim())
  184. {
  185. return _stuList[i].Avg;
  186. }
  187. }
  188. return 0;
  189. }
  190. #endregion

  191. #region 按平均分排序方法
  192. public void sorting()
  193. {
  194. List<Students> list = new List<Students>();
  195. for (int i = 0; i < _stuList.Count; i++)
  196. {
  197. Students stus = new Students();
  198. for (int j = 0; j < _stuList.Count - i - 1; j++)
  199. {
  200. if (_stuList[j].Avg > _stuList[j + 1].Avg)
  201. {
  202. stus = _stuList[j];
  203. _stuList[j] = _stuList[j + 1];
  204. _stuList[j + 1] = stus;
  205. }
  206. else
  207. {
  208. stus = _stuList[j + 1];
  209. }
  210. }
  211. if (i == _stuList.Count - 1)
  212. {
  213. stus = _stuList[0];
  214. }
  215. list.Add(stus);
  216. }
  217. _stuList = list;
  218. }
  219. #endregion

  220. #region 输出所有学生成绩
  221. public void printStu()
  222. {
  223. Console.WriteLine("一下是所有学生信息:");
  224. for (int i = 0; i < _stuList.Count; i++)
  225. {
  226. Console.WriteLine(_stuList[i].Name + "同学的基本信息:");
  227. Console.WriteLine("学号:" + _stuList[i].Number + " C++成绩为:" + _stuList[i].Cres + " 英语成绩为:" + _stuList[i].Ely + " 数学成绩为:" + _stuList[i].Math+" 平均成绩为:"+_stuList[i].Avg);
  228. Console.WriteLine();
  229. }
  230. Console.ReadKey();
  231. }
  232. #endregion

  233. #region 验证浮点数方法
  234. public static bool Isfloat(string Input)
  235. {
  236. if (Input == null)
  237. {
  238. return false;
  239. }
  240. else
  241. {
  242. string pattern = "^(\\d*\\.)?\\d+$";
  243. if (Regex.Match(Input, pattern, RegexOptions.Compiled).Success)
  244. {
  245. return true;
  246. }
  247. else
  248. {
  249. return false;
  250. }
  251. }
  252. }
  253. #endregion
  254. }

  255. #endregion



  256. namespace TestAnthing
  257. {
  258. class Program
  259. {
  260. static void Main(string[] args)
  261. {
  262. Student.InPutAllScore();
  263. Student.Students.Sort();
  264. Student.ListAllInfo();
  265. Console.ReadLine();
  266. }
  267. }

  268. // 课目枚举
  269. public enum ClassName {
  270. CPP, Eng, Math
  271. }

  272. // 单科成绩类
  273. public class Score
  274. {
  275. public ClassName ItemName { get; set; } // 科目
  276. public Decimal ScoreNum { get; set; }
  277. }

  278. // 学生类
  279. public class Student: IComparable
  280. {
  281. public string ID { get; set; }
  282. public string Name { get; set; }

  283. // 下面用一个字典来保存此学生的各课成绩
  284. public Dictionary<ClassName, Score> Score = new Dictionary<ClassName, Score>();

  285. // 只读属性:平均分
  286. public Decimal AvgSocre
  287. {
  288. get
  289. {
  290. var a = (from s in this.Score.Values
  291. select s.ScoreNum).Average();
  292. return a;
  293. }
  294. }

  295. // 为学生设置一科分数
  296. public void SetScore(ClassName c, decimal dcScore)
  297. {
  298. this.Score[c] = new Score() { ItemName = c, ScoreNum = dcScore };
  299. }

  300. // 得到学生的某科分数
  301. public decimal GetScore(ClassName c)
  302. {
  303. Score s = this.Score[c];
  304. return s == null ? 0m : s.ScoreNum;
  305. }

  306. // 全局保存全班学生
  307. public static List<Student> Students = new List<Student>();

  308. // 输入全班分数
  309. public static void InPutAllScore()
  310. {
  311. int i = 0;
  312. while (true)
  313. {
  314. i++;
  315. Console.WriteLine("请输入第{0}个学生的信息, 学号输入end表示结束", i);
  316. Console.Write("学号:");
  317. string sID = Console.ReadLine();
  318. if (sID == "end")
  319. break;

  320. Student s = new Student();
  321. s.ID = sID;

  322. Console.Write("姓名:");
  323. string sName = Console.ReadLine();
  324. s.Name = sName;

  325. Console.Write("C++成绩:");
  326. string sScore = Console.ReadLine();
  327. s.SetScore(ClassName.CPP, decimal.Parse(sScore));
  328. s.Name = sName;

  329. Console.Write("英语成绩:");
  330. sScore = Console.ReadLine();
  331. s.SetScore(ClassName.Eng, decimal.Parse(sScore));
  332. s.Name = sName;

  333. Console.Write("数学成绩:");
  334. sScore = Console.ReadLine();
  335. s.SetScore(ClassName.Math, decimal.Parse(sScore));
  336. s.Name = sName;

  337. Console.WriteLine("此学生的平均分数为:{0}", s.AvgSocre);

  338. Students.Add(s);
  339. }
  340. }

  341. #region IComparable 成员, 用于排序

  342. int IComparable.CompareTo(object obj)
  343. {
  344. Student s = obj as Student;
  345. return -(int)(this.AvgSocre - s.AvgSocre);
  346. }

  347. #endregion

  348. // 输出全部
  349. public static void ListAllInfo()
  350. {
  351. Console.WriteLine("");
  352. for (int i=0; i< Students.Count; i ++)
  353. {
  354. Student s = Students[i];
  355. Console.WriteLine(
  356. "第{0}名:{1, -10} {2, -10} C++: {3,5:#.0} 英语: {4,5:#.0} 数学: {5,5:#.0} 平均分: {6,5:#.0}",
  357. i+1, s.ID, s.Name, s.GetScore(ClassName.CPP),
  358. s.GetScore(ClassName.Eng), s.GetScore(ClassName.Math), s.AvgSocre);

  359. }
  360. }
  361. }
  362. }
复制代码

作者: 张荣耀    时间: 2013-7-24 17:18
  1. Java
  2. #include<iostream>
  3. #include<string.h>

  4. using namespace std;
  5. //----------------student类的声明部分开始-------------------
  6. class Student{
  7. public:
  8. // ---------------三个重载构造函数的开始--------------------
  9. Student();
  10. Student(int NewId, char * NewName, int NewScore0,int NewScore1,int NewScore2);
  11. Student(int NewId, char * NewName, int * NewScore);
  12. //----------------三个重载函数声明结束----------------------
  13. //----------------析构函数的内联定义------------------------

  14. ~Student()
  15. {
  16. cout<<endl<<name<<"被析构了!"<<endl;
  17. }

  18. void StuInput();
  19. void StuOutput();
  20. float AverScore();
  21. void ScoreSum();

  22. private:

  23. static int stu_count;
  24. int id;
  25. char name[10];
  26. float score[3];
  27. float ScoSum;
  28. };

  29. //-------------------student类的声明部分结束----------------------------
  30. //-------------------student类的实现部分开始----------------------------

  31. int Student::stu_count=0;
  32. //static型的数据成员必须全局性初始化

  33. //-------------------三个重载构造函数定义开始---------------------------

  34. Student::Student()
  35. {
  36. cout<<"没有参数的构造函数被调用!"<<endl;

  37. stu_count++;
  38. id=0;
  39. strcpy(name,"aaaa");
  40. score[0]=score[1]=score[2]=0;
  41. ScoSum=0;
  42. }

  43. Student::Student(int NewId, char * NewName, int NewScore0,int NewScore1,int NewScore2)
  44. {
  45. cout<<"用三个整数成绩初始化数组的函数被构造函数调用了!"<<endl;
  46. stu_count++;
  47. id=NewId;
  48. strcpy(name,NewName);
  49. score[0]=NewScore0;
  50. score[1]=NewScore1;
  51. score[2]=NewScore2;
  52. }

  53. Student::Student(int NewId, char * NewName, int * NewScore)
  54. {
  55. cout<<"用成绩数组初始化的成绩数组被构造函数调用了!"<<endl;
  56. stu_count++;
  57. id=NewId;
  58. strcpy(name,NewName);
  59. for(int i = 0 ; i < 3 ; i ++){
  60. score[i]= * NewScore++;
  61. }
  62. }

  63. //---------------------三个重载构造函数的结束---------------------------

  64. void Student::StuInput()
  65. {
  66. stu_count++;
  67. int i;
  68. cout<<"\n请输入整数编号,回车结束:\n";
  69. cin>>id;
  70. cout<<"\n请输入姓名字符串,回车结束:\n";
  71. cin>>name;
  72. cout<<"请输入三门课程成绩,每门回车结束!:\n";
  73. for(i = 0; i < 3 ; i ++){
  74. cin>>score[i];
  75. }
  76. cout<<endl;
  77. }

  78. void Student::StuOutput()
  79. {
  80. cout<<"现在共有"<<stu_count<<"名同学!"<<endl;
  81. cout<<id<<'\t'<<name<<'\t'<<score[0]<<'\t'<<score[1]<<'\t'<<score[2]<<endl;
  82. ScoreSum();
  83. cout<<"总成绩是:"<<ScoSum<<endl;
  84. cout<<"平均成绩是:"<<AverScore()<<endl;
  85. }

  86. void Student::ScoreSum()
  87. {
  88. ScoSum=score[0]+score[1]+score[2];
  89. }

  90. float Student::AverScore()
  91. {
  92. return ScoSum/3.0;
  93. }

  94. //---------------------------Student类的实现部分结束------------------------------

  95. //---------------------------main函数的开始---------------------------------------

  96. int main()
  97. {
  98. int temScore[]={100,100,100};
  99. Student LiHong;
  100. LiHong.StuOutput();
  101. cout<<endl<<"----------------------输出第一条分割线----------------------------"<<endl<<endl;
  102. Student ZhangJun(1001,"张军",89,79,69);
  103. Student Wangxinye(1002,"王新野",temScore);
  104. cout<<endl<<"----------------------输出第二条分割线----------------------------"<<endl<<endl;
  105. ZhangJun.StuOutput();
  106. cout<<endl<<"----------------------输出第三条分割线----------------------------"<<endl<<endl;
  107. Wangxinye.StuOutput();
  108. return 0;
  109. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2