<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>CFGReader</title>
</head>

<body>



<p align="center"><font size="6" color="#FF0000"><b>Strange Dictionary</b></font></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. First Edition</font></b></pre>
</div>
<div align="left">
  <pre><b><font size="3">This is first<span lang="en-ca"> edition of my </span>strange dictionary.</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <b>In Comp354 we need a dictionary to be used in our project, so I just write 
  such a small toy to enjoy </b></div>
<div align="left">
  　</div>
<div align="left">
  <b>myself.</b></div>
<div align="left">
  　</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <b>A dictionary is just a storage where you can look up a string to see if it 
  lies there. Usually we </b></div>
<div align="left">
  　</div>
<div align="left">
  <b>don't remove a word from our dictionary, do we? (However, in everyday life, 
  we do remove some words</b></div>
<div align="left">
  　</div>
<div align="left">
  <b>from our dictionary.) Therefore, I would only give two methods: addWord, 
  findWord. How to implement?</b></div>
<div align="left">
  　</div>
<div align="left">
  <b>The obvious method is to use a binary search tree, which I wrote before for 
  several times. But it has</b></div>
<div align="left">
  　</div>
<div align="left">
  <b>one drawback: it wastes a lot of space. i.e. space and spaceship has same 
  prefix, but in BST it is </b></div>
<div align="left">
  　</div>
<div align="left">
  <b>stored in two nodes. Why don't we just store letters in tree as nodes 
  instead of strings? That is</b></div>
<div align="left">
  　</div>
<div align="left">
  <b>each node is a letter. For example, &quot;space&quot; is saved as &quot;s&quot;, &quot;p&quot;, &quot;a&quot;, &quot;c&quot;, 
  &quot;e&quot;. And for the last </b></div>
<div align="left">
  　</div>
<div align="left">
  <b>node &quot;e&quot;, we use a flag to indicate word ends. &quot;spaceship&quot; will reuse most 
  of structure of &quot;space&quot;</b></div>
<div align="left">
  　</div>
<div align="left">
  <b>except &quot;ship&quot; is added to tree with four more letters. And the ending 
  letter &quot;p&quot; has a flag to say</b></div>
<div align="left">
  　</div>
<div align="left">
  <b>it is end. The searching will be very fast because in BST we use string 
  comparison, here, we use </b></div>
<div align="left">
  　</div>
<div align="left">
  <b>char to char comparison. And what's more, we saved a lot space, especially 
  when words has very </b></div>
<div align="left">
  　</div>
<div align="left">
  <b>similar prefix which is common in English.</b></div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><b><font size="3"><span lang="en-ca">1. </span>dictionary<span lang="en-ca">.h</span></font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>2. </span>dictionary<span lang="en-ca">.</span>cpp<span lang="en-ca">  </span></b></font></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>3. main.cpp (main)</b></font></span></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: </span>dictionary<span lang="en-ca">.h</span></b></font></pre>
</div>
<pre>const int LetterCount=26;

struct Letter
{
	char ch;
	Letter* next;
	Letter* son;
	bool end;
};

class Dictionary
{
private:
	Letter* root[52];
	Letter* current;
	Letter* findBrother(Letter* brother, char ch);
	Letter* createLetter(char ch);
	Letter* addSon(char ch);
	int indexOf(char ch);
public:
	bool addWord(char* str);
	bool findWord(char* str);
	void readFile(const char* fileName);
	Dictionary();
};

</pre>
<pre>
</pre>
<pre></pre>
<pre><font size="3" color="#FF0000"><span lang="en-ca"><b>file name: </b></span><b>dictionary.cpp </b></font></pre>
<pre>#include &lt;iostream&gt;
#include &lt;fstream&gt;

#include &quot;dictionary.h&quot;

using namespace std;


void Dictionary::readFile(const char* fileName)
{
	char buffer[20];
	ifstream f;
	f.open(fileName);
	while (!f.eof())
	{
		f&gt;&gt;buffer;
		addWord(buffer);
	}
}


