在Main中定义student的结构体,进行年龄从大到小依次排序录入学生信息。(结构体的用法以及冒泡排序)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace c编程练习题
{
   
    class Program
    {
       public  struct student               //结构体的用法。
       {
          public int code;
          public string name;
          public int old;
         }
 static void Main(string[] args)
        {
                Console.WriteLine("请输入学生的人数");
                int count = int.Parse(Console.ReadLine());
                ArrayList arr=new ArrayList();
                for (int i = 0; i < count; i++)
                {

                    student s = new student();
                    Console.WriteLine("学号:");
                    s.code = int.Parse(Console.ReadLine());
                    Console.WriteLine("姓名:");
                    s.name = Console.ReadLine();
                    Console.WriteLine("年龄:");
                    s.old = int.Parse(Console.ReadLine());
                    arr.Add(s);
                }
                for (int i = 0; i <count; i++)
                {
                    for (int j = i+1; j < count ; j++)
                    {
                        if (((student)arr[i]).old<((student)arr[j]).old)
                        {
                            student temp = (student)arr[i];
                           arr[i] = arr[j];
                           arr[j] = temp;
                        }
                    }
                }
                Console.WriteLine("学号     姓名      年龄    从大到小依次排列");
                foreach (student s in arr)
                {
                    Console.Write("{0}     {1}      {2}\n",s.code,s.name,s.old);
                }
                Console.ReadLine();

        
        }
    }
}

 

你可能感兴趣的:(冒泡排序)