Category : Files from Magazines
Archive   : CLM89DEC.ZIP
Filename : ARRAYS.CPP

 
Output of file : ARRAYS.CPP contained in archive : CLM89DEC.ZIP
LISTING 1.

// bst.hpp

#ifndef _BST
#define _BST 1
struct bst_nodes {
bst_nodes *left, *right;
char *string;
bst_nodes(char *);
~bst_nodes();
void forall(void (*)(bst_nodes *, ostream &), ostream &);
friend bst_nodes **lookup(bst_nodes **, char *);
};
#endif

LISTING 2.

// bst.cpp

#include
#include "bst.hpp"

// constructor
bst_nodes::bst_nodes(char *s)
{
left = right = NULL;
strcpy(string = new char[strlen(s) + 1], s);
}

// destructor
bst_nodes::~bst_nodes()
{
if (this != NULL) {
delete left;
delete string;
delete right;
}
}

// lookup - tree search primitive
bst_nodes **lookup(register bst_nodes **t, char *index)
{
int cc;
while (*t != NULL && (cc = strcmp(index, (*t)->string)) != 0)
t = (cc < 0) ? &(*t)->left : &(*t)->right;
return t;
}

// forall - tree traversal
void bst_nodes::forall(void (*ftn)(bst_nodes *, ostream &), ostream &s)
{
if (this != NULL) {
left->forall(ftn, s);
(*ftn)(this, s);
right->forall(ftn, s);
}
}

LISTING 3.

// freq.cpp

#include
#include "bst.hpp"

// string indexed array of counts
class count_nodes : bst_nodes {
int count;
count_nodes(char *s) : (s) {count = 0;}
friend void print(bst_nodes *, ostream &);
friend class count_arrays;
};
class count_arrays {
count_nodes *tree;
public:
count_arrays() {tree = NULL;}
~count_arrays() {if (tree != NULL) delete tree;}
int &operator[](char *);
friend ostream &operator<<(ostream &stream, count_arrays s)
{s.tree->bst_nodes::forall(print, stream);}
};

// print - count_nodes output
void print(bst_nodes *t, ostream &stream)
{
register count_nodes *s = (count_nodes *) t;
stream << s->string << "\t" << s->count << "\n";
}

// operator[] - access function
int &count_arrays::operator[](char *s)
{
register count_nodes *&t = (count_nodes *) *lookup(&tree, s);
if (t == NULL)
t = new count_nodes(s);
return t->count;
}

// "word" frequency counter
int main()
{
char word[80];
count_arrays word_table;
while (cin >> word)
++word_table[word];
cout << word_table;
}

LISTING 4.

// struct.cpp

#include
#include "bst.hpp"

// string indexed array of employees class
#define MINIMUM_WAGE 4.75

class employee_nodes : bst_nodes {
employee_nodes *left() {return (employee_nodes *) left;}
employee_nodes *right() {return (employee_nodes *) right;}
public:
int id;
float salary;
employee_nodes(char *s, int n, float sal = MINIMUM_WAGE) : (s)
{id = n; salary = sal;}
friend ostream &operator<<(ostream &stream, employee_nodes &s)
{stream << s.string << "\t#" << s.id << "\t$" << s.salary << "\n";}
friend void print(ostream &, employee_nodes *);
friend class employees;
};

class employees {
static int employee_number = 0;
employee_nodes *tree;
public:
employees() {tree = NULL;}
~employees() {if (this != NULL) delete this;}
employee_nodes &operator[](char *);
friend ostream &operator<<(ostream &stream, employees e)
{print(stream, e.tree);}
};

// print - print employees
void print(ostream &stream, employee_nodes *e)
{
if (e != NULL) {
print(stream, e->left());
stream << *e;
print(stream, e->right());
}
}

// operator[] - access function
employee_nodes &employees::operator[](char *s)
{
register employee_nodes *&t = (employee_nodes *) *lookup(&tree, s);
if (t == NULL)
t = new employee_nodes(s, ++employee_number);
return *t;
}

main()
{
employees payroll;
cout << "President: " << payroll["Jeff Taylor"]; // hire myself
payroll["Jeff Taylor"].salary *= 2.0; // give myself a raise
payroll["Ima Flunky"].salary = 5.00; // hire a flunky
payroll["The Gofer"].id = 7777; // add a gofer
cout << "\nPayroll:\n" << payroll;
cout << "\ndone\n";
}


  3 Responses to “Category : Files from Magazines
Archive   : CLM89DEC.ZIP
Filename : ARRAYS.CPP

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/