using System; using System.Collections.Generic; namespace Quiz.Utility { /// /// 表示标志条件描述。 /// [Serializable] public class FlagCondition { public enum ConditionType { 全满足, 单满足 } int checkvalue, mustvalue; bool canaddflag; IList consistentflags, noconsistentflags; string checkmode; ConditionType type; /// /// 表示标志条件是否可用。 /// public bool CanUse { get { return this.checkvalue != 0; } } /// /// 获取或设置检查的标识值。 /// public int CheckValue { get { return this.checkvalue; } set { this.checkvalue = value; } } /// /// 获取或设置满足的标识值。 /// public int MustValue { get { return this.mustvalue; } set { this.mustvalue = value; } } /// /// 获取检查方式。 /// public string CheckMode { get { return this.checkmode; } } /// /// 是否相等 /// public bool IsEqual { get { return CheckMode == "="; } } /// /// 创建标识条件描述。 /// public FlagCondition(ConditionType type) { this.type = type; this.canaddflag = true; this.consistentflags = new List(); this.noconsistentflags = new List(); this.checkmode = "="; } /// /// 创建标识条件描述。 /// /// 检查的标识值。 /// 满足的标识值。 public FlagCondition(int checkvalue, int mustvalue) { this.canaddflag = false; this.checkvalue = checkvalue; this.mustvalue = mustvalue; this.checkmode = "="; } /// /// 创建标识条件描述。 /// /// 检查并满足的标识值。 public FlagCondition(int checkvalue) { this.canaddflag = false; this.checkvalue = checkvalue; this.mustvalue = checkvalue; this.checkmode = "="; } /// /// 增加一个位条件。 /// /// 要添加的位值。 /// 指添加的位值是否相符。 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]; } } } }