FlagCondition.cs
3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
namespace Quiz.Utility
{
/// <summary>
/// 表示标志条件描述。
/// </summary>
[Serializable]
public class FlagCondition
{
public enum ConditionType { 全满足, 单满足 }
int checkvalue, mustvalue;
bool canaddflag;
IList<int> consistentflags, noconsistentflags;
string checkmode;
ConditionType type;
/// <summary>
/// 表示标志条件是否可用。
/// </summary>
public bool CanUse { get { return this.checkvalue != 0; } }
/// <summary>
/// 获取或设置检查的标识值。
/// </summary>
public int CheckValue { get { return this.checkvalue; } set { this.checkvalue = value; } }
/// <summary>
/// 获取或设置满足的标识值。
/// </summary>
public int MustValue { get { return this.mustvalue; } set { this.mustvalue = value; } }
/// <summary>
/// 获取检查方式。
/// </summary>
public string CheckMode { get { return this.checkmode; } }
/// <summary>
/// 是否相等
/// </summary>
public bool IsEqual { get { return CheckMode == "="; } }
/// <summary>
/// 创建标识条件描述。
/// </summary>
public FlagCondition(ConditionType type)
{
this.type = type;
this.canaddflag = true;
this.consistentflags = new List<int>();
this.noconsistentflags = new List<int>();
this.checkmode = "=";
}
/// <summary>
/// 创建标识条件描述。
/// </summary>
/// <param name="checkvalue">检查的标识值。</param>
/// <param name="mustvalue">满足的标识值。</param>
public FlagCondition(int checkvalue, int mustvalue)
{
this.canaddflag = false;
this.checkvalue = checkvalue;
this.mustvalue = mustvalue;
this.checkmode = "=";
}
/// <summary>
/// 创建标识条件描述。
/// </summary>
/// <param name="checkvalue">检查并满足的标识值。</param>
public FlagCondition(int checkvalue)
{
this.canaddflag = false;
this.checkvalue = checkvalue;
this.mustvalue = checkvalue;
this.checkmode = "=";
}
/// <summary>
/// 增加一个位条件。
/// </summary>
/// <param name="flag">要添加的位值。</param>
/// <param name="consistent">指添加的位值是否相符。</param>
public void AddFlag(int flag, bool consistent)
{
if (!canaddflag) throw new InvalidOperationException("已初始化条件描述值后,不可再添加位条件。");
if (flag < 0) return;
if (consistent && !this.consistentflags.Contains(flag)) this.consistentflags.Add(flag);
else if (!this.noconsistentflags.Contains(flag)) this.noconsistentflags.Add(flag);
Calculate();
}
void Calculate()
{
this.checkvalue = this.mustvalue = 0; this.checkmode = "=";
for (int i = this.consistentflags.Count - 1; i >= 0; i--) if (this.noconsistentflags.Contains(this.consistentflags[i]))
{
this.noconsistentflags.Remove(this.consistentflags[i]);
this.consistentflags.Remove(this.consistentflags[i]);
}
if (this.consistentflags.Count <= 0 && this.noconsistentflags.Count <= 0) return;
this.checkmode = this.type == ConditionType.全满足 ? "=" : "<>";
for (int i = 0; i < this.consistentflags.Count; i++)
{
this.checkvalue |= this.consistentflags[i];
if (this.type == ConditionType.全满足) this.mustvalue |= this.consistentflags[i];
}
for (int i = 0; i < this.noconsistentflags.Count; i++)
{
this.checkvalue |= this.noconsistentflags[i];
if (this.type == ConditionType.单满足) this.mustvalue |= this.noconsistentflags[i];
}
}
}
}