美文网首页
C#(10) 枚举 二分查找

C#(10) 枚举 二分查找

作者: cGunsNRoses | 来源:发表于2017-08-28 11:40 被阅读0次

    8yue17

    枚举  赋值给变量的有命名的整数常量的方法

    定义:枚举类型为定义了一组可以赋值给变量的命名整数常

    量提供了一种有效方法,是一个整形常量集合,枚举是一种值类型。

    枚举经常应用于对取值有限定的地方,比如星期只能是周一

    到周日,月份只能是一月到十二月

    枚举声明 enum

    1、基础类型默认为int

    //格式:enum 枚举名{枚举数0,枚举数1...}

    //例子:enum Season {Spring ,Summeer , Autumn , Winter}

    //枚举默认的基础类型是 Int,可以指定除char以为其他整形

    //一般不设置情况下第一个为0  后面依次累加

    2、使用初始值来代替默认值

    enum Season {Spring = 1 ,Summeer , Autumn , Winter}

    //3、指定枚举基础类型

    enum Season:uint{ Spring,Summer,Autumn,Winter}

    基础类型可以是除char外的任何整型,包括byte,sbyte,short

    ushort,int,uint,long或ulong

    创建枚举

    格式:枚举名 枚举变量 = 枚举名.枚举值

    Season first = Season.Spring;

    Console.WriteLine (first);

    //枚举可以自增递减

    // wk++;//超出范围后加的是 数字

    Week wk = Week.Friday;

    Test (Week.Thursday);

    Console.WriteLine ("{0}",(int)wk);

    枚举数与关联值的相互转换

    获取枚举数关联的值需要对枚举值进行强制类型转换:

    int intValue = (int)Season.Spring;

    打印intValue结果为0。

    获得某个值所对应的枚举数同样要进行强制类型转换:

    Season autumn = (Season)2;

    打印autumn结果为Autumn。

    注:当不存在该枚举数的时候则返回原数值。


    获取某一整数值关联的枚举数的名称

    一、

    string str = Enum.GetName (typeof(Week) ,6);

    string str_1 = ((Week)7).ToString ();

    Console.WriteLine (str + ", " + str_1);

    二、

    int weapon = int.Parse (Console.ReadLine());

    Console.WriteLine ("主角更换武器为:" + ((Weapon)weapon).ToString());

    typeof方法 获得当前的类型

    输入枚举值 得到对应的Int值

    Department dep = (Department)Enum.Parse( typeof (Department),"Market");

    Console.WriteLine ("{0}",(int)dep);

     把整个枚举转化为string 的数组 通过数组下标得到枚举值

    string [] atrArr = Enum.GetNames (typeof(Department));

    Console.WriteLine (atrArr[0]);//3

    Array numbers = Enum.GetValues (typeof(Department));

    int n = (int)numbers.GetValue (0);

    string str = Console.ReadLine ();

    int  dep;

    switch (str) {

    case "Accounting":{

    dep = (int)Department.Accounting;

    break;

    }

    case "Administration":{

    dep = (int)Department.Administration;

    break;

    }

    default:{

    Console.WriteLine ("错误");

    break;

    }

    }

    }

    public static void Test (Week wk) {

    switch (wk ){

    case Week.Friday:

    case Week.Monday:

    case Week.Thursday:

    case Week.Tuesday:

    case Week.Wednesday:

    {

    Console.WriteLine ("今天是工作日");

    break;

    }

    case Week.Saturday:

    case Week.Sunday:

    {

    Console.WriteLine ("今天是周末");

    break;

    }

    二分查找

    //必须是有序数组

    //k 是要找的数 m是一半的数的下标

    //i 是第一个 j 是最后一个

    // int []a={2,5,10,15,55,99,111,555};

    // int low = 0, high = a.Length - 1;//低高位下标

    // int k = int.Parse(Console.ReadLine());//要找的数

    // while (low <= high) {

    // //求出中间下标

    // int mid = (low + high) / 2;

    // if (a[mid] == k) {

    // Console.WriteLine ("find,index = {0}",mid);

    // return ;//结束整个方法

    // //break;

    // } else if (a[mid] < k) {

    // low = mid + 1;

    // } else {

    // high= mid - 1;

    // }

    //

    // }

    // if(low > high){

    // Console.WriteLine("404");//404网页没找到

    // }

    相关文章

      网友评论

          本文标题:C#(10) 枚举 二分查找

          本文链接:https://www.haomeiwen.com/subject/ivkorxtx.html