






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: WORD ORIGINS; Subject: Linguistics; University: University of California - Santa Barbara; Term: Summer A 2009;
Typology: Assignments
1 / 11
This page cannot be seen from the preview
Don't miss anything!







Programming Assignment # Due Date: July 30, 2009 (Noon). 90 Points. Remember: Points will be deducted if turned in after the Due Date. Deadline: August 3, 2009 (Noon) Remember: No homework will be accepted after the Deadline You must work on this assignment independently.
For this assignment you will write a C++ program to manipulate a set of people that belong to sets of groups (or organizations). Each person has a name formed by at most 254 lower case English letters (without blanks). Each person has a telephone number formed by at most 254 decimal numbers (without blanks). Similarly, each group has a name formed by at most 254 lower case English letters (without blanks). Each group has a telephone number formed by at most 254 decimal numbers (without blanks). We will define all the operations for people and groups below. Each person must be represented by a derivation of classes Tree and TreeNode which we call PeopleTree and PeopleTreeNode defined below. The same holds for each group (GroupTree and GroupTreeNode). Note that we do not keep multiple copies of any name of a person or name of a group. The ordering of the names is alphabetical (like in a Word dictionary). If a name x is a prefix of a name y, then x is in alphabetical order before name y. The same holds for groups. For example, the name art is a prefix of the word arthur. Therefore art is in alphabetical order before arthur. This is the ordering produced by type string when we use #include
In this assignment you will be performing operations on a set of people and a set of groups. The operations are given below. For this assignment you will also implement appropriate constructors and destructor for for all your Classes. These functions should be similar in functionality to the ones discussed in class (see also the progs.html web page). You must also implement the following member functions for the class Tree.
Big Hint: The easiest way to implement some of these functions recursive. For example, traversing the whole tree in alphabetical order can be easily done by a procedure similar to recursive inorder traversal (remember CMPSC 20). You must implement your functions so that they solve the problems efficiently. Your main program will read in a set of commands to operate on a set of people and groups. The format for the input lines are given below. Everything is typed as indicated except for any thing surrounded by < and >. For example
You may assume that each input line is of the form indicated above with one blank separating every pair of adjacent words. You may also assume that there is a \n after the last character in each line. There is a simple program in the class web page that you may easily generalize to read the input lines.
Example: Processing of commands.
insert-person john 1239854 people={john} groups={} insert-person jack 3234657 people={jack,john} groups={} insert-person jane 4578345 people={jack,jane,john} groups={} insert-person ron 1255236 people={jack,jane,john,ron} groups={} insert-group acm 2374833 people={jack,jane,john,ron} groups={acm} insert-group ieee 8945756 people={jack,jane,john,ron} groups={acm,ieee} insert-group acm 6745684 people={jack,jane,john,ron} groups={acm,ieee} insert-group orsa 3489292 people={jack,jane,john,ron} groups={acm,ieee,orsa} insert-group golf 8348484 people={jack,jane,john,ron} groups={acm,ieee,golf,orsa} member-group acm people={jack,jane,john,ron} groups={acm,ieee,golf,orsa}
prints true member-group cam people={jack,jane,john,ron} groups={acm,ieee,golf,orsa} prints false member-group rron people={jack,jane,john,ron} groups={acm,ieee,golf,orsa} prints false member-group ron people={jack,jane,john,ron} groups={acm,ieee,golf,orsa} prints true print group people={jack,jane,john,ron} groups={acm,ieee,golf,orsa} The groups are: acm ieee golf join jack acm people={jack (acm),jane,john,ron} groups={acm (jack),ieee,golf,orsa} join jack ieee people={jack (acm,ieee),jane,john,ron} groups={acm (jack),ieee (jack),golf,orsa} join golf jack people={jack (acm,ieee),jane,john,ron} groups={acm (jack),ieee (jack),golf,orsa} join jane acm people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} print-group acm people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} The people in group acm are: jack jane print-person jack people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} jack is a member of groups: acm ieee print-person acm people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} cardinality-people people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} There are 4 people cardinality-group people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} There are 4 groups person 2 people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} The following people belong to 2 or more groups:
TreeNode::TreeNode(string w="") { name=w; leftchild=rightchild=0; }
void TreeNode::SetName(string w) {name=w;}
void TreeNode::SetRight(TreeNode* j) {rightchild=j;}
void TreeNode::SetLeft(TreeNode* j) {leftchild=j;}
string TreeNode::GetName() {return name;}
TreeNode* TreeNode::GetLeft() {return leftchild;}
TreeNode* TreeNode::GetRight() {return rightchild;}
class PeopleTreeNode : public TreeNode { public: void SetPhone(string& w) {phone=w; return;} string GetPhone() {return phone;} void SetGroups(List * ptr){} List * GetGroups(){} PeopleTreeNode(string& n, string& p) : TreeNode(n) {SetGroups(0); SetPhone(p); } private: string phone; List *groups; };
class GroupTreeNode : public TreeNode { public: void SetPhone(string& w) {phone=w; return;}
string GetPhone() {return phone;} void SetPeople(List * ptr){} List * GetPeople(){} GroupTreeNode(string& n, string& p) : TreeNode(n) {SetPeople(0); SetPhone(p); } private: string phone; List *people; };
class Tree { public: Tree() ; bool Insert(TreeNode); bool Insert(TreeNode, TreeNode); virtual void Insert(string& a, string& b){}; bool Member(string); bool Member(TreeNode, string); void Readlist(); private: TreeNode *root; };
Tree::Tree(){ root = 0; }
bool Tree::Insert(TreeNode * newnode) { TreeNode *current = root; if (current==0) { root = newnode; return true; } return Insert(root,newnode); }
bool Tree::Insert(TreeNode *ptr, TreeNode *newnode) { if (newnode->GetName() < ptr->GetName()) {if (ptr->GetLeft() == 0) {ptr->SetLeft(newnode); return true; }
public: void Insert(string& n, string& p) {PeopleTreeNode * ptr; ptr = new PeopleTreeNode(n,p); if (not Tree::Insert(ptr)) delete ptr; } People() : Tree(){}; };
class Group : public Tree { public: void Insert(string& n, string& p) {GroupTreeNode * ptr; ptr = new GroupTreeNode(n,p); if (not Tree::Insert(ptr)) delete ptr; } Group() : Tree(){}; };
main() { int numtests; string newelem;
// This is a test program to see how it works. // You need to modify a lot of stuff for hw6. // We will discuss this program in class on Thursday.
People t; cout << "Read in people names to store in the tree" << endl; t.Readlist();
Group p; cout << "Read in group names to store in the tree" << endl; p.Readlist();
cout << endl; cout << "num of tests for the people tree" << endl; cin >> numtests; for ( int i = 1 ; i <= numtests ; i = i + 1 ) { cout << "Input Person name to see if it is in the people group" << endl; cin >> newelem; cout << newelem << "IS A MEMBER?"
<< t.Member(newelem) << endl; } cout << endl; cout << "num of tests for the groups tree" << endl; cin >> numtests; for ( int i = 1 ; i <= numtests ; i = i + 1 ) { cout << "Input Group name to see if it is in the group tree" << endl; cin >> newelem; cout << newelem << "IS A MEMBER?" << p.Member(newelem) << endl; } }