标签:
using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace 学生信息 { class Program { public struct student//创建一个结构体,每一个学生的三个信息 { public string name; public int code; public int degree; } /// <summary> /// 冒泡排序 /// </summary> /// <param name="a"></param> /// <returns>输入参数ArrayList a</returns> public ArrayList paixu(ArrayList a) { student b = new student();//创建临时结构体,用于当做中间值 for (int i = 0; i <a.Count; i++) { for (int j = i; j <a.Count-1; j++) { if (((student)a[i]).degree < ((student)a[j + 1]).degree)//强制转换成结构体类型,取分数用于比较大小 { b = ((student)a[i]);//中间值,互换集合中位置 a[i] = a[j + 1]; a[j + 1] = b; } } } return a; } static void Main(string[] args) { student p = new student(); ArrayList s = new ArrayList(); Console.WriteLine("请输入学生人数:"); int a = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i <a; i++) { Console.WriteLine("请输入学生姓名:"); string b1 = Console.ReadLine(); p.name = b1; Console.WriteLine("请输入学号:"); int b2 = Convert.ToInt32(Console.ReadLine()); p.code = b2; Console.WriteLine("请输入分数:"); int b3 = Convert.ToInt32(Console.ReadLine()); p.degree = b3; s.Add(p);//存于集合中 } double sum = 0; for (int i = 0; i < a; i++) { sum = sum + ((student)s[i]).degree; } double avg = sum / a; Console.WriteLine("平均分数为:"+avg); new Program().paixu(s);//调用函数,排序 for (int i = 0; i < a; i++)//排完序,遍历集合 { Console.WriteLine("姓名:"+((student)s[i]).name+" 学号:"+((student)s[i]).code+" 分数:"+((student)s[i]).degree); } Console.ReadLine(); } } }
标签:
原文地址:http://www.cnblogs.com/happinesshappy/p/4520950.html