<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 align="left">&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> 
</b></font><font color="#FF0000" size="6"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Word Reader</b></font>    
</p>
<div align="left">
  <pre><b><font color="#ff0000" size="5">A.Very First Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is the very early edition of my own library(I am too ambitious to want to write my own lib.) And I make </b></pre>
</div>
<div align="left">
  <pre><b>a little amendment.</b></pre>
</div>
<div align="left">
  <pre><b>1¡£ Basic idea: A dynamic array which will automatically expand size like that in Delphi. And a word reader </b></pre>
</div>
<div align="left">
  <pre><b>which will read all word from a file. It is also use the dynamic list to store words.;</b></pre>
</div>
<div align="left">
  <pre><b>2¡£ Program design: </b></pre>
</div>
<div align="left">
  <pre><b>3¡£ Major function£º</b></pre>
</div>
<div style="WIDTH: 892px; HEIGHT: 102px" align="left">
  <pre><b>     A. int count();</b></pre>
  <pre><b>	  int wordCount(); </b></pre>
  <pre><b>	  int items(int index);</b></pre>
  <pre><b>	  char* items(int index);</b></pre>
  <pre><b>	These  methods exists in both list and wordReader, they are in similar functions for you to determine how</b></pre>
  <pre><b>	many items are stored and how to access them by index.</b></pre>
  <pre><b>	B. int filereader::readword()</b></pre>
  <pre><b>	This is the kernel function for reading words from file. I also use some trivial function like trim(),</b></pre>
  <pre><b>	filter() etc. which I forgot the purpose by myself.</b></pre>
  <pre>¡¡</pre>
  <pre><b>4¡£ Further improvement£º</b></pre>
  <pre><b>	A. These functions are not so good which I re-write when design my dictionary program.</b></pre>
  <pre><b>	B. However, some piece of code is still not bad as long as it runs well.</b></pre>
  <pre><b>	</b></pre>
</div>
<div align="left">
  <pre><b>		</b></pre>
</div>
<pre>#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;math.h&gt;</pre>
<pre>¡¡</pre>
<pre>using namespace std;
</pre>
<pre>enum FilterMode
{
	WORDONLY,
	WITHPUNCTUATION
};</pre>
<pre>¡¡</pre>
<pre>class mylist
{
private:
	int *flist;
	int LENGTH;
	int SIZE;
	int counter;
protected:
	void uninitialize();
	void initialize();
	bool checksize();
	void expand();
public:
	mylist();
	~mylist();
	void add(int ptr);
	void display();
	int count();
	int items(int index);</pre>
<pre>};

