<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Puzzle</title>
</head>

<body>

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







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="proof.htm"><img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</a>          


</p>

</body>

</html>
