Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases 11th British National Conference On Databases Bncod 11 Keele Uk July 7 9 1993 Proceedings Lncs 696

Authors: Michael F. Worboys ,Anna F. Grundy

1993rd Edition

3540569219, 978-3540569213

More Books

Students also viewed these Databases questions