main.cpp

来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 98 行

CPP
98
字号
#include "../../C/UTILITY.H"
#include   "../../6/LINKLIST/LIST.H"
#include   "../../6/LINKLIST/LIST.CPP"
#include <string.h>
#include   "../../6/STRINGS/STRING.H"
#include   "../../6/STRINGS/STRING.CPP"
#include "../../9/RADIX/KEY.H"
#include "../../9/RADIX/KEY.CPP"
#include "../../9/RADIX/RECORD.H"
#include "../../9/RADIX/RECORD.CPP"
#include "TRIENODE.H"
#include "TRIENODE.CPP"
#include "TRIE.H"
#include "TRIE.CPP"
 
void write_entry(Record *x)
{
   if (x != NULL) {
      for (int i = 0; x->key_letter(i) != ' '; i++) cout << x->key_letter(i);
      cout << "\n";
   }
}
 
Key get_key()
{
   char c;
   int position;
   int key_size = 10;
   char *k = new char[key_size];
   for (position = 0; position < key_size; position++) k[position] = ' ';
   position = 0;
   do {
      cin.get(c);
   } while (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z'));
   while (c != '\n') {
      if (position < key_size) k[position] = c;
      position++;
      cin.get(c);
   }
   Key x(k);
   return x;
}
 
char get_command()
{
   char c, d;
   cout << "Select command (H for help) and press <Enter>:";
   while (1) {
      do {
         cin.get(c);
      } while (c == '\n');
      do {
         cin.get(d);
      } while (d != '\n');
      c = tolower(c);
      if (c == '#' || c == 'i' || c == 'c' ||
                      c == 'q' || c == 'p') {
         return c;
      }
      cout << "Please enter a valid command or H for help:";
      cout << "\n\t[P]rint trie\t[#] size of trie\n"
           << "\t[C]lear trie\t[I]nsert entry\n"
           << "\t[Q]uit.\n";
   }
}
 
int do_command(char c, Trie &test_tree)
{
   switch (c) {
   case 'c':
      test_tree.clear();
      cout << "Tree is cleared.\n";
      break;
   case 'p':
      test_tree.inorder(write_entry);
      break;
   case '#':
      cout << "The size of the tree is " << test_tree.size() << "\n";
      break;
   case 'q':
      cout << "Trie demonstration finished.\n";
      return 0;
   case 'i':
      cout << "Enter new (character string) key to insert:";
      Key x(get_key());
      Record y(x);
      test_tree.insert(y);
      break;
   }
   return 1;
}
 
main()
{
   Trie test_tree;
   while (do_command(get_command(), test_tree));
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?