实验 结构体

来源:互联网 发布:ubuntu 修复win7引导 编辑:程序博客网 时间:2024/06/03 21:05

实验目标:

    1、掌握结构的声明和使用。

    2、掌握结构默认及有参数构造函数的用法。

    3、理解结构和类的区别。

实验内容:

    1、自定义类实现IComparer接口,比较空间两点大小,先按x的坐标值比较,若相等再按y的坐标值比较。在main方法中进行测试,比较两个坐标点的大小;对由空间某点构成的数组进行排序。

using System;using System.Collections.Generic;using System.Linq;using System.Text;/*自定义类实现IComparer接口,比较空间两点大小, * 先按x的坐标值比较,若相等再按y的坐标值比较。 * 在main方法中进行测试,比较两个坐标点的大小; * 对由空间某点构成的数组进行排序。*/namespace ConsoleApplication1{    class Space:IComparer    {        public double X{get; set;}        public double Y { get; set; }        public Space(double x, double y)        {            this.X = x;            this.Y = y;        }        public void disp()        {            Console.WriteLine("X:{0},Y:{1}",this.X,this.Y);        }        public int Compare(Space s2)        {            Space s1 = new Space(X, Y);            if (s1.X > s2.X)                return 1;            else                if (s1.X < s2.X)                    return -1;                else                    if (s1.Y > s2.Y)                        return 1;                    else if (s1.Y < s2.Y)                        return -1;                    else                        return 0;        }    }    interface IComparer    {         int Compare(Space s2);     }    class Program    {        static void Main(string[] args)        {            Space[] points = new Space[5]{                new Space(1,2),                new Space(2,2.3),                new Space(1,3),                new Space(2,1.4),                new Space(4,2)                        };            for (int i = 0; i < 5; i++)                points[i].disp();            for (int i = 0; i < 5; i++)            {                for (int j = i + 1; j < 5; j++)                {                    if (points[i].Compare(points[j]) > 0)                    {                        Space temp = points[i];                        points[i] = points[j];                        points[j] = temp;                    }                }            }            Console.WriteLine("=======================");            for (int i = 0; i < 5; i++)                points[i].disp();            Console.ReadLine();        }    }}

    2、定义一个学生分数结构体StudentGrade,包含字段Name、Score,以及2个参数的构造函数。利用结构体StudentGrade,创建结构数组变量,存放若干学生的姓名和分数信息,计算平均分。

从主函数中初始化以下字段:运行效果如图所示。

    "张三",99

    "李四",68

    "王五",89

    "姚六",76

using System;using System.Collections.Generic;using System.Linq;using System.Text;/*定义一个学生分数结构体StudentGrade,包含字段Name、Score, * 以及2个参数的构造函数。利用结构体StudentGrade, * 创建结构数组变量,存放若干学生的姓名和分数信息,计算平均分。*/namespace ConsoleApplication2{    struct StudentGrade    {        public string Name;        public double Score;        public StudentGrade(string name, double score)        {            Name = name;            Score = score;        }    }    class Program    {        static void Main(string[] args)        {            StudentGrade[] students = new StudentGrade[4]{                new StudentGrade("张三",99),                new StudentGrade("李四",68),                new StudentGrade("王五",89),                new StudentGrade("姚六",76)            };            for (int i = 0; i < 4; i++)                Console.WriteLine("{0},{1}", students[i].Name, students[i].Score);            Console.ReadLine();        }    }}


0 0
原创粉丝点击