One realization of tri-value logic

xiaoxiao2021-03-06  104

In the previous article, we discussed the possible calculation system and possible code implementation of the three-value logic. Here, we give a complete implementation. The following code uses enumeration, identifying its approved algorithm in each three-valued logic object, and throws an exception if the two object recognized is inconsistent with / or algorithm. For the integrity restrictions on the program structure, the logical object has the default algorithm --Aithmetics.Mathemetic. However, we can modify it in the runtime, or you can easily get the object of the specified algorithm through the factory method. The following is a definition of the algorithm to enumerate the definition of Arithmetics: Public Enum Arithmetics {Mathematic, Database, Verify}; The following is the code of the three-value logical structure, from the name of different and / or arithmetic methods, we can see their support algorithm. Of course, if you see, this design structure is closed, we can't add new algorithms to it - from a practical perspective, these three algorithms have been able to cope with almost all occasions. public struct VarBool {// The three possible Boolw3 values ​​public static readonly VarBool Null = new VarBool (0);. public static readonly VarBool False = new VarBool (-1); public static readonly VarBool True = new VarBool (1); / / Private Field That Stores -1, 0, 1 for False, Null, True. Private sbyte value; private arithmetics _arithmetic; public arithmetics arithmetic {get {returithmetic;} set {_arithmetic = value;}}

// Private Instanceter Must BE -1, 0, OR 1. Private varbool (int value) {this.Value = (sbyte) value; this._arithmetic = arithmetics.mathematic;}

///

/// Structure /// /////// //// can only be the System.Boolean or DBNULL type. public static varbool read (object value) {if (null == value) throw new argumentException ("the value is null!"); if (Value is Bool || Value Is DBNULL) Return (varBool) Value; Else Throw new ArgumentException ("The Value Must In True, False Or DBNULL!");

///

/// Analyzes the value from the string. /// /// Optional value is "true", "false", "null" public static varber parse (String value) {switch (value) {CASE "true": {return true;} case "false": {Return False;} Case "null": {Return null;} default: throw new argumentException ("THE VALUE MUST IN /" TRUE / ", /" False / "OR /" NULL / "!");}}}}}}}}}}}}} // Properties to Examine The Value of a Boolw3. Return True if this // Boolw3 Has The Given Value, False Otherwise. Public Bool ISNULL {Get {Return Value = = 0;}} public bool isfalse {get {return value <0;}} public bool istrue {get {return value> 0;}} // implicit conversion from Bool To Boolw3. Maps True to Boolw3.true and // False To Boolw3.false. Public Static Implicit Operator VarBool (BOOL X) {RETURN X? TRUE: FALSE;}

Public static implicit operator varbool (dbnull x) {return null;}

// Explicit conversion from Boolw3 to bool.Throws an exception if the // given Boolw3 is Null, otherwise returns true or false. Public static explicit operator bool (VarBool x) {if (x.value == 0) throw new InvalidOperationException ( Return X.Value> 0;}

Public Static Explicit Operator DBNULL (Varbool X) {IF (x.value! = 0) throw new invalidopertyleException (); return dbnull.value;}

// Equality Operator. Returns Null if Either Operand Is Null, OtherWise // Returns True Or False. Public Static Varbool Operator == (Varbool X, Varbool Y) {ix (x.value == 0 || y.value == 0) RETURN NULL; RETURN X.Value == Y.Value? True: false;} // inequality operator. Returns Null if Either operand is null, OtherWise // Returns True or false. Public static varbool operator! = (Varbool X , Varbool y) {if (x.value == 0 || y.value == 0) Return null; returnx x.value! = Y.Value? True: false;} // logical negation operator. Returns True IF the Operand is false, null // if the operand is null, or false if the operand is true. public static varbool operator! (varbool x) {return new varbool (-x.value);} public override bool equals (Object Obj) {Return Base.equals (OBJ); PUBLIC OVERRIDE INT GETHASHCODE () {Return this.Value;} public override string toString () {if (value> 0) Return "true"; if (value <0) Return "False "; Return" null ";

Public Static Varbool Mathand (varbool x, varbool y) {return new varbool (x.value

Public Static Varbool Mathor (varbool x, varbool y) {return new varbool (x.value> y.value? x.Value: y.value);

Public static varbool dband (varbool x, varbool y) {if (x.value == 0 || y.value == 0) Return Null; Return New varBool (x.value y.value? x. Value: y.value);

Public Static Varbool Verify (varbool x, varbool y) {if (x.value == -1 || y.value == -1) Return false; if (x.value == 1 || y.Value == 1 Return True; Return Null;} public static varbool verify (varbool x, varbool y) {if (x.value == 1 || y.value == 1) Return true; if (x.value == -1) y.Value == -1) Return False; Return True;} Public Static Varbool Operator & (Varbool X, Varbool Y) {if (x.Arithmetic! = Y.ARITHMETIC) throw new argumentException (String.Format) The {0} algorithm is selected, and the right parameter is selected. "X.Arithmetic, y.Arithmetic); Switch (x.arithmetic) {CASE ARITHMETICS.DATABASE: Return DBAND (X, Y); CASE Arithmetics.mathematic: Return Mathand (X, Y); Case Arithmetics.Verify: Return Verify (x, y); default: return null;}}

Public Static Varbool Operator | (Varbool X, Varbool Y) {if (x.arithmetic! = y.Arithmetic) throw new argumentException (String.Format ("Left parameters Selected {0: D} algorithm, and right parameters selected { 1: D} algorithm. ", X.arithmetic, y.ARithmetic); Switch (X.Arithmetic) {Case Arithmetics.Database: Return Dbor (x, y); Case Arithmetics.mathematic: return Mathor (x, y) Case Arithmetics.Verify: Return Verify (x, y); default: return null;}}

转载请注明原文地址:https://www.9cbs.com/read-105372.html

New Post(0)