我的逻辑类
A.第一版
这个小程序是最初的版本。
1。 程序基本说明:这是一个逻辑计算的类,我的思想是这个类可以成为一个判断的实例(proposition),将来通过De Morgen's Law来进行
逻辑计算,并有可能进行逻辑推理,例如,以前我做过的“爱因斯坦”的问题,也许就可以用这个类来做,(很乐观啊!哈哈)
2。 程序思路:我的类为了将来的逻辑计算重载了很多操作符。
A. operator&& 逻辑与。
B. operator|| 逻辑或。
C. operator! 逻辑非。
D. operator== 比较操作符,我是用内定的index来判断两个实例是否相同,这个值得推敲。
E. operator= 复制操作符。
F. bool state 逻辑类的逻辑状态,是核心数据。
G. char* express 逻辑类的名字。
H. static Logic* temp[100] 你想不出这个是干什么的吧?我确实没有什么好主意,在连续运算过程中,每次会返回一个新逻辑
类对象,这对于逻辑运算来说是一些中间过程对象,我把这些中间对象都存在这里,最后再全部释放。我不知道重定向(redirect)
是怎样实现的,对于那些运算他们是返回操作对象本身,这样就相当于赋值改变了对象的状态。例如:A||B&&C, 在A||B运算后必须
返回一个逻辑类对象N来进行"N&&C"的运算,而如果用A或者B做返回,就必须改变A或者B的状态,这不是我所要得。因此,我就产生
新的对象,如果,不在程序结束时消灭,会有很多问题。
3。 主要函数介绍:
A. void catName(char*, const char*, LogicOp); 一个内部方法,主要是把两个对象的名字加上逻辑操作符合成新名字。我原
本用一个局域变量返回其指针,编译器总是报警,运行时候有很多莫名其妙的问题,最后,才明白要把buffer的指针传入。
4。 不足之处:
A. 所有的操作符返回的是对象指针,很不好用,应该改为对象引用。
B. 我原本计划写一个检查方法,比如:假定A is true,然后检查A&&B||C的真假,但是,这很复杂,因为,第一,表达式可能不定长,
这等于我要写一个parser来翻译command命令,天啊!第二,很多对象的真假可能是不确定的,我只有一些所谓“规则”,按照这种
规则和有限的确定的量去进行推理,这种算法,我想可能还没有。
#include <iostream>
using namespace std;
enum LogicOp
{AND, OR, NOT, CONDITIONAL};
char* OpStr[4] = {" AND ", " OR ", " NOT "," CONDITIONAL "};
enum ELEMENT
{R,S,I,W,D,H,F};
char* factStr[7] =
{"It is raining", "It is snowing", "I get ill","I get wet", "I need doctor",
"It is hot", "I have a fevor"};
class Logic
{
private:
static bool status;
static int logicCount;
char* express;
int index;
bool state;
void catName(char*, const char*, LogicOp);
static Logic* temp[100];
static int tempCount;
static Logic* TRUE;
static Logic* FALSE;
Logic* checkList[30];
bool definedStatus;
protected:
void initialize();
bool compare(const Logic& dummy);
void uninitialize();
public:
Logic(const char* newExpress, const bool value=false);
~Logic();
Logic();
bool getStatus() const {return definedStatus;}
void setStatus(const bool newStatus) {definedStatus = newStatus;}
void setExpress(const char*);
char* getExpress() const {return express;}
void setIndex(const int newIndex) { index = newIndex;}
int getIndex() const {return index;}
void setState(bool newState) { state = newState;}
bool getState() const{ return state;}
static const int count() {return logicCount;}
Logic* operator&&(const Logic& dummy);
Logic* operator||(const Logic& dummy);
Logic* operator!();
bool operator==(const Logic& dummy);
Logic* operator&&(const bool value);
Logic* operator||(const bool value);
Logic* operator=(const Logic& dummy);
Logic* operator>>(const Logic& dummy);
void addRule(const Logic* rule);
};
Logic element[7];
int main()
{
Logic A("A"),B("B"), C("C");
Logic* ptr;
A.setState(true); C.setState(true); cout<<"A's state is:"<<(A.getState()?"true":"false")<<endl; cout<<"B's state is:"<<(B.getState()?"true":"false")<<endl; cout<<"C's state is:"<<(C.getState()?"true":"false")<<endl; ptr = (*(A&&B))||C;
cout<<"now calculate (A&&B)||C which should be false now:"<<endl; cout<<"the result name is:"<<ptr->getExpress()<<endl; cout<<"the result is:"<<(ptr->getState()?"true":"false")<<endl; cout<<"now set C to be false and calculate (A&&B)||C again, result should be false:"<<endl; C.setState(false); cout<<"A's state is:"<<(A.getState()?"true":"false")<<endl; cout<<"B's state is:"<<(B.getState()?"true":"false")<<endl; cout<<"C's state is:"<<(C.getState()?"true":"false")<<endl;
ptr = (*(A&&B))||C;
cout<<"the result name is:"<<ptr->getExpress()<<endl; cout<<"the result is:"<<(ptr->getState()?"true":"false")<<endl;
return 0; }
Logic* Logic::operator >>(const Logic& dummy)
{
char buffer[256];
catName(buffer, dummy.getExpress(), CONDITIONAL);
temp[tempCount] = new Logic(buffer);
if (state&&(!dummy.getState()))
{
temp[tempCount]->setState(false);
}
else
{
temp[tempCount]->setState(true);
}
tempCount++;
return temp[tempCount-1];
}
void Logic::uninitialize()
{
status = true;
for (int i=0; i< tempCount;i++)
{
delete temp[i];
}
}
bool Logic::status = false;
Logic* Logic::temp[100] = {NULL};
int Logic::tempCount = 0;
Logic* Logic::FALSE = new Logic("FALSE", false);
Logic* Logic::operator =(const Logic& dummy)
{
setExpress(dummy.getExpress());
setState(dummy.getState());
return this;
}
Logic* Logic::TRUE = new Logic("TRUE", true);;
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];
strcpy(buffer, OpStr[NOT]);
strcat(buffer, getExpress());
temp[tempCount] = new Logic(buffer);
temp[tempCount]->setState(!getState());
tempCount++; return temp[tempCount-1]; }
Logic* Logic::operator &&(const Logic& dummy)
{
char buffer[256];
catName(buffer, dummy.getExpress(), AND);
temp[tempCount] = new Logic(buffer); temp[tempCount]->setState(getState()&&dummy.getState());
tempCount++; return temp[tempCount-1]; }
Logic* Logic::operator ||(const Logic& dummy)
{
char buffer[256];
catName(buffer, dummy.getExpress(), OR);
temp[tempCount] = new Logic(buffer);
temp[tempCount]->setState(getState()||dummy.getState());
tempCount++; return temp[tempCount-1]; }
int Logic::logicCount =0;
void Logic::initialize()
{
express = NULL;
state = false;
definedStatus = false;
setIndex(logicCount);
logicCount++;
}
Logic::~Logic()
{
if (!status) //if you destroy one, then you destroy all!!!!!
{
uninitialize();
}
if (express!=NULL)
{
free(express);
}
}
Logic::Logic(const char* newExpress, const bool value)
{
initialize();
setExpress(newExpress);
setState(value);
}
Logic::Logic()
{
initialize();
}
void Logic::setExpress(const char* newExpress)
{
if (express==NULL)
{
int i;
i = strlen(newExpress) +1;
express = (char*)malloc(i);
strcpy(express, newExpress);
}
}