</pre>
<pre>class filereader
{
private:
	FILE *stream;
	mylist list;
//	char delimiter;
	int BUFFERSIZE;
	int WORDLENGTH;
//	char *buffer;
	bool wordonly;
	char trim();
	void expandBuffer(char *buf);
	bool filter(char ch);
protected:
	void initialize(bool wordonly);
	void uninitialize();</pre>
<pre>public:
	filereader(bool wordonly= true);
	~filereader();
	filereader(char *filename);
	void openfile(char *filename);
//	void setdelimiter(char delimiter);
//	int readword(char delimiter); 
	int readword();
	void display();
	const int wordCount(); 
	const char* items(const int index);</pre>
<pre>};</pre>
<pre>¡¡</pre>
<pre>int main()</pre>
<pre>{</pre>
<pre>	filereader reader;
	reader.openfile(&quot;c:\\myfile.txt&quot;);
	cout&lt;&lt;reader.readword();
	reader.display();</pre>
<pre>	cout&lt;&lt;&quot;\nnow use another way to show word\n&quot;;
	for (int i=0; i&lt; reader.wordCount(); i++)
	{
		cout&lt;&lt;&quot;word no. &quot;&lt;&lt;i&lt;&lt;&quot; is :  &quot;&lt;&lt;reader.items(i)&lt;&lt;endl;
	}</pre>
<pre>	return 0;
}</pre>
<pre>bool filereader::filter(char ch)
{
	if (wordonly)
		return (isalnum(ch));
	else
		return ((ch != 10)&amp;&amp;(ch != 32));
}
</pre>
<pre>void filereader::uninitialize()
{
	for (int i =0; i &lt; list.count(); i++)
	{
		free((void *)(list.items(i)));
	}
	fclose(stream);
}</pre>
<pre>filereader::filereader(bool wordonly)
{
	initialize(wordonly);
}</pre>
<pre>void filereader::display()
{
	for (int i=0; i&lt; list.count(); i++)
	{
		cout&lt;&lt;(char*)(list.items(i))&lt;&lt;endl;
	}
}</pre>
<pre>const int filereader::wordCount()
{
	return list.count();
}</pre>
<pre>const char* filereader::items(const int index)
{
	return (char*)list.items(index);
}</pre>
<pre>filereader::~filereader()
{
	uninitialize();
	</pre>
<pre>}
</pre>
<pre>void filereader::initialize(bool wordonly)
{
//	delimiter = ';';
	this-&gt;wordonly = wordonly;
	WORDLENGTH = 5;
//	BUFFERSIZE = 5;
//	buffer = (char *) malloc(sizeof(char) *BUFFERSIZE);
}</pre>
<pre>/*
int filereader::readword()
{
	this-&gt;readword(this-&gt;delimiter);
}
*/</pre>
<pre>char filereader::trim()
{
	char ch;
	ch = fgetc(stream);
	while (!feof(stream)&amp;&amp;(!isalnum(ch)))
	{
		ch = fgetc(stream);
	}
	return ch;
}
</pre>
<pre>filereader::filereader(char *filename)
{
	filereader();
	openfile(filename);</pre>
<pre>}</pre>
<pre>/*
void filereader::setdelimiter(char delimiter)
{
	this-&gt;delimiter = delimiter;
}
*/</pre>
<pre>void filereader::openfile(char *filename)
{
	if ((stream = fopen(filename, &quot;r&quot;))==NULL)
		cout&lt;&lt;&quot;unable open file &quot;&lt;&lt;filename&lt;&lt;endl;
}</pre>
<pre>int filereader::readword()
{
	char ch, *buf, *temp;
	int counter, wordLength, wordCounter=0; 	
	while (!feof(stream))
	{
		wordLength = WORDLENGTH;
		if ((buf = (char *)malloc(sizeof(char) * wordLength))== NULL)
			cout&lt;&lt;&quot;unable alloc memory!&quot;;
		counter = 0;
		ch = fgetc(stream);
		temp = buf;
		while (filter(ch)&amp;&amp;!feof(stream))
		{			
			*temp = ch;			
			counter++;
			if (counter &gt;= wordLength)
			{
				wordLength += WORDLENGTH;
				buf = (char *) realloc(buf, wordLength * sizeof(char));
				temp = buf + counter - 1;
			}
			temp++;
			ch = fgetc(stream);
		}
		if (counter!=0)
		{
			*temp = '\0';
			list.add((int)(buf));
			wordCounter++;
		}</pre>
<pre>	}
	return wordCounter;
}
			
</pre>
<pre>void filereader::expandBuffer(char *buf)
{
	</pre>
<pre>}
</pre>
<pre>int mylist::count()
{
	return counter;
}
</pre>
<pre>int mylist::items(int index)
{
	return flist[index];
}</pre>
<pre>void mylist::display()
{
	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;
	}
}
</pre>
<pre>void mylist::uninitialize()
{
//	for (int i = 0; i &lt; counter; i++)
	{
		//free(flist[i]);
	}
	free(flist);
}</pre>
<pre>mylist::~mylist()
{
	uninitialize();
}</pre>
<pre>void mylist::add(int ptr)
{
//	int *temp;	
	if (!checksize())
		expand();
//	temp = flist;
//	temp+=counter;
//	*temp = ptr;
	flist[counter] = ptr;
	counter++;
}</pre>
<pre>void mylist::initialize()
{
	LENGTH = 10;
	SIZE = LENGTH;
	if ((flist =(int*)(malloc(sizeof(int) * 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;
}</pre>
<pre>bool mylist::checksize()
{
	return (counter &lt; SIZE);
}</pre>
<pre>void mylist::expand()
{
	SIZE += LENGTH;
	if ((flist = (int*)(realloc(flist, sizeof(int) * SIZE)))== NULL)
		cout&lt;&lt;&quot;Unable realloc memory for mylist of size &quot;&lt;&lt;SIZE&lt;&lt;endl;
}
</pre>
<pre>mylist::mylist()
{
	initialize();
}</pre>
<pre>¡¡</pre>
<pre><img border="0" src="picture/wordReader.JPG" width="560" height="585"></pre>
<pre>¡¡</pre>
<pre>	

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


</a>          


</p>

</body>

</html>