bool Dictionary::findWord(char* str)
{
	char* ptr=str;
	Letter* hold;
	int index;
	//not empty string
	if (ptr!=NULL)
	{
		index = indexOf(*ptr);
		if (index==-1)
		{
			return false;
		}
		current=root[index];		
		ptr++;

		if (current-&gt;son==NULL)
		{
			//if string is a one-letter string
			if (*ptr=='\0')
			{
				//and there is a one-letter word in dictionary
				return current-&gt;end;
			}
			else
			{
				return false;
			}
		}
		hold=current;//
		current=current-&gt;son;
		
		while (*ptr!='\0')
		{				
			current=findBrother(current, *ptr);
			if (current==NULL)
			{
				return false;
			}
			if (current-&gt;ch==*ptr)
			{
				hold=current;
				current=current-&gt;son;
			}
			else
			{
				//not found
				return false;
			}
			ptr++;
		} 
		return hold-&gt;end;
	}
	//in my dictionary there is no empty string word
	return false;
}
	

Letter* Dictionary::createLetter(char ch)
{
	Letter* ptr=new Letter;
	ptr-&gt;ch=ch;
	ptr-&gt;end=false;
	ptr-&gt;next=NULL;
	ptr-&gt;son=NULL;
	return ptr;
}

//ch is not '\0'
Letter* Dictionary::findBrother(Letter* brother, char ch)
{
	Letter* hold=brother;
	if (brother==NULL)
	{
		return NULL;
	}
	while (hold-&gt;next!=NULL)
	{
		if (hold-&gt;ch==ch)
		{
			break;
		}
		hold=hold-&gt;next;
	}
	return hold;
}

Letter* Dictionary::addSon(char ch)
{
	//the word ends
	if (ch=='\0')
	{
		current-&gt;end=true;		
	}
	else
	{
		//need a new son
		if (current-&gt;son==NULL)
		{
			current-&gt;son=createLetter(ch);
			current=current-&gt;son;
		}
		else
		{
			//current-&gt;son is not NULL!!!
			current=findBrother(current-&gt;son, ch);
			//check if the current is the node
			if (current-&gt;ch!=ch)
			{
				current-&gt;next=createLetter(ch);	
				current=current-&gt;next;//add brother
			}	
			//else return current;!!!
		}
	}
	return current;
}


//add word actually add letter by letter till NULL, nonsense!
bool Dictionary::addWord(char* str)
{
	char* ptr=str;
	int index;
	if (*ptr!='\0')
	{
		index=indexOf(*ptr);
		if (index==-1)
		{
			return false;
		}
		current=root[index];		
		do
		{
			ptr++;
			current=addSon(*ptr);			
		} while (*ptr!='\0');
		return true;
	}
	return false;
}

Dictionary::Dictionary()
{
	for (int i=0; i&lt;LetterCount; i++)
	{
		root[i]=new Letter;
		root[i]-&gt;ch='A'+i;
		root[i]-&gt;next=NULL;
		root[i]-&gt;son=NULL;
	}
	for (i=0; i&lt;LetterCount; i++)
	{
		root[i+LetterCount]=new Letter;
		root[i+LetterCount]-&gt;ch='a'+i;
		root[i+LetterCount]-&gt;next=NULL;
		root[i+LetterCount]-&gt;son=NULL;
	}
}

int Dictionary::indexOf(char ch)
{
	if (ch-'A'&gt;=0&amp;&amp;ch-'Z'&lt;=0)
	{
		return ch-'A';
	}
	if (ch-'a'&gt;=0&amp;&amp;ch-'z'&lt;=0)
	{
		return ch-'a'+LetterCount;
	}
	return -1;
}


</pre>
<pre>
</pre>
<pre><font size="3" color="#FF0000"><span lang="en-ca"><b>file name: </b></span><b>main.cpp (main)</b></font>
</pre>
<pre>#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &quot;dictionary.h&quot;

using namespace std;

int main()
{
	ifstream f;
	char buffer[20];
	Dictionary D;
	D.readFile(&quot;c:\\nickdictionary.txt&quot;);

	f.open(&quot;c:\\nickDictionaryTest.txt&quot;);
	while (!f.eof())
	{
		f&gt;&gt;buffer;
		if (D.findWord(buffer))
		{
			cout&lt;&lt;&quot;dictionary has word :&quot;&lt;&lt;buffer&lt;&lt;endl;
		}
		else
		{
			cout&lt;&lt;&quot;dictionary has no word :&quot;&lt;&lt;buffer&lt;&lt;endl;
		}
	}
	



	return 0;
}</pre>
<pre>　</pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre><font color="#0000FF"><b>Here is the result:<span lang="en-ca"> The input file is </span>&quot;<a href="nickdictionary.txt">nickdictionary.txt</a>&quot; which should be place in root directory of your C</b></font></pre>

