home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1991 / 02 / grdlagen / wc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-11  |  2.3 KB  |  115 lines

  1. //----------------------------------------------------------
  2. //                         WC.CPP                          -
  3. //        (c) 1991 by Thole Groeneveld & toolbox           -
  4. //----------------------------------------------------------
  5.  
  6. #include <iostream.h>
  7. #include <iomanip.h>
  8. #include <fstream.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include "cstring.h"
  12. #include "tree.h"
  13.  
  14. class StringNode : public String, public Node
  15. {
  16. public:
  17.  StringNode (char* s) : String(s), cnt(1) { };
  18.  int operator < (Node&);
  19.  int operator > (Node&);
  20.  void action();
  21.  void equal (Node*);
  22. private:
  23.  int cnt;
  24. };
  25.  
  26. int StringNode::operator < (Node& n) {
  27.  return *(String*)this < *(String*)(StringNode*)&n;
  28. }
  29.  
  30. int StringNode::operator > (Node& n) {
  31.  return *(String*)this > *(String*)(StringNode*)&n;
  32. }
  33.  
  34. void StringNode::action() {
  35.  cout << setw(20) << String(*this)
  36.   << " : "
  37.   << setw(6)  << cnt << endl;
  38. }
  39.  
  40. void StringNode::equal (Node* n) {
  41.  cnt++;
  42.  delete n;
  43. }
  44.  
  45. class my_ifstream : public ifstream {
  46. public:
  47.  my_ifstream();
  48.  void open (char*);
  49.  int  getword (char*);
  50. private:
  51.  char linebuf[256];
  52.  int  pos;
  53.  int  buflen;
  54.  int  DateiEOL;
  55. };
  56.  
  57. my_ifstream::my_ifstream() {
  58.  DateiEOL = 1;
  59.  pos      = 0;
  60.  buflen   = 0;
  61. }
  62.  
  63. void my_ifstream::open (char *f) {
  64.  char file[256];
  65.  strcpy (file, f);
  66.  ifstream::open (file);
  67.  while ( fail() ) {
  68.          cout << "Eingabedatei : ";
  69.  cin >> file;
  70.  ifstream::open (file);
  71.  }
  72. }
  73.  
  74. int my_ifstream::getword (char *cp) {
  75.  do {
  76.   if ( DateiEOL ) {
  77.        getline (linebuf, 256);
  78.   if ( eof() )
  79.        return 0;
  80.   buflen = strlen (linebuf);
  81.   pos = 0;
  82.   DateiEOL = 0;
  83.  }
  84.  while ((pos < buflen) && (!isalpha(linebuf[pos])))
  85.  pos++;
  86.  if ( pos >= buflen )
  87.  DateiEOL = 1;
  88.  }
  89.  while ( DateiEOL );
  90.  
  91.  int start = pos;
  92.  while ( isalnum(linebuf[pos]) )
  93.          pos++;
  94.  char sto = linebuf[pos];
  95.  linebuf[pos] = 0;
  96.  strcpy (cp, linebuf + start);
  97.  linebuf[pos] = sto;
  98.  return 1;
  99. }
  100.  
  101.  
  102. main (int argc, char *argv[]) {
  103.  my_ifstream mif;
  104.  Tree t;
  105.  char buffer[256];
  106.  
  107.  mif.open ( (argc > 1) ? (argv[1]) : (0) );
  108.  while ( mif.getword (buffer) )
  109.          t.insert (new StringNode (buffer) );
  110.  t.walk();
  111.  mif.close();
  112. }
  113. //----------------------------------------------------------
  114. //                     Ende von WC.CPP                     -
  115.