🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.msdos.programmer
2 messages
2 total messages Started by you Sun, 26 Jul 1992 23:06
Help referencing pointers in C++
#102
Author: you
Date: Sun, 26 Jul 1992 23:06
45 lines
1999 bytes
Hello,
  I'm playing with C++, teaching it to myself.  What I would like to know how to
do is reference certain pointers in a weird way....let me explain.  I'm
using a trie ADT.  Each node has 27 children, A-Z plus one to see if it's a word
or not.  It's a type of spell checker.  You trace down the tree the letters of
the word and when you're done, you check to see if it's a valid word or not.  
Of course, if along that way you find a path is NULL then it isn't a word.  What
I had hoped could be done is something like this, where the word is in the
variable "inword":

	char1 = toupper(inword[count]);
	if (dictionary->char1==NULL)
		notfound=1;
	else
		dictionary=dictionary->char1;

Count keeps tract of which letter the program is searching for and when count
equals the length of the word, you check to see if the flag is set and if
it is, then it's a word, else it isn't.  I've looked at the enum datatype
but I haven't had any luck with it.  Any help would be greatly
appreciated.  What I don't want to do is this:

      switch(*char1) {
         case 'A' : {
            if (current->a==NULL)
               done = 1;
            else
               current=current->a;
            };
         case 'B' : {
            if (current->a==NULL)
               done = 1;
            else
               current=current->b;
            };
         case 'C' : {
            if (current->c==NULL)
               done = 1;
            else
               current=current->c;
            };
.......

					Thanks, 
						Kevin
Re: Help referencing pointers in C++
#103
Author: Fergus James Henderson
Date: Mon, 27 Jul 1992 00:11
77 lines
3091 bytes
you@matt.ksu.ksu.edu writes:

>Hello,
>  I'm playing with C++, teaching it to myself.  What I would like to know how to
>do is reference certain pointers in a weird way....let me explain.  I'm
>using a trie ADT.  Each node has 27 children, A-Z plus one to see if it's a word
>or not.  It's a type of spell checker.  You trace down the tree the letters of
>the word and when you're done, you check to see if it's a valid word or not.  
>Of course, if along that way you find a path is NULL then it isn't a word.  What
>I had hoped could be done is something like this, where the word is in the
>variable "inword":
>
>	char1 = toupper(inword[count]);
>	if (dictionary->char1==NULL)
>		notfound=1;
>	else
>		dictionary=dictionary->char1;
>
>Count keeps tract of which letter the program is searching for and when count
>equals the length of the word, you check to see if the flag is set and if
>it is, then it's a word, else it isn't.  I've looked at the enum datatype
>but I haven't had any luck with it.  Any help would be greatly
>appreciated.  What I don't want to do is this:
>
>      switch(*char1) {
>         case 'A' : {
>            if (current->a==NULL)
>               done = 1;
>            else
>               current=current->a;
>            };
>         case 'B' : {
>            if (current->a==NULL)
>               done = 1;
>            else
>               current=current->b;
>            };
>         case 'C' : {
>            if (current->c==NULL)
>               done = 1;
>            else
>               current=current->c;
>            };
>.......
>
>					Thanks, 
>						Kevin

Use an array of pointers:

struct trie {
	int is_word;
	struct trie *next[26];
} *dictionary;	/* you better initialize this yourself! */

/* return TRUE if inword is in dictionary */
int check_word(const char *inword) {
	const struct trie *ptr;
	int index, c;
	for (ptr=dictionary; ptr && *inword; ptr=ptr->next[index]) {
		assert(isalpha(*inword));
		c = toupper(*inword++);
		index = c - 'A';  /* works for ASCII but not for EBCDIC */
		assert(0<=index && index<26
	}
	return ptr && ptr->is_word;
}
-- 
Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
This .signature VIRUS is a self-referential statement that is true - but 
you will only be able to consistently believe it if you copy it to your own
.signature file!
-- 
Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
This .signature VIRUS is a self-referential statement that is true - but 
you will only be able to consistently believe it if you copy it to your own
.signature file!
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads