⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mytree.cpp

📁 《Visual C++ Bible》或者说是《Visual C++ 宝典》的对应的源码文件
💻 CPP
字号:
///////////////////////////////////////////////////////////////////////
//
// Mytree.cpp - use the tree structure for string keys and data.
//
// Description:
//
///////////////////////////////////////////////////////////////////////

#include <iostream.h>
#include <string.h>

#include "Tree.h"
static int testing	= 0;
#include "Mytree.h"

//
// Copy constructor function for the Mynode class.  Makes sure that
// the data pointers are clean copies.
//
Mynode::Mynode(Mynode & t)
{
	if (t.key == NULL) {
		key = NULL;
	} else {
		key = new char[strlen(t.key) + 1];
		if (key == (char *) NULL)
			return;
	}
	if (t.data == NULL) {
		data = NULL;
	} else {
		data = new char [strlen(t.data) + 1];
		if (data == (char *) NULL) {
			if (key != NULL) {
				delete [] key;
				key = (char *) NULL;
			}
			return;
		}
	}
	if (key != NULL && t.key != NULL)
		(void) strcpy(key, t.key);
	if (data != NULL && t.data != NULL)
		(void) strcpy(data, t.data);
}

//
// Special constructor function for the Mynode class, to make clean copies of
// the data strings and use them in the objects.
//
Mynode::Mynode(char *inkey, char *indata)
{
	if (inkey == NULL) {
		key = NULL;
	} else {
		key = new char[strlen(inkey)+1];
		if (key == (char *) NULL)
			return;
	}
	if (indata == NULL) {
		data = NULL;
	} else {
		data = new char[strlen(indata)+1];
		if (data == (char *) NULL) {
			if (key != NULL) {
				delete [] key;
				key = (char *) NULL;
			}
			return;
		}
	}
	if (key != NULL && inkey != NULL)
		(void) strcpy(key, inkey);
	if (data != NULL && indata != NULL)
		(void) strcpy(data, indata);
}

//
// Print the node to standard output.
//
void Mynode::print()
{
	char *k = key == NULL ? "(null)" : key;
	char *d = data == NULL ? "(null)" : data;

	cout <<  k << " -> " << d << endl;
}

//
// Fashion a Tree and Node of Mytree and Mynode, then call the
// underlying class member function to insert.
//
Mynode *Mytree::insert(char *keystring, char *datastring)
{
	Mynode *mnPtr = new Mynode(keystring, datastring);

	return(insert(mnPtr));
}


//
// Fashion a Tree and Node of Mytree and Mynode, then call the
// underlying class member function to insert.
//
Mynode *Mytree::insert(Mynode *mnPtr)
{
	Node *np = (Node *) new Mynode(*mnPtr);
	Tree *tp = (Tree *) this;
	Mynode *mnp = (Mynode *) tp->insert(np);
	return(mnp);
}

//
// Fashion a Tree and Node of Mytree and Mynode, then call the
// underlying class member function to seach.
//
Mynode *Mytree::search(char *keystring)
{
	Mynode *mnPtr = new Mynode(keystring, NULL);

	return(search(mnPtr));
}


//
// Fashion a Tree and Node of Mytree and Mynode, then call the
// underlying class member function to search.
//
Mynode *Mytree::search(Mynode *mnPtr)
{
	Node *np = (Node *) new Mynode(*mnPtr);
	Tree *tp = (Tree *) this;
	Mynode *mnp = (Mynode *) tp->search(np);
	return(mnp);
}

static const char NUL	= '\0';
static const char CR	= '\r';
static const char NL	= '\n';

static const int INPUT_LENGTH	= 128;

//
// This array is specially ordered to create a fairly balanced
// tree, when inserted.  Without special code to balance the
// tree, it's possible to have it wildly unbalanced.
//
static char *strings[30]	= {
	"Picard",	"captain",
	"Kirk",		"captain",
	"Spock",	"first officer",
	"Riker",	"first officer",
	"McCoy",	"doctor",
	"Crusher",	"doctor",
	"Scott",	"engineer",
	"LaForge",	"engineer",
	"Rand",		"yeoman",
	"Troi",		"counselor",
	"Sulu",		"helmsman",
	"Data",		"helmsman",
	"Chekhov",	"navigator",
	"Worf",		"navigator",
	NULL,		NULL
};

main()
{
	register char **cpp;
	char cBuf[INPUT_LENGTH];
	char *name, *station;
	Mytree personnel;

	cpp = strings;
	for (;;) {
		if (*cpp == NULL)
			break;

		name = *cpp++;
		station = *cpp++;

		personnel.insert(name, station);
	}

	personnel.list();

	for (;;) {
		cout << "Name? " << flush;
		cin.getline(cBuf, sizeof(cBuf));

		if (cin.eof() || !cin.good() || *cBuf == NUL)
			break;

		Mynode *result = personnel.search(cBuf);
		if (result == NULL)
			cout << "No such crew member as " << cBuf << endl;
		  else
		  	result->print();
	}

	return(0);
}

⌨️ 快捷键说明

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