<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 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="center"><b><font color="#FF0000" size="6">&nbsp;<span lang="en-ca">Logic 
Sets</span></font></b></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.<span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is the <span lang="en-ca">first</span> edition of my </b><span lang="en-ca"><b>LogicSets class and it has been lingering in my mind for a long time. </b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Idea of program</b></font></span></pre>
</div>
<div align="left">
  <pre><b>1¡£ Basic idea: </b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>To create a couple of classes: LogicSets---the container, Elements---the member class. You can imagine it is </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>something like linked list with parent having a list containing all his sons. Inside parent(LogicSets), I used</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>my own version of dynamic list which is a template. :)</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>My favourite part is the &quot;for all&quot; and &quot;exist&quot; function for a set. It is the implementation of logic universal </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>and existential quantifiers! I used a call-back-function to implement them. </b></span></pre>
</div>
<div align="left">
  <pre><b>2¡£ Program design: </b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">The two classes are recursively declared, so a forward declaration is necessary. Originally I plan to implement multi-type </span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">constructor for versatile. But later I found out it maybe useless. A general &quot;traverse&quot; call-back function is added besides </span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">the &quot;universal&quot; and &quot;existential&quot; quantifier functions. Actually there are quite few new codes apart from old &quot;myList&quot; which</span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">is a dynamic template list.</span></pre>
</div>
<div align="left">
  <pre><b>3¡£ Major function</b></pre>
  <blockquote>
    <blockquote>
  <pre><b> A.<span lang="en-ca">  bool LogicSets::exists(bool (*checkEach)(Elements* each))
     bool LogicSets::forAll(bool (*checkEach)(Elements* each))
     void LogicSets::forEach(void (*checkEach)(Elements* each))</span></b></pre>
  <pre><span lang="en-ca">These are three call-back functions. The first two are &quot;universal&quot; and &quot;existential&quot; quantifier for sets.</span></pre>
  <pre><span lang="en-ca">The last one is only general purpose callback.</span></pre>
    </blockquote>
  </blockquote>
  <pre><b>4¡£ Further improvement£º</b></pre>
  <pre><b>	A. <span lang="en-ca">To include this class in my further projects like &quot;relations&quot;.</span></b></pre>
</div>
<pre>#include &lt;iostream&gt;

using namespace std;

template&lt;class T&gt;
class mylist
{
private:
	T *flist;
	int LENGTH;
	int SIZE;
	int counter;
protected:
	void uninitialize();
	void initialize();
	bool checksize();
	void expand();
	int locate(T ptr);
public:
	mylist();
	~mylist();
	void add(T ptr);
	bool find(T ptr);
	void display();
	int count();
	T&amp; items(int index);
	void insert(int index, T ptr);

};


class LogicSets;

class Elements
{
	long ID;  //plan to use address to represent as ID
	char* name;
	LogicSets* parent;
public:
	LogicSets* belong() { return parent; }
	char* getName() { return name;}
	void setName(const char* givenName);
	bool belong(LogicSets* father){ return father== parent;}
	void setParent(LogicSets* father) { parent = father;}
	Elements(LogicSets* father);
	~Elements();
};

class LogicSets 
{
private:
	mylist&lt;Elements*&gt; lst;
public:
	bool include(Elements* pElement);
	void add(Elements* pElement);
	void create(char* givenName);
	bool forAll( bool (*checkEach)(Elements* each)); //call back function &quot;for all&quot;
	bool exists( bool (*checkEach)(Elements* each)); //call back function &quot;exist&quot;
	void forEach( void (*checkEach)(Elements* each)); //call back function for general purpose
	~LogicSets();
};

//to check all if all are string like &quot;this is no.5&quot;
bool allCheck(Elements* each)
{
	return strcmp(each-&gt;getName(), &quot;this is no.5&quot;) == 0;
}

bool existCheck(Elements* each)
{
	return strcmp(each-&gt;getName(), &quot;this is no.5&quot;) == 0;
}

void showEach(Elements* each)
{
	cout&lt;&lt;each-&gt;getName()&lt;&lt;endl;
}


int main()
{
	LogicSets someSets;
	char nameBuffer[15];
	char buffer[3];
	for (int i =0; i&lt;20; i++)
	{
		itoa(i, buffer, 10);
		strcpy(nameBuffer, &quot;this is no.&quot;);
		someSets.create(strcat(nameBuffer, buffer));
	}
	cout&lt;&lt;&quot;now output all elements by showing their name&quot;&lt;&lt;endl;
	someSets.forEach(showEach);
	cout&lt;&lt;endl;
	cout&lt;&lt;&quot;now for all check to see if all elments' name are 'this is no.5'&quot;&lt;&lt;endl;
	cout&lt;&lt;&quot;allcheck result is: &quot;&lt;&lt;(someSets.forAll(allCheck)?&quot;true&quot;:&quot;false&quot;)&lt;&lt;endl;
	cout&lt;&lt;endl;
	cout&lt;&lt;&quot;now check if exist an element such that its name is 'this is no.5'&quot;&lt;&lt;endl;
	cout&lt;&lt;&quot;exist check result is:&quot;&lt;&lt;(someSets.exists(existCheck)?&quot;true&quot;:&quot;false&quot;)&lt;&lt;endl;

	return 0;
}


LogicSets::~LogicSets()
{
	for (int i=0; i&lt;lst.count(); i++)
	{
		delete lst.items(i);
	}
}

void LogicSets::forEach(void (*checkEach)(Elements* each))
{
	for (int i = 0; i&lt; lst.count(); i++)
	{
		checkEach(lst.items(i));
	}
}


Elements::~Elements()
{
	if (name!=NULL)
	{
		free(name);
	}
}



Elements::Elements(LogicSets* father)
{
	parent = father;
	setName(&quot;no name&quot;);
}

void Elements::setName(const char* givenName)
{
	name = (char*)malloc(strlen(givenName)+1);
	strcpy(name, givenName);
}



void LogicSets::create(char* givenName)
{
	Elements* ptr;
	ptr = new Elements(this);
	ptr-&gt;setParent(this);
	ptr-&gt;setName(givenName);
	lst.add(ptr);
}

void LogicSets::add(Elements* pElement)
{
	lst.add(pElement);
}

bool LogicSets::forAll(bool (*checkEach)(Elements* each))
{
	for (int i=0; i&lt; lst.count() - 1; i++)
	{
		if (!checkEach(lst.items(i)))
		{
			return false;
		}
	}
	return true;
}

bool LogicSets::exists(bool (*checkEach)(Elements* each))
{
	for (int i=0; i&lt; lst.count() - 1; i++)
	{
		if (checkEach(lst.items(i)))
		{
			return true;
		}
	}
	return false;
}



//dynamic list

template&lt;class T&gt;
void mylist&lt;T&gt;::insert(int index, T ptr)
{
	if (!checksize())
		expand();

	if (counter == 0)
	{
		items(0) = ptr;
		counter++;
	}
	else
	{
		if (index&gt;=0&amp;&amp; index&lt;=counter)
		{
			int i=index;
			T hold1 = items(index), hold2= items(index+1);
			while (i&lt;counter)
			{	
				hold2 = items(i+1);
				items(i+1) = hold1;
				hold1 = hold2;				
				i++;
			}
			items(index) = ptr; //any exception trap???
			counter++;
		}
	}
}
			
template&lt;class T&gt;
int mylist&lt;T&gt;::locate(T ptr)
{
	int index = 0;
	while (items(index) &lt;ptr &amp;&amp;index &lt;counter)
	{
		index++;
	}
	return index;
}



template&lt;class T&gt;
bool mylist&lt;T&gt;::find(T ptr)
{
	int index = 0;

	index = locate(ptr);
	if (index == counter)
	{
		return false;
	}
	else
	{
		return (items(index) == ptr);
	}
}


template&lt;class T&gt;
int mylist&lt;T&gt;::count()
{
	return counter;
}

template&lt;class T&gt;
T&amp; mylist&lt;T&gt;::items(int index)
{
	return flist[index];
}


template&lt;class T&gt;
void mylist&lt;T&gt;::display()
{
	cout&lt;&lt;setiosflags(ios::showpoint|ios::fixed);
	for (int i = 0; i &lt; counter; i ++)
	{
		cout&lt;&lt;&quot;Number &quot;&lt;&lt;i&lt;&lt;&quot; item is:&quot;&lt;&lt;flist[i]&lt;&lt;endl;
	}
}

template&lt;class T&gt;
void mylist&lt;T&gt;::uninitialize()
{
	free(flist);
}

template&lt;class T&gt;
mylist&lt;T&gt;::~mylist()
{
	uninitialize();
}


template&lt;class T&gt;
void mylist&lt;T&gt;::add(T ptr)
{ 
	int index;
	index = locate(ptr);
	if (items(index)!=ptr)
	{
		insert(index, ptr);
	}
}

template&lt;class T&gt;
void mylist&lt;T&gt;::initialize()
{
	LENGTH = 10;
	SIZE = LENGTH;
	if ((flist =(T*)(malloc(sizeof(T) * SIZE)))==NULL)
		cout&lt;&lt;&quot;Unable malloc memory for size of &quot;&lt;&lt;SIZE&lt;&lt;endl;  //exception need to be handled here!!
	counter = 0;
}

template&lt;class T&gt;
bool mylist&lt;T&gt;::checksize()
{
	return (counter &lt; SIZE);
}

template&lt;class T&gt;
void mylist&lt;T&gt;::expand()
{
	SIZE += LENGTH;
	if ((flist = (T*)(realloc(flist, sizeof(T) * SIZE)))== NULL)
		cout&lt;&lt;&quot;Unable realloc memory for mylist of size &quot;&lt;&lt;SIZE&lt;&lt;endl;
}

template&lt;class T&gt;
mylist&lt;T&gt;::mylist()
{
	initialize();
}

</pre>
<pre>
</pre>

<p>¡¡ <span lang="en-ca">This simple output result demonstrate the 3 call-back 
functions:</span></p>

<p><span lang="en-ca">1. The forEach can &quot;traverse&quot; all members with 
programmer-defined call-back function to do specific job for </span>          


</p>

<p><span lang="en-ca">each element.</span></p>

<p><span lang="en-ca">2. The &quot;universal&quot; and &quot;existential&quot; quantifier check to 
give correct results.</span></p>

<p>now output all elements by showing their name<br>
this is no.1<br>
this is no.0<br>
this is no.11<br>
this is no.19<br>
this is no.18<br>
this is no.17<br>
this is no.16<br>
this is no.15<br>
this is no.14<br>
this is no.13<br>
this is no.12<br>
this is no.10<br>
this is no.9<br>
this is no.8<br>
this is no.7<br>
this is no.6<br>
this is no.5<br>
this is no.4<br>
this is no.3<br>
this is no.2<br>
<br>
now for all check to see if all elments' name are 'this is no.5'<br>
allcheck result is: false<br>
<br>
now check if exist an element such that its name is 'this is no.5'<br>
exist check result is:true<br>
¡¡</p>

<p><span lang="en-ca"> <br>
¡¡</span></p>

<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; <a href="Monopoly.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;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>