Hey I need some help coding this C assignment Objectives Thi
Hey, I need some help coding this C++ assignment.
Objectives:
This assignment will access your C++ language skills. After completing this assignment you will be able to do the following:
(1) allocate memory dynamically (2) implement a default, explicit-value and copy constructor, (3) a destructor, (4)parse string, (5) identify substrings in a string, and (6) fully implement and manage a string class.
In this assignment you will implement the WORD ADT (abstract data type) using a singly-linked list of characters. The WORD ADT is defined below. Call the class you implement \"WORD\". Remember, a singly-linked list is composed of nodes. You will also implement a class called \"character\" that has two fields: a char field called \"symbol\"; and a pointer field called \"next\". Essentially, each character is really a node in the list. Each node (character) in the list (word) will contain one alpha/numeric character of a word. Consider the following declaration of a character. A WORD is composed of characters:
class character
{
public:
char symbol;
character *next;
};
The state (private data members) of your WORD class should contain a pointer to the front of the list of characters called \"front\" and a pointer to the back of the list called \"back\". You may add more members to the state and more member functions to the behavior of the class if you determine necessary. Store the definition of your class in a file called \"word.cpp.\" and the declaration of your class in a file called \"word.h .\" You will implement all the code necessary to maintain a word. See the ADT below. Test the complete functionality of the class WORD. Use the file \"word_driver.cpp\" as a guide to help you understand and test all the functionality of the class. If you discover that you need more tests while implementing the functionality of WORD, then add more tests to your driver.
ADT---WORD
Data:
A set of characters
Operations:
1. IsEmpty: Check to see if the word A is empty; A is the current object;
2. Length: Determines the length of the word A; remember A is the current object;
6. operator<<: Overload the insertion operator as a friend function with chaining to print a word A;
7. operator= : Overload the assignment operator as a member function to take a string (C-style or C++ string, just be consistent in your implementation) as an argument and assigns its value to A, the current object;
8. operator= : Overload the assignment operator as a member function with chaining to take a word object as an argument and assigns its value to A, the current object;
9. operator+: Overload the ‘+” operator as a member function without chaining to add word B (adds the set of symbols that makep B\'s linked list to the back of A\'s linked list) to the back of word A; remember A is the current object;
10. Default constructor: The default constructor will initialize your state variables. The front of the linked list is initially set to NULL or 0; this implies a non-header node implementation of the link list.
11. Explicit-value constructor: This constructor will have one argument; a C-style string or a C++ string representing the word to be created;
12. Copy Constructor: Used during a call by value, return, or initialization/declaration of a word object;
13.Destructor: The destructor will de-allocate all memory allocated for the word. Put the message \"destructor called\ \" inside the body of the destructor.
14. IsEqual: Returns true if two word objects are equal; otherwise false; remember A is the current
***********************
word_driver.cpp
***********************
#include
#include
#include \"word.h\"
using namespace std;
int main()
{
WORD you;
cout<<\"************TEST#1*******************************\ \";
cout<<\"Testing the default constructor and printing empty word\ \";
cout< cout<<\"The length of you is \"<
cout<<\"************TEST#2*******************************\ \";
WORD me(\"AAAA0000AAA0000AAA\");
cout<<\"Testing the explicit-value constructor\ \";
cout< cout<<\"me is \ \"<
cout<<\"************TEST#3*******************************\ \";
WORD them = me;
cout<<\"Testing the copy constructor\ \";
cout<<\"them is \ \"<
cout<<\"************TEST#4*******************************\ \";
cout<<\"Testing length\ \";
cout<<\"The length of me is \"<
cout<<\"************TEST#5*******************************\ \";
WORD us;
us = \"AA\";
cout<<\"Testing operator= by assignment the value of \\\"AA\\\" to use\ \";
cout<<\"The word us is \ \"<
cout<<\"************TEST#6*******************************\ \";
WORD everyone = \"hello world\ \";
cout<<\"Testing operator= by assignment the value of \\\"hello world\\\" to use\ \";
cout<<\"The word everyone is \ \"<
cout<<\"************TEST#7*******************************\ \";
WORD our, him;
our = \"AAA000AAA000\";
us = \"XXXX\";
cout<<\"Testing IsEqual by testing to see inf us is equal to our \ \";
if (our.IsEqual(us))
cout<<\"The 2 words are equal\ \";
else
cout<<\"The 2 words are not equal\ \";
cout<<\"TEST SHOULD REVEAL THAT our AND us are not equal\ \";
cout<<\"*************************************************\ \";
cout<
cout<<\"************TEST#8*******************************\ \";
cout<<\"Adding 2 words by adding us to the back of their. Their is the current object \ \";
WORD their(\"AAAA0000AAA0000AAA\");
their + us;
cout<<\"their followed by us is \ \"<
cout<<\"************TEST#9*******************************\ \";
cout<<\"Adding 2 words, their2 is empty, by adding us to the back of their. Their is the current object \ \";
WORD their2(\"\");
their2 + us;
cout<<\"their followed by us is \ \"<
cout<<\"************TEST#10*******************************\ \";
cout<<\"Adding 2 words, their3 is empty, by adding us to the back of their. Their is the current object \ \";
WORD their3(\"\");
us + their3;
cout<<\"us followed by empty their3 is \ \"<
return 0;
}
***********************
word.h
***********************
#include
#include
using namespace std;
#ifndef WORD_H
#define WORD_H
class character
{
public:
char symbol;
character *next;
};
class WORD
{
public:
bool IsEmpty() { return front == 0; }
int Length(); //Length: Determines the length of the word A; remember A is the current object;
friend ostream & operator<<(ostream & out, const WORD &) { return out; } //Overload the insertion operator as a friend function with chaining to print a word A;
void operator=(const string & s) {}// Overload the assignment operator as a member function to take a
//string (C-style or C++ string, just be consistent in your implementation) as an argument and
//assigns its value to A, the current object;
WORD & operator=(const WORD & w) { WORD k; return k; } // Overload the assignment operator as a member function with chaining to take a word
//object as an argument and assigns its value to A, the current object;
void operator+(const WORD & B) {} //Overload the ‘+” operator as a member function without chaining to add word B
//(adds the set of symbols that makep B\'s linked list to the back of A\'s linked list) to the back of word A;
//remember A is the current object;
WORD() { front = back = 0; } //The default constructor will initialize your state variables.
//The front of the linked list is initially set to NULL or 0; this implies a non-header node
//implementation of the link list.
WORD(const string & s) { } //Explicit-value constructor: This constructor will have one argument;
//a C-style string or a C++ string representing the word to be created;
WORD(const WORD & org) {} // Copy Constructor: Used during a call by value, return, or initialization/declaration of a word object;
~WORD() {} //Destructor: The destructor will de-allocate all memory allocated for the word. Put the message
//\"destructor called\ \" inside the body of the destructor.
bool IsEqual(const WORD & B) { return true; } // Returns true if two word objects are equal; otherwise false; remember A is the current
private:
character *front, *back;
};
#endif
***********************
word.cpp
***********************
#include
#include
#include \"word.h\"
using namespace std;
int WORD::Length()
{
if (front == 0)
{
return 0;
}
else
{
character *p = front;
int count = 0;
while (p != 0)
{
count++;
p = p->next;
}
return count;
}
}
LINKED_LIST_CLASS::LINKED_LIST_CLASS()
{
cout<<\"Inside the default constructor\ \";
front = back = 0;
}
LINKED_LIST_CLASS::~LINKED_LIST_CLASS()
{
cout<<\"Destructor has been called\ \";
while (front != 0)
{
LIST_NODE *p = front;
front = front->next;
delete p;
}
}
void LINKED_LIST_CLASS::Print()accessor
{
LIST_NODE *p = front;
cout<
while( p != 0)
{
cout<data<
p = p->next;
}
cout<
}
void LINKED_LIST_CLASS::Insert(const List_Type & key)
{
LIST_NODE *p = new LIST_NODE;
p->next =0;
p->data = key;
if (front == 0)
{
front = p;
back = front;
}
else
{
p->next = front;
front = p;
}
}
LIST_NODE * LINKED_LIST_CLASS::Search(const List_Type & key)
{
LIST_NODE *p = front;
while(p!=0)
{
if (p->data == key)
{
return p;
}
p = p->next;
}
return p;
}
void LINKED_LIST_CLASS::Remove(const List_Type & key)
{
LIST_NODE *p = Search(key);
if (p == 0)
{
cout<
}
else
{
if (p==front && front==back)
{
delete p;
front = back = 0;
}
else if (p==front)
{
front = front->next;
delete p;
}
else
{
LIST_NODE *back_ptr = front;
while(back_ptr!=0 && back_ptr->next!= p)
{
back_ptr = back_ptr->next;
}
if (p == back)
{
back = back_ptr;
}
back_ptr->next = p->next;
delete p;
}
}
}
Test 11 - 14 are unnecessary
Solution
home / study / engineering / computer science / questions and answers / please help! i need to implement the following. ...
Question: Please Help! I need to implement the following. I\'...
Please Help! I need to implement the following. I\'ve written the header file, I need help with the implementation file (word.cpp) . Below are the directions:
-Implement the WORD ADT (abstract data type) using a singly-linked list of characters. The WORD ADT is defined below. Call the class you implement \"WORD\". Define a class called \"character\" that has two fields, a char field called \"symbol\" and a pointer field called \"next\". Each node (character) in the list will contain one symbol of a word. The state of your class should contain a pointer to the front of the list of characters called \"front\". You may add more members to the state and more member functions to behavior of the class if your determine necessary. Store the definition of your class in a file called \"word.cpp.\" and the declaration of your class in a file called \"word.h .\" You will implement all the code necessary to maintain a word. See the ADT below. Test the complete functionality of the class WORD. Use the file \"driver.cpp\" as a guide to help you understand and test the class. If you discover that you need more tests while implementing the functionality of WORD, then add more tests to your driver.
ADT---WORD
Data:
A set of characters
Operations:
IsEmpty: Check to see if the word A is empty; A is the current object;
Length: Determines the length of the word A; remember A is the current object;
Insert: Inserts a copy of word B (adds symbols that makeup B\'s linked list to A\'s linked list) into word A at position p; remember A is the current object;
Remove: Deletes the first occurrence of word B (removes the first set of symbols that makeup B\'s linked list from A\'s linked list) from word A if it is there; remember A is the current object;
RemoveAll: Removes all occurrences of word B (removes each set of symbols that makeup B\'s linked list from A\'s linked list) from word A if it is there; remember A is the current object;
operator<<: Overload the insertion operator as a friend function with chaining to print a word A;
operator= : Overload the assignment operator as a member function to take a string as an argument and assigns its value to A, the current object;
IsEqual: Returns true if two words are equal; otherwise false; remember A is the current object;
SetEqual: Assigns word B (creates a copy of B\'s linked list and assigns it to A\'s linked list) to word A; remember A is the current object;
AddWords: Adds word B (adds the set of symbols that makep B\'s linked list to the back of A\'s linked list) to the back of word A; remember A is the current object;
Default constructor: The default constructor will initialize your state variables. The front of the linked list is initially set to NULL or 0;
Explicit-value constructor: This constructor will have one argument; a C++ string representing the word to be created;
Copy Constructor: Used during a call by value, return, or initialization/declaration;
Destructor: The destructor will de-allocate all memory allocated for the word. Put the message \"destructor called\ \" inside the body of the destructor.
*****************************************************************************************************
word.h
*****************************************************************************************************
include <iostream> //i/o stream library
#include <string> //c++ string library
using namespace std; //standard workspace
class character
{
public:
char symbol;
character *next;
};
class WORD
{
public:
WORD();
WORD(const WORD &);
~WORD();
bool IsEmpty();
int Length();
void Insert(WORD &, int pos);
void Remove (WORD &);
void RemoveAll (WORD &);
friend ostream & operator<<(ostream &, const WORD &);
WORD & operator = (const string &);
bool IsEqual(WORD &);
void SetEqual(WORD &);
void AddWords(WORD &);
character *Search(WORD &);
void Insert_ch(const char &);
private:
character *front; //pointer to the front of the list.
int length;
};
****************************************************************************************************
driver.cpp
***************************************************************************************************
#include <iostream>
#include <string>
#include \"word.h\"
using namespace std;
int main()
{
WORD you;
cout<<\"Testing the default constructor and printing empty word\ \";
cout<<you<<endl;
WORD me(\"123abc345abc129012\");
cout<<\"Testing the explicit-value constructor\ \";
cout<<me<<endl;
WORD them = me;
cout<<\"Testing the copy constructor\ \";
cout<< them <<\" = \"<< me << endl;
cout<<\"Testing length\ \";
cout<<\"The length of me is \"<<me.Length()<<endl;
cout<<\"The length of them is \"<<them.Length()<<endl;
cout<<\"The length of you is \"<<you.Length()<<endl;
cout<<\"Testing Insert by inserting me into you at position 0\ \";
you.Insert(me,0);
cout<<\"The word you is \"<<you<<endl;
WORD us;
us = \"abc\";
cout<<\"Testing operator= by assignment the value of \\\"abc\\\" to use\ \";
cout<<us;
them.Remove(us);
cout<<\"Testing Remove by removing us from them \ \";
cout<<\"The word them is \"<<them<<endl;
me.RemoveAll(us);
cout<<\"Testing RemoveAll by removing all occurrences of us in me \ \";
cout<<\"The word me is \"<<me<<endl;
WORD our, him;
our = \"XXXCCCYYY\";
us = \"XXXX\";
cout<<\"Testing IsEqual by testing to see inf us is equal to our \ \";
if (our.IsEqual(us))
cout<<\"The 2 words are equal\ \";
else
cout<<\"The 2 words are not equal\ \";
cout<<\"Testing SetEqual and IsEqual by setting the value of us to the value stored in our \ \";
our.SetEqual(us);
if (our.IsEqual(us))
cout<<\"The 2 words are equal\ \";
else
cout<<\"The 2 words are not equal\ \";
WORD their(\"abcABCDEF\");
WORD yo, child;
cout<<\"Testing AddStrings by adding a string to the back of an empty string \ \";
cout<<\"Adding to an empty word\ \";
yo.AddWords(their);
cout<<\"The value of yo is \"<<yo<<endl;
cout<<\"Adding an empty word to another word by adding an empty string to a non empty string \ \";
yo.AddWords(child);
cout<<\"The value of yo is \"<<yo<<endl;
cout<<\"Adding 2 words by adding us to the bac of their. Their is the current object \ \";
their.AddWords(us);
cout<<\"their followed by us is \"<<their<<endl;
return 0;
}