<pre><font color="#0000FF"><b>drive<span lang="en-ca">.</span> This file gives dictionary all words to add. Another file &quot;<a href="nickdictionarytest.txt">nickdictionarytest.txt</a>&quot; is used to test</b></font></pre>

<pre><font color="#0000FF"><b>if words is indeed stored in dictionary. Of course you know you can set up your own dictionary by using your own</b></font></pre>

<pre><font color="#0000FF"><b>txt files.<span lang="en-ca"> </span>But words can only start with letters and my dictionary is case-sensitive.</b></font></pre>

<pre>dictionary has word :a
dictionary has word :A
dictionary has no word :the1
dictionary has word :following
dictionary has no word :templatef
dictionary has no word :iss
dictionary has word :provided
dictionary has word :for
dictionary has word :use
dictionary has word :with
dictionary has word :the
dictionary has no word :rational
dictionary has no word :unified
dictionary has no word :process.
dictionary has word :text
dictionary has word :enclosed
dictionary has word :in
dictionary has word :square
dictionary has word :brackets
dictionary has word :and
dictionary has word :displayed
dictionary has word :in
dictionary has word :blue
dictionary has word :italics
dictionary has no word :style=infoBlue)
dictionary has no word :isn't
dictionary has word :included
dictionary has word :to
dictionary has word :provide
dictionary has word :guidance
dictionary has word :to
dictionary has word :the
dictionary has word :author
dictionary has word :and
dictionary has word :should
dictionary has word :be
dictionary has word :deleted
dictionary has word :before
dictionary has word :publishing
dictionary has word :the
dictionary has word :document.
dictionary has word :a
dictionary has word :paragraph
dictionary has word :entered
dictionary has word :following
dictionary has word :this
dictionary has word :style
dictionary has word :will
dictionary has word :automatically
dictionary has word :be
dictionary has word :set
dictionary has word :to
dictionary has word :normal
dictionary has word :this
dictionary has word :template
dictionary has word :is
dictionary has no word :directly
dictionary has no word :inspired
dictionary has word :by
dictionary has word :the
dictionary has no word :templates
dictionary has word :provided
dictionary has word :in
dictionary has word :the
dictionary has word :Rational
dictionary has word :Unified
dictionary has no word :Process,
dictionary has word :and
dictionary has word :are
dictionary has no word :all
dictionary has no word :available
dictionary has word :for
dictionary has no word :free
dictionary has word :on
dictionary has word :the
dictionary has word :Rational
dictionary has no word :Software
dictionary has no word :web
dictionary has no word :site.
dictionary has word :All
dictionary has no word :credit
dictionary has no word :go
dictionary has word :to
dictionary has word :Rational
dictionary has no word :Software,
dictionary has no word :except
dictionary has word :for
dictionary has word :the
dictionary has no word :edition
dictionary has no word :that
dictionary has no word :we
dictionary has no word :made,
dictionary has word :for
dictionary has no word :which
dictionary has word :Rational
dictionary has no word :Software
dictionary has word :is
dictionary has word :not
dictionary has no word :responsible
dictionary has word :for
dictionary has no word :ialog,
dictionary has word :automatic
dictionary has word :fields
dictionary has word :may
dictionary has word :be
dictionary has word :updated
dictionary has word :throughout
dictionary has word :the
dictionary has word :document
dictionary has word :by
dictionary has word :selecting
dictionary has word :Edit&gt;Select
dictionary has word :All
dictionary has no word :(or
dictionary has word :Ctrl-A)
dictionary has word :and
dictionary has word :pressing
dictionary has word :F9,
dictionary has word :or
dictionary has word :simply
dictionary has word :between
dictionary has word :displaying
dictionary has word :the
dictionary has word :field
dictionary has word :names
dictionary has word :and
dictionary has word :the
dictionary has word :A
dictionary has word :paragraph
dictionary has word :entered
dictionary has word :following
dictionary has word :this
dictionary has word :style
dictionary has word :a
Press any key to continue






</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; <a href="WhoAmI.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>