linqを使用して、指定された数値がセグメントの範囲内に含まれているかどうかを迅速に判断します.
10062 ワード
一、需要:
ある範囲0 x 020~0 x 007 F 0 x 00 A 0~0 x 017 F 0 x 01 A 0~0 x 01 CF 0 x 01 F 0 x 01 FF 0 x 020~0 x 021 F 0 x 1 EA 0~0 x 1 EFFに1つの値が与えられていることを知り、与えられた値が以上の符号化範囲内であるか否かを迅速に判断する
二、解決策
オブジェクト向けのソリューションで解決
1、各セグメントに最小値、最大値、クラスを定義する
2、初期化データ
linqでサイズ検証方法を比較する
メソッドを呼び出します.
ある範囲0 x 020~0 x 007 F 0 x 00 A 0~0 x 017 F 0 x 01 A 0~0 x 01 CF 0 x 01 F 0 x 01 FF 0 x 020~0 x 021 F 0 x 1 EA 0~0 x 1 EFFに1つの値が与えられていることを知り、与えられた値が以上の符号化範囲内であるか否かを迅速に判断する
二、解決策
オブジェクト向けのソリューションで解決
1、各セグメントに最小値、最大値、クラスを定義する
1 public Section(int minValue, int maxValue)
2 {
3 this.MinValue = minValue;
4 this.MaxValue = maxValue;
5 }
6
7 /// <summary>
8 ///
9 /// </summary>
10 public int MinValue { get; set; }
11
12 /// <summary>
13 ///
14 /// </summary>
15 public int MaxValue { get; set; }
2、初期化データ
1 private static readonly List<Section> lstSections;
2
3 static Program()
4 {
5 lstSections = new List<Section>
6 {
7 new Section(0x0020, 0x007F),
8 new Section(0x00A0, 0x017F),
9 new Section(0x01A0, 0x01CF),
10 new Section(0x01F0, 0x01FF),
11 new Section(0x0210, 0x021F),
12 new Section(0x1EA0, 0x1EFF)
13 };
14
15 }
linqでサイズ検証方法を比較する
/// <summary>
/// Vaid
/// </summary>
/// <param name="value"></param>
/// <returns>True: Fase: </returns>
public static bool IsValidSection(int value)
{
var lstFind = lstSections.FindAll(p => value >= p.MinValue && value <= p.MaxValue);
return lstFind.Count > 0;
}
メソッドを呼び出します.
1 static void Main(string[] args)
2 {
3
4
5 while (true)
6 {
7 Console.Write(" 16 :");
8 var input = Console.ReadLine();
9 if (input == "q")
10 {
11 break;
12 }
13 var result = 0;
14 var b = int.TryParse(input.ToUpper(), NumberStyles.AllowHexSpecifier, null, out result);
15 if (b)
16 {
17 Console.WriteLine("{0}-{1}", input, IsValidSection(result) ? " " : " ");
18 }
19 else
20 {
21 Console.WriteLine(" ");
22 }
23 }
24
25 }