C#实验

来源:互联网 发布:膛线管5.5淘宝400元 编辑:程序博客网 时间:2024/05/16 01:35

C#实验5 [图片]

  • 复制地址
  • 分享

ゞ詠恆ゞ 2010年10月12日 22:47 阅读(0) 评论(0) 分类:个人日记 权限: 公开

  • 字体:中
  • 更多
    • 设置置顶
    • 权限设置
    • 推荐日志
    • 转为私密日志
  • 删除
  • 编辑
 

源代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace 实验5
{
    struct Student
    {
        public string Name;
        public string Id;
    }
    class Program
    {
       

        static  void SortName(Student[] stu)
        {
           for(int i=0;i<stu.Length -1;i++)
               for(int j=0;j<stu .Length -1-i;j++)
            {
                if(stu [j].Name .CompareTo (stu[j+1].Name )>0)
                {
                    Student tem=stu[j];
                    stu [j]=stu [j+1];
                    stu [j+1]=tem ;
                }
            }
        }

        static void SortId(Student[] stu)
        {
            for (int i = 0; i < stu.Length - 1; i++)
                for (int j = 0; j < stu.Length - 1 - i; j++)
                {
                    if (stu[j].Id.CompareTo(stu[j + 1].Id) > 0)
                    {
                        Student tem = stu[j];
                        stu[j] = stu[j + 1];
                        stu[j + 1] = tem;
                    }
                }
        }
       
        static void Main(string[] args)
        {
            Student[] stu=new Student [5];
            stu[0].Name = "yang";
            stu[0].Id = "1";
            stu[1].Name = "zhang";
            stu[1].Id = "3";
            stu[2].Name = "li";
            stu[2].Id = "4";
            stu[3].Name = "zhao";
            stu[3].Id = "2";
            stu[4].Id = "5";
            stu[4].Name = "wang";
            Console.WriteLine("排序前:");
            Console.Write("学号:");
            for (int i = 0; i < stu.Length; i++)
                Console.Write("{0,-10}",stu [i].Id );
            Console.WriteLine();
            Console.Write("姓名:");
            for (int i = 0; i < stu.Length; i++)
                Console.Write("{0,-10}",stu [i].Name );
            Console.WriteLine();
            Console.WriteLine("按姓名排序后:");
            SortName(stu);
            Console.Write("学号:");
            for (int i = 0; i < stu.Length; i++)
                Console.Write("{0,-10}",stu [i].Id );
            Console.WriteLine();
            Console.Write("姓名:");
            for (int i = 0; i < stu.Length; i++)
                Console.Write("{0,-10}",stu [i].Name );
            Console.WriteLine();
            Console.WriteLine("按学号排序后:");
            SortId(stu );
            Console.Write("学号:");
            for (int i = 0; i < stu.Length; i++)
                Console.Write("{0,-10}", stu[i].Id);
            Console.WriteLine();
            Console.Write("姓名:");
            for (int i = 0; i < stu.Length; i++)
                Console.Write("{0,-10}", stu[i].Name);


        }
    }
}