Article View: alt.msdos.programmer
Article #103Re: Help referencing pointers in C++
From: Fergus James Henderson
Date: Mon, 27 Jul 1992 00:11
Date: Mon, 27 Jul 1992 00:11
77 lines
3091 bytes
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!
Technical Details
Message-ID:
<9220910.1110@mulga.cs.mu.OZ.AU>Path:
arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!fjhReferences:
<14vb6jINNlr9@matt.ksu.ksu.edu>Show Full Headers
Complete Article Headers
Newsgroups: alt.msdos.programmer,comp.os.msdos.programmer,comp.programming,comp.unix.programmer Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!fjh From: fjh@cs.mu.OZ.AU (Fergus James Henderson) Subject: Re: Help referencing pointers in C++ Message-ID: <9220910.1110@mulga.cs.mu.OZ.AU> Organization: Computer Science, University of Melbourne, Australia References: <14vb6jINNlr9@matt.ksu.ksu.edu> Date: Mon, 27 Jul 1992 00:11:58 GMT Lines: 77