2016.8.3 C#基础 结构体,枚举类型

结构体-枚举类型


一、枚举类型

      枚举(enum)是值类型的一种特殊形式,它从System.Enum继承而来,并为基础类型的值提供替代名称。枚举类型有名称、基础类型和一组字段。基础类型必须是一个除char 类型外的内置的有符号(或无符号)整数类型(如Byte、Int32或UInt64)。也可以说,枚举类型是一组常量的集合。

1、定义:Enum....{E}

      枚举元素的默认基础类型为int。默认情况下,第一个枚举数的值为0,后面每个枚举数的值依次递增1。例如:

  enum meiju://枚举是常量的集合,一般冒号后面不指定数据类型

        {

        one=3, //枚举里面有默认的索引,也可以指定;如索引3,但在控制台中不起作用;

        two=6,

        three,//用逗号隔开

        four=two,//如果等于之前的一个常量,那么就等于这个常量;最后一行的逗号可省略

        }       

        static void Main(string[] args)

        {

            Console.WriteLine(meiju.one);

            Console.ReadLine();

2、用法:

   枚举也可以指定数据类型,一般不指定。

在定义枚举类型时,可以选择基类型,但可以使用的基类型仅限于long、int、short和byte。例如:

如: enum meiju:int

 二、结构体

      结构类型是用户自己定义的一种类型,它是由其他类型组合而成的,可包含构造函数、常数、字段、方法、属性、索引器、运算符、事件和嵌套类型的值类型。结构在几个重要方面不同于类:结构为值类型而不是引用类型,并且结构不支持继承。

    用结构的主要思想是用于创建小型的对象,如Point和FileInfo等等。这可以节省内存,因为没有如类对象所需的那样有额外的引用产生。例如,当声明含有成千上万个对象的数组时,这会引起极大的差异。

    结构体是一个变量组,将一组变量放在一起,其实就是一个自定义的集合,里面可以包含各种类型的数据,用法和集合一样。

1、定义:

结构体一般定义在Main函数上面,位于Class下面,作为一个类;一般情况Struct定义在Main函数前面,Main函数里面的地方都可以使用,参数前面加上public代表公用变量。

格式:

struct +结构体的名称

{

public int+变量名;

public string+变量名;

public int+变量名;

}

    以上就是定义一个结构体的格式,里面包含许多种数据类型,如整形int,字符串string,带小数点decimal等;

如果结构体定义在Main函数中,再能在Main函数中使用,一般看情况来定义在Main函数前面,Main函数里面的所有地方都可以使用,参数前面加上public代表公用变量。

格式:

 struct student

{

public int no;

public string name;

public int Csharp;

public int web;

public int database;

public int sum;

}

2、用法

 1)在Main函数外面定义了一个student类型的结构体,在Main主函数中使用:

 2)然后为里面的每个元素赋值,结构体名+点+结构体里面变量名称=值。

 3)赋值之后完成之后进行打印。

Student lch=new student();//这句是在Main函数里定义了一个名为lch的student 类型的结构体。

{

lch.no=1;          

lch.name=”李昌辉”;

lch.Cshap=88;

lch.web=90;

lch.database=98;

lch.sum=lch.Cshap+lch.web+lch.database;

}

Console.WriteLine(lch.no,lch.name,lch.Cshap,lch.web,lch.database,lch.sum );

例题:

//定义一个结构体,存放关于车辆的几个信息             //将所有车的信息都放入集合中             //车型号               价格(W)    轴距 (mm)         油耗(L/100km)             //宝马320Li           38              2920                      6.9             //宝马520Li           43              3108                     7.2             //宝马730Li            89              3210                       6.3             //奥迪A4L35TFSI   31              2869                        6.2             //奥迪A6L30TFSI   43              3012                        7.6             //奥迪A8L45TFSI   89              3122                        8.1             //奔驰C200L           35              2920                        6.1             //奔驰E260L           48              3014                        6.7             //奔驰S320L           93              3165                        8

            //现在想买一辆价格在50万以内的车,请给出车的型号以及价格                        

            //买得起豪车,可是很关心油耗,给查一下油耗最低的三辆车的型号以及油耗情况

            //买了车之后后座会经常坐人,所有我现在想要轴距大一些的车,前三名即可             //要列出车的型号以及车的轴距

            //有钱就是任性,就买最贵的,不买最好的,直接调出来最贵的车的所有信息

            //本店有哪几款宝马车??列出来所有信息

            //本店奔驰车里面油耗最低的是哪一个?所有信息

            //本店在售车辆中轴距最长的是不是奥迪??

            //本店在售车辆轴距最长的是不是油耗最高的??

            //奔驰车中轴距最长的是哪一辆??

            //就只喜欢宝马,可是没多少钱,请列出来现有车辆中最便宜的宝马车的所有信息

 struct car
        {
            public string xinghao;
            public double jia;
            public double zhou;
            public double you;
        }
 static void Main(string[] args)
        {                       
            ArrayList al = new ArrayList();
            car car1 = new car();
            car1.xinghao = "宝马320Li";
            car1.jia = 38;
            car1.zhou = 2920;
            car1.you = 6.9;
            al.Add(car1);
            car1.xinghao = "宝马520Li";
            car1.jia = 43;
            car1.zhou = 3108;
            car1.you = 7.2;
            al.Add(car1);
            car1.xinghao = "宝马730Li";
            car1.jia = 89;
            car1.zhou = 3210;
            car1.you = 6.3;
            al.Add(car1);
            car1.xinghao = "奥迪A4L35TFSI";
            car1.jia = 31;
            car1.zhou = 2869;
            car1.you = 6.2;
            al.Add(car1);
            car1.xinghao = "奥迪A6L30TFSI ";
            car1.jia = 43;
            car1.zhou = 3012;
            car1.you = 7.6;
            al.Add(car1);
            car1.xinghao = "奥迪A8L45TFSI";
            car1.jia = 89;
            car1.zhou = 3122;
            car1.you = 8.1;
            al.Add(car1);
            car1.xinghao = "奔驰C200L";
            car1.jia = 35;
            car1.zhou = 2920;
            car1.you = 6.1;
            al.Add(car1);
            car1.xinghao = "奔驰E260L";
            car1.jia = 48;
            car1.zhou = 3014;
            car1.you = 6.7;
            al.Add(car1);
            car1.xinghao = "奔驰S320L";
            car1.jia = 93;
            car1.zhou = 3165;
            car1.you = 8;
            al.Add(car1);

            //现在想买一辆价格在50万以内的车,请给出车的型号以及价格
            Console.WriteLine("价格在50万以内的车的型号:");
            for (int i = 0; i < 9; i++)
            {
                car car2 = new car();
                car2 = (car)al[i];
                if (car2.jia <= 50)
                {
                    Console.WriteLine("型号" + car2.xinghao + "价格" + car2.jia + "万。");
                }
            }

            //买得起豪车,可是很关心油耗,给查一下油耗最低的三辆车的型号以及油耗情况
            Console.WriteLine();
            Console.WriteLine("油耗最低的三辆车的型号以及油耗情况:");

            for (int i = 0; i < 9; i++)
            {
                car car3 = new car();
                car3 = (car)al[i];
                for (int j = i; j < 9; j++)
                {
                    car car4 = new car();
                    car4 = (car)al[j];
                    if (car3.you < car4.you)
                    {
                        object zhong = al[i];
                        al[i] = al[j];
                        al[j] = zhong;
                    }
                }
            }
            car car5 = new car();
            car5 = (car)al[8];
            Console.WriteLine("型号:" + car5.xinghao + "油耗:" + car5.you);
            car car6 = new car();
            car6 = (car)al[7];
            Console.WriteLine("型号:" + car6.xinghao + "油耗:" + car6.you);
            car car7 = new car();
            car7 = (car)al[6];
            Console.WriteLine("型号:" + car7.xinghao + "油耗:" + car7.you);

            //买了车之后后座会经常坐人,所有我现在想要轴距大一些的车,前三名即可,要列出车的型号以及车的轴距
            Console.WriteLine();
            Console.WriteLine("轴距最大的三辆车:");
            for (int i = 0; i < 9; i++)
            {
                car car2 = new car();
                car2 = (car)al[i];
                for (int j = i; j < 9; j++)
                {
                    car car3 = new car();
                    car3 = (car)al[j];
                    if (car2.zhou < car3.zhou)
                    {
                        object zhong = al[i];
                        al[i] = al[j];
                        al[j] = zhong;
                    }
                }
            }
            car car8 = new car();
            car8 = (car)al[0];
            Console.WriteLine("型号:" + car8.xinghao + "轴距:" + car8.zhou);
            car car9 = new car();
            car9 = (car)al[1];
            Console.WriteLine("型号:" + car9.xinghao + "轴距:" + car9.zhou);
            car car10 = new car();
            car10 = (car)al[2];
            Console.WriteLine("型号:" + car10.xinghao + "轴距:" + car10.zhou);


            //有钱就是任性,就买最贵的,不买最好的,直接调出来最贵的车的所有信息
            Console.WriteLine();
            for (int i = 0; i < 9; i++)
            {
                car car2 = new car();
                car2 = (car)al[i];
                for (int j = i; j < 8; j++)
                {
                    car car3 = new car();
                    car3 = (car)al[j];
                    if (car2.jia < car3.jia)
                    {
                        object zhong = al[i];
                        al[i] = al[j];
                        al[j] = zhong;
                    }
                }
            }
            car car11 = new car();
            car11 = (car)al[0];
            Console.WriteLine("最贵的车的型号:{0},价格:{1}, 轴距:{2},油耗:{3}。", car11.xinghao, car11.jia, car11.zhou, car11.you);


            //本店有哪几款宝马车??列出来所有信息
            Console.WriteLine();
            Console.WriteLine("本店有以下几款宝马车系:");
            for (int i = 0; i < 9; i++)
            {
                car car2 = new car();
                car2 = (car)al[i];
                if (car2.xinghao.Contains("宝马"))
                {
                    Console.WriteLine("型号:" + car2.xinghao + ",价格:" + car2.jia + ",轴距:" + car2.zhou + ",油耗:" + car2.you);
                }
            }

            //本店奔驰车里面油耗最低的是哪一个?所有信息
            Console.WriteLine();
            Console.WriteLine("油耗最低的奔驰车:");
            ArrayList ben = new ArrayList();
            for (int k = 0; k < 9; k++)
            {
                car car2 = new car();
                car2 = (car)al[k];                
                if (car2.xinghao.Contains("奔驰"))
                {
                    ben.Add(car2);                    
                }
                for (int i = 0; i <ben.Count; i++)
                {
                    car car3 = new car();
                    car3 = (car)ben[i];
                    for (int j = i; j < ben.Count; j++)
                    {
                        car car4 = new car();
                        car4 = (car)ben[j];
                        if (car3.you > car4.you)
                        {
                            object zhong = ben[i];
                            ben[i] = ben[j];
                            ben[j] = zhong;
                        }
                    }
                }               
            }
            car car12 = new car();
            car12 = (car)ben[0];
            Console.WriteLine("型号:" + car12.xinghao + ",价格:" + car12.jia + ",轴距:" + car12.zhou + ",油耗:" + car12.you);

            //本店在售车辆中轴距最长的是不是奥迪??      
            Console.WriteLine();
            for (int i = 0; i < 9; i++)
            {
                car car2 = new car();
                car2 = (car)al[i];
                for (int j = i; j < 9; j++)
                {
                    car car3 = new car();
                    car3 = (car)al[j];
                    if (car2.zhou < car3.zhou)
                    {
                        object zhong = al[i];
                        al[i] = al[j];
                        al[j] = zhong;
                    }
                }
            }
            car car14=new car();
            car14=(car)al[0];
            if (car14.xinghao.Contains("奥迪"))
            {
                Console.WriteLine("本店在售车辆中轴距最长的是奥迪车系");
            }
            else
            {
                Console.WriteLine("本店在售车辆中轴距最长的不是奥迪车系");
            }


            //本店在售车辆轴距最长的是不是油耗最高的??
            Console.WriteLine();
            for (int i = 0; i < 9; i++)
            {
                car car2 = new car();
                car2 = (car)al[i];
                for (int j = i; j < 9; j++)
                {
                    car car3 = new car();
                    car3 = (car)al[j];
                    if (car2.zhou < car3.zhou)
                    {
                        object zhong = al[i];
                        al[i] = al[j];
                        al[j] = zhong;
                    }
                }
            }
            car car15 = new car();
            car14 = (car)al[0];

            for (int i = 0; i < 9; i++)
            {
                car car3 = new car();
                car3 = (car)al[i];
                for (int j = i; j < 9; j++)
                {
                    car car4 = new car();
                    car4 = (car)al[j];
                    if (car3.you < car4.you)
                    {
                        object zhong = al[i];
                        al[i] = al[j];
                        al[j] = zhong;
                    }
                }
            }
            car car16 = new car();
            car16=(car)al[0];
            if (car15.xinghao == car16.xinghao)
            {
                Console.WriteLine("本店在售车辆轴距最长的是油耗最高的");
            }
            else
            {
                Console.WriteLine("本店在售车辆轴距最长的不是油耗最高的");
            }


            //奔驰车中轴距最长的是哪一辆??
            Console.WriteLine();
            Console.Write("奔驰车中轴距最长的是:");
            ArrayList ben1 = new ArrayList();
            for (int k = 0; k < 9; k++)
            {
                car car2 = new car();
                car2=(car)al[k];
                if (car2.xinghao.Contains("奔驰"))
                {
                    ben1.Add(car2);
                }
                for (int i = 0; i <ben1.Count; i++)
                {
                    car car3 = new car();
                    car3 = (car)ben1[i];
                    for (int j = i; j < ben1.Count; j++)
                    {
                        car car4 = new car();
                        car4 = (car)ben1[j];
                        if (car3.zhou < car4.zhou)
                        {
                            object zhong = ben1[i];
                            ben1[i] = ben1[j];
                            ben1[j] = zhong;
                        }
                    }
                }
            }
            car car17=new car();
            car17=(car)ben1[0];
            Console.WriteLine("   型号:"+car17.xinghao);

            //就只喜欢宝马,可是没多少钱,请列出来现有车辆中最便宜的宝马车的所有信息
            Console.WriteLine();
            Console.Write("宝马车系中最便宜的宝马:");
            ArrayList bao = new ArrayList();
            for (int k = 0; k < 9; k++)
            {
                car car2 = new car();
                car2=(car)al[k];
                if (car2.xinghao.Contains("宝马"))
                {
                    bao.Add(car2);
                }
                for (int i = 0; i < bao.Count; i++)
                {
                    car car3 = new car();
                    car3 = (car)bao[i];
                    for (int j = i; j < bao.Count; j++)
                    {
                        car car4 = new car();
                        car4 = (car)bao[j];
                        if (car3.jia > car4.jia)
                        {
                            object zhong=bao[i];
                            bao[i] = bao[j];
                            bao[j] = zhong;
                        }
                    }
                }
            }
            car car18 = new car();
            car18=(car)bao[0];
            Console.Write("型号:"+car18.xinghao+",价格:"+car18.jia+",轴距:"+car18.zhou+",耗油:"+car18.you);


        }

posted on 2016-08-03 16:55  皮皮怪  阅读(382)  评论(0编辑  收藏  举报

导航