home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / alt / msdos / programm / 2073 < prev    next >
Encoding:
Text File  |  1992-07-26  |  3.0 KB  |  89 lines

  1. Xref: sparky alt.msdos.programmer:2073 comp.os.msdos.programmer:8097 comp.programming:2113 comp.unix.programmer:3928
  2. Newsgroups: alt.msdos.programmer,comp.os.msdos.programmer,comp.programming,comp.unix.programmer
  3. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!fjh
  4. From: fjh@cs.mu.OZ.AU (Fergus James Henderson)
  5. Subject: Re: Help referencing pointers in C++
  6. Message-ID: <9220910.1110@mulga.cs.mu.OZ.AU>
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <14vb6jINNlr9@matt.ksu.ksu.edu>
  9. Date: Mon, 27 Jul 1992 00:11:58 GMT
  10. Lines: 77
  11.  
  12. you@matt.ksu.ksu.edu writes:
  13.  
  14. >Hello,
  15. >  I'm playing with C++, teaching it to myself.  What I would like to know how to
  16. >do is reference certain pointers in a weird way....let me explain.  I'm
  17. >using a trie ADT.  Each node has 27 children, A-Z plus one to see if it's a word
  18. >or not.  It's a type of spell checker.  You trace down the tree the letters of
  19. >the word and when you're done, you check to see if it's a valid word or not.  
  20. >Of course, if along that way you find a path is NULL then it isn't a word.  What
  21. >I had hoped could be done is something like this, where the word is in the
  22. >variable "inword":
  23. >
  24. >    char1 = toupper(inword[count]);
  25. >    if (dictionary->char1==NULL)
  26. >        notfound=1;
  27. >    else
  28. >        dictionary=dictionary->char1;
  29. >
  30. >Count keeps tract of which letter the program is searching for and when count
  31. >equals the length of the word, you check to see if the flag is set and if
  32. >it is, then it's a word, else it isn't.  I've looked at the enum datatype
  33. >but I haven't had any luck with it.  Any help would be greatly
  34. >appreciated.  What I don't want to do is this:
  35. >
  36. >      switch(*char1) {
  37. >         case 'A' : {
  38. >            if (current->a==NULL)
  39. >               done = 1;
  40. >            else
  41. >               current=current->a;
  42. >            };
  43. >         case 'B' : {
  44. >            if (current->a==NULL)
  45. >               done = 1;
  46. >            else
  47. >               current=current->b;
  48. >            };
  49. >         case 'C' : {
  50. >            if (current->c==NULL)
  51. >               done = 1;
  52. >            else
  53. >               current=current->c;
  54. >            };
  55. >.......
  56. >
  57. >                    Thanks, 
  58. >                        Kevin
  59.  
  60. Use an array of pointers:
  61.  
  62. struct trie {
  63.     int is_word;
  64.     struct trie *next[26];
  65. } *dictionary;    /* you better initialize this yourself! */
  66.  
  67. /* return TRUE if inword is in dictionary */
  68. int check_word(const char *inword) {
  69.     const struct trie *ptr;
  70.     int index, c;
  71.     for (ptr=dictionary; ptr && *inword; ptr=ptr->next[index]) {
  72.         assert(isalpha(*inword));
  73.         c = toupper(*inword++);
  74.         index = c - 'A';  /* works for ASCII but not for EBCDIC */
  75.         assert(0<=index && index<26
  76.     }
  77.     return ptr && ptr->is_word;
  78. }
  79. -- 
  80. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  81. This .signature VIRUS is a self-referential statement that is true - but 
  82. you will only be able to consistently believe it if you copy it to your own
  83. .signature file!
  84. -- 
  85. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  86. This .signature VIRUS is a self-referential statement that is true - but 
  87. you will only be able to consistently believe it if you copy it to your own
  88. .signature file!
  89.