An example of a counter-law made by logic class

zhaozj2021-02-16  58

Power Logic Applications

http://www3.sympatico.ca/qingzhehuang/logicapp.htm

A.fouth Edition

THIS Is Actually Fourth (Really? I don't Remember Clearly and who cares?) Edition of my logic class. And it Would Be

More precise to call ite an application. However, There is INDEED SOME IMPROVEMENT WITH LITTLE CODE ADDED BUT

Powerful Function Achieved! (Really? I Guess I Won't Believe What Said Myself.)

B. The Problem to Solve:

I Made Little Improvement to try to Solve An Logic Proof In Comp238 Sample Test in Other Section. The Problem IS

Like Following:

// this is what we need to pro:

/ / [(p || t) && (p -> q) && (q -> r) && (r -> s) && (s -> t)] -> T

P, Q, R, S, T Are All Propomitional functions. and the problem. Indeed, IF you

Don't Go this Way, IT IS Quite Difficult, at Least Very Tedious.

C. What is my approach?

I Chang Major Function "Doanalysing ()" a little bit by adding a parameter of bool bycontradiction to make

IT Look Like "Void Doanalysis" ". So, When You Want To Prove by ContraDiction,

SIMPLY Pass A True Parameter Instead The Default False Parameter. Then in The Implementation of The Original

Function, I only compare the in-passing parameter and change the last result to be false to begin an analysis

If IT IS "Proof By ContraDiction". That IS.

However, At First The Program Doesn't Give Correct Outcome Except Only Show That "T" T "T" T "T" T "T" T "T" T "T" T "T" T "T" T "T" T "T" T "T" T

Unknown ". this is quite normal ashen program pass" on on "

result of "t is false". How to make program to invoke new result as long as he gets it? Simply redo when there isnew discovery. That is what I did in changing the function "analysing" of class "Analyse". I simply Add A

Recursive Call of Itself when "Readstack ()" Return True Which Means The Program Did Find New Value and Change IT.

Originally I am So Clever to Write this "ReadStack ()" As Bool-Return Function. Now It Proves it is Worth THE

Efforts!

D. Major Function:

A. Void Analyz :: Analysis (Bool bycontradiction)

Keep Finding The "Result" TYPE IN OPREC (THE Stack) And Call "Readstack", doing the analysis. Have to clear stack for

Next Expression Checking. When "Readstack ()" Returns True, Recursively Call Analyysing Itself to Restart

Analysising Job.

(Want TO KNOW MORE ABOUT LOGIC AND AnalySE CLASS FUNCTION? Click Here to See Power Logic.)

E. Moral:

Once Our Great Leader Chairman Mao Taught US That "Theory Cannot Be Tested and Developed Unless".

So, All The Program I wrote Needs to Be Put Into Practice TO GET Qualified and Developed.

#include

Using namespace std;

Const int stacklimit = 150;

ENUM LOGICOP

{AND, OR, NOT, CONDitional};

ENUM OPSTATE

{OPERAND, OPERATOR, RESULT};

ENUM logicState

{POSITIVE = 1, Possible = 0, Negative = -1};

Bool Operator == (LogicState Self, LogicState Dummy)

{

Return (Self - Dummy) == 0);

}

// forward decaration

Class AnalySe;

Char * OPSTR [4] = {"AND", "or", "not", "conditional"};

Class Logic

{

Private:

STATIC AnalySE * Analyzes;

Static bool status;

Static int Logiccount;

CHAR * Express; Int index;

LogicState State;

Void catname (char *, const char *, logicop);

Static Logic * Temp [100];

Static int Tempcount;

Static Logic * true;

Static Logic * false;

Bool DefinedStatus;

LogicState and (LogicState Self, LogicState Dummy);

LogicState or (LogicState Self, LogicState Dummy);

LogicState Not (LogicState Self);

Void Pushstack (Const Logic * Self, Const Logic * Pdummy, Logicop Opcode);

Logic * getTemp () {return temp [Tempcount-1];

protected:

Voidinitialize ();

BOOL Compare (Const Logic & Dummy);

Void uninitialize ();

PUBLIC:

Analyze * getAnalyse ();

Logic (Const Char * NewExpress, Const LogicState Value = POSSIBLE);

~ Logic ();

Logic ();

Void prepare ();

Bool getStatus () const {return definedstatus;

Void setStatus (const bool newstatus) {definedstatus = newstatus;

Void setExpress (const char *);

Char * getExpress () const {return.com

Void setIndex (const INT newIndex) {index = newIndex;}

Int getIndex () const {return index;}

Void setState (logicstate newstate) {state = newstate;

LogicState getState () const {return state;}

Static const Int count () {return logiccount;

Logic & Operator && (Logic & Dummy);

Logic & Operator || (Logic & Dummy);

Logic & Operator! ();

Bool Operator == (Const Logic & Dummy);

Logic & Operator && (Const Bool Value);

Logic & Operator || (Const Bool Value);

Logic & Operator = (const logic& ";

Logic & Operator >> (Logic & Dummy);

Void Doanalysis (Bool bycontraDiction = false);

}

Union unknown

{

Logicop Operator = (Logicop Dummy);

Logic * Operator = (const logic * dummy);

Logic * logic;

Logicop OP;

}

Struct OPREC

{

OpState Optype;

Union unknown UNKNOWN;

}

Logic * unknown :: Operator = (const logic * dummy)

{

Logic = (logic *) Dummy;

Return Logic;

}

Logicop unknown :: Operator = (Logicop Dummy)

{

Op = Dummy;

Return OP;

}

Class AnalySe

{

Private:

BOOL Analyand (Logic * OP1, Logic * OP2, Logic * Result);

Bool Analyble (Logic * OP1, Logic * OP2, Logic * Result);

Bool Analyconditional (Logic * OP1, Logic * OP2, Logic * Result);

BOOL Analynot (Logic * OP1, Logic * Result);

Static OPREC * OPSTACK [stacklimit];

Int top;

Void clearstack ();

Logicop Findopcode (INT INDEX);

Bool SetNewop (Logic * Dummy, LogicState NewValue);

INT FINDPRERESULT (INT INDEX);

OPREC * gettop () {if (TOP> 0) Return Opstack [TOP-1]; else return null;}

PUBLIC:

Analyze ();

~ Analyze ();

Void Push (OPREC * NEWREC) {IF (TOP

OPREC * POP () {if (TOP> 0) Return Opstack [- TOP];

Bool ReadStack (int INDEX);

Void Analysis and (Bool bycontradiction);

}

Char * logicstr [3] = {"true", "unknown", "false"};

// this is what we need to pro:

/ / [(p || t) && (p -> q) && (q -> r) && (r -> s) && (s -> t)] -> T

Const int elementnum = 5;

ENUM ElementIndex {P, Q, R, S, T};

Logic element [ElementNum];

Char * FactStr [ElementNum] = {"P", "q", "r", "s", "t"};

Logic & formular ();

Void DisplayResult ();

Void setFactstr ();

int main ()

{

SetFactstr ();

Formular (). Doanalysis (TRUE);

DISPLAYRESULT ();

Return 0;

}

Logic & formular ()

{

Return ((Element [P] || Element [T]) && (Element [P] >> ELEMENT [Q]) &&

Element [q] >> Element [r]) && (Element [r] >> Element [s]) && (Element [s] >>

Element [t])) >> ELEMENT [T]);

}

Void setFactstr () {

For (int i = 0; i

{

ELEMENT [I] .SETEXPRESS (FactStr [I]);

}

}

Void DisplayResult ()

{

For (int i = 0; i

{

COUT << "/ nthe logic expression '" << FactStr [i] << "" "" "

Switch (Element [i] .getState ())

{

Case Positive:

Cout << Logicstr [0];

Break;

Case Possible:

Cout << Logicstr [1];

Break;

Case NEGATIVE:

COUT << Logicstr [2];

Break;

}

Cout << "/ n";

}

}

Analyze * logic :: getAnalyse ()

{

Return AnalySe;

}

Void Logic :: doalysing (Bool bycontradiction)

{

GetAnalyse () -> analyysing (bycontradiction);

}

Int AnalySE :: FINDPRERESULT (int INDEX)

{

While (INDEX> 0)

{

Index -;

IF (OpStack [index] -> OPTYPE == Result)

Return Index;

}

Return -1;

}

Void Analyz :: Analysis (Bool bycontradiction)

{

IF (Gettop () -> Optype! = Result

{

Cout << "/ NOPSTACK IS NOT Ended with Result: << Endl;

Return;

}

Else

{

INT i = TOP -1;

IF (! bycontradiction)

{

Opstack [i] -> Unknown.logic-> setState (POSITIVE);

}

Else

{

Opstack [i] -> unknown.logic-> setState (negative);

}

While (i! = -1)

{

IF (ReadStack (i))

{

Analysis;

}

// readstack (i);

I = FINDPRERESULT (i);

}

}

Clearstack ();

}

Logicop Analyz :: Findopcode (int index)

{

INT i = index;

While (Opstack [i] -> OPTYPE! = Operator)

{

I-;

}

Return Opstack [I] -> Unknown.op;

}

Bool Analyz :: readstack (int index)

{

Bool result = false;

Logicop opcode;

IF (OpStack [index] -> Optype! = result)

{

COUT << "stack error! / n";

Return False;

}

Opcode = Findopcode (INDEX);

Switch (opcode)

{

Case and:

Result = analynd (Opstack [index-3] -> unknown.logic, Opstack [index - 1] -> unknown.logic, Opstack [index] -> unknown.logic;

Break;

Case or:

Result = analyyor (Opstack [index-3] -> unknown.logic,

Opstack [INDEX - 1] -> Unknown.logic, Opstack [index] -> unknown.logic;

Break;

Case Conditional:

Result = analysis -> unknown.logic,

Opstack [INDEX - 1] -> Unknown.logic, Opstack [index] -> unknown.logic;

Break;

Case NOT:

Result = analysis -> unknown.logic, Opstack [index] -> unknown.logic;

Break;

}

Return Result;

}

Bool Analyze :: setNewop (Logic * Dummy, LogicState NewValue)

{

IF (Dummy-> getState ()! = newValue)

{

IF (Dummy-> getState ()! = possible)

{

Cout << "/ NYOU Are Changing Expression '" << Dummy-> getExpress () << "Value!"

Return False;

}

Else

{

Dummy-> setState (NewValue);

Return True;

}

}

Else

Return False;

}

Bool Analyz :: Analyans (Logic * OP1, Logic * OP2, Logic * Result)

{

Bool newop = false;

Switch (result-> getState ())

{

Case Positive:

IF (op1-> getState () == negative || OP2-> getState () == NEGATIVE

{

Cout << "/ Nthere IS ContraDictive At:" << result-> getExpress ();

Return False;

}

NEWOP = setnewop (op1, posacy) || setnewop (op2, posacy);

Break;

Case NEGATIVE:

IF (op1-> getState () == posTIVE && OP2-> getState () == posTIVE)

{

Cout << "/ Nthere IS ContraDictive At:" << result-> getExpress ();

Return False;

}

IF (op1-> getState () == posTIVE)

{

Newop = setnewop (OP2, NEGATIVE);

}

Else

{

IF (OP2-> getState () == posTIVE)

{

Newop = newop || SetNewop (OP1, NEGATIVE);

}

Break;

Case Possible:

Break;

}

Return False;

}

Bool Analyz :: Analyble (Logic * OP1, Logic * OP2, Logic * Result)

{

Bool newop = false;

Switch (result-> getState ())

{

Case Positive:

IF (op1-> getState () == Negative && op2-> getState () == NEGATIVE)

{

Cout << "/ Nthere IS ContraDictive At:" << result-> getExpress ();

Return False;

}

IF (OP1-> getState () == NEGATIVE)

{

NEWOP = setnewop (op2, posacy);

}

Else

{

IF (OP2-> getState () == NEGATIVE)

{

Newop = newop || SetNewop (OP1, POSTIVE);

}

}

Break;

Case NEGATIVE:

IF (op1-> getState () == posTIVE || OP2-> getState () == posTIVE)

{

Cout << "/ Nthere IS ContraDictive At:" << result-> getExpress ();

Return False;

}

Break;

Case Possible:

Break;

}

Return newop;

}

Bool Analyz :: Analyconditional (Logic * OP1, Logic * OP2, Logic * Result)

{

Bool newop = false;

Switch (result-> getState ())

{

Case Positive:

IF (op1-> getState () == posTIVE && OP2-> getState () == NEGATIVE

{

Cout << "/ Nthere IS ContraDictive At:" << result-> getExpress ();

Return False;

}

IF (op1-> getState () == posTIVE)

{

NEWOP = setnewop (op2, posacy);

}

Else

{

IF (OP2-> getState () == posTIVE)

{

Newop = setnewop (OP1, POSTIVE);

}

}

Break;

Case NEGATIVE:

IF (op1-> getState () == NEGATIVE || OP2-> getState () == posTIVE)

{

Cout << "/ Nthere IS ContraDictive At:" << result-> getExpress ();

Return False;

}

Newop = setnewop (OP2, NEGATIVE);

Newop = newop || SetNewop (OP1, POSTIVE);

Break;

Case Possible: Break;

}

Return newop;

}

Bool Analynot (Logic * OP1, Logic * Result)

{

Bool newop = false;

Switch (result-> getState ())

{

Case Positive:

IF (op1-> getState () == posTIVE)

{

Cout << "/ Nthere IS ContraDictive At:" << result-> getExpress ();

Return False;

}

Else

{

Newop = setnewop (OP1, NEGATIVE);

}

Break;

Case NEGATIVE:

IF (OP1-> getState () == NEGATIVE)

{

Cout << "/ Nthere IS ContraDictive At:" << result-> getExpress ();

Return False;

}

Else

{

Newop = setnewop (OP1, POSTIVE);

}

Break;

Case Possible:

Break;

}

Return newop;

}

Analyze * Logic :: Analyze = New Analyz

OPREC * Analyz :: opstack [stacklimit];

Void analyse :: clearstack ()

{

For (INT i = 0; i

{

Delete Opstack [i];

}

TOP = 0;

}

Analyzes :: analyse ()

{

TOP = 0;

}

Analyz :: ~ Analyze ()

{

Clearstack ();

}

LogicState Logic :: And (LogicState Self, LogicState Dummy)

{

IF (Self! = Dummy)

{

Return (LogicState) (Self * Dummy);

}

Else

{

Return Self;

}

}

LogicState Logic :: OR (LogicState Self, LogicState Dummy)

{

Return (Self> = DUMMY)? Self: Dummy);

}

LogicState Logic :: Not (LogicState Self)

{

Return (LogicState) (- 1 * Self);

}

void logic :: prepare ()

{

For (int i = 0; i

{

Free (Temp [i]);

}

Tempcount = 0;

}

Logic & Logic :: Operator >> (Logic & Dummy)

{

Char buffer [256];

Catname (Buffer, Dummy.getexpress (), Conditional

Temp [Tempcount] = New Logic (buffer);

Temp [Tempcount] -> SetState (Not (State), Dummy.getState ()));

Tempcount ;

Pushstack (this, & demmy, conditional);

Return * Temp [Tempcount-1];

}

void logic :: uninitialize ()

{

STATUS = TRUE;

For (int i = 0; i

{

Delete Temp [I];

}

}

Bool logic :: status = false;

Logic * logic :: temp [100] = {null}; // = new logic

INT logic :: tempcount = 0;

Logic * logic :: false = new logic ("false", negative);

Logic & Logic :: Operator = (Const Logic & Dummy)

{

setExpress (Dummy.Getexpress ());

SetState (Dummy.getState ());

Return * this;

}

Logic * logic :: true = new logic ("true", posTIVE) ;;

Logic & Logic :: Operator && (Const Bool Value)

{

IF (Value)

{

Return (* this) && (* true);

}

Else

{

Return (* this) && (* false);

}

}

Logic & Logic :: Operator || (Const Bool Value)

{

IF (Value)

{

Return (* this) || (* TRUE);

}

Else

{

Return (* this) || (* false);

}

}

BOOL Logic :: Operator == (Const Logic & Dummy)

{

Return Compare (DUMMY);

}

BOOL Logic :: Compare (Const Logic & Dummy)

{

Return (index == Dummy.GetIndex ());

}

Void Logic :: CatName (Char * Buffer, Const Char * SECOND, LOGICOP OPCODE)

{

STRCPY (Buffer, getExpress ());

STRCAT (Buffer, Opstr [OPCode]);

STRCAT (Buffer, Second);

}

Logic & Logic :: Operator! ()

{

Char buffer [256];

OPREC * PTR;

STRCPY (Buffer, Opstr [not]);

Strcat (buffer, getExpress ());

Temp [Tempcount] = New Logic (buffer);

Temp [Tempcount] -> SetState (not (getState ()));

Tempcount ;

PTR = New OPREC;

Ptr-> OPTYPE = Operator;

Ptr-> unknown = NOT;

Analyze-> Push (PTR);

PTR = New OPREC;

Ptr-> OPTYPE = OPERAND;

Ptr-> unknown = this;

Analyze-> Push (PTR);

PTR = New OPREC;

PTR-> OPTYPE = Result;

Ptr-> unknown = Temp [Tempcount-1];

Analyze-> Push (PTR);

Return * Temp [Tempcount-1];

}

Void Logic :: PushStack (Const Logic * Self, Const Logic * Pdummy, Logicop Opcode) {

OPREC * PTR;

PTR = New OPREC;

Ptr-> OPTYPE = OPERAND;

Ptr-> unknown = Self;

Analyze-> Push (PTR);

PTR = New OPREC;

Ptr-> OPTYPE = Operator;

Ptr-> unknown = opcode;

Analyze-> Push (PTR);

PTR = New OPREC;

Ptr-> OPTYPE = OPERAND;

Ptr-> unknown = pdummy;

Analyze-> Push (PTR);

PTR = New OPREC;

PTR-> OPTYPE = Result;

Ptr-> unknown = getTemp ();

Analyze-> Push (PTR);

}

Logic & Logic :: Operator && (Logic & Dummy)

{

Char buffer [256];

CatName (Buffer, Dummy.Gtexpress (), and);

Temp [Tempcount] = New Logic (buffer);

Temp [Tempcount] -> setState (getState (), Dummy.getState ())));

Tempcount ;

Pushstack (this, & dummy, and);

Return * Temp [Tempcount-1];

}

Logic & Logic :: Operator || (Logic & Dummy)

{

Char buffer [256];

CatName (Buffer, Dummy.Getexpress (), OR)

Temp [Tempcount] = New Logic (buffer);

Temp [Tempcount] -> setState (or (getState (), Dummy.getState ());

Tempcount ;

Pushstack (this, & dummy, or);

Return * Temp [Tempcount-1];

}

INT logic :: logiccount = 0;

void logic :: initialize ()

{

Express = NULL;

State = possible;

DefinedStatus = false;

SetIndex (Logiccount);

Logiccount ;

STATUS = false;

}

Logic :: ~ logic ()

{

IF (! status) // if you destroy one, then you destroy all !!!!!

{

uninitialize ();

}

IF (Express! = NULL)

{

FREE (Express);

}

}

Logic :: Logic (Const Char * NewExpress, Const LogicState Value)

{

INITIALIZE ();

SetExpress (newExpress);

SetState (Value);

}

Logic :: Logic ()

{

INITIALIZE ();

}

Void Logic :: setExpress (const char * newexpress)

{

INT I;

I = Strlen (NewExpress) 1; if (Express == Null)

{

Express = (char *) Malloc (i);

}

Else

{

Express = (char *) Realloc (void *) Express, i);

}

STRCPY (Express, NewExpress);

}

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

New Post(0)