紫岚和卡鲁鲁相爱:枚举排序的程序

来源:百度文库 编辑:高考问答 时间:2024/05/03 22:56:34
对无序数组用枚举法进行排序

using System;

namespace scm
{
class Student
{
private int id;
private string name;
private float score;

public int Id
{
get
{
return id;
}
set
{
id=value;
}
}

public string Name
{
get
{
return name;
}
set
{
name=value;
}
}

public float Score
{
get
{
return score;
}
set
{
score=value;
}
}
}
class StudentCollectionManager
{
System.Collections.Hashtable ht=new System.Collections.Hashtable();

public void Add(Student stu)
{
ht.Add(stu.Id,stu);
}

public void Display()
{
System.Collections.ArrayList al=new System.Collections.ArrayList();
System.Collections.IDictionaryEnumerator e=ht.GetEnumerator();
Student ss=new Student();
while(e.MoveNext())
{
ss=(Student)e.Value;
al.Add(ss.Score);
}
al.Sort();
al.Reverse();
e.Reset();
Console.WriteLine("学号 姓名 成绩\n");
for(int i=0;i<al.Count;i++)
{
while(e.MoveNext())
{
ss=(Student)e.Value;
if(ss.Score ==(float)al[i])
{
Console.WriteLine(" "+ss.Id+" "+ss.Name +" "+ss.Score );
}
}
e.Reset();
}
}
}

class Test
{
static void Main(string[] args)
{
Student s1=new Student();
Student s2=new Student();
Student s3=new Student();
s1.Id=1;
s1.Name="张华";
s1.Score=89;
s2.Id=2;
s2.Name="李丽";
s2.Score=79;
s3.Id=3;
s3.Name="王红";
s3.Score=90.5f;

StudentCollectionManager scm=new StudentCollectionManager();
scm.Add(s1);
scm.Add(s2);
scm.Add(s3);

scm.Display();

Console.ReadLine();
}
}
}
枚举排序哈希表