Question
Can you make a header file for this program called sentence h (in C++), Thank You #include #include #include sentence.h using namespace std; sentence::sentence(){ front
Can you make a header file for this program called sentence h (in C++), Thank You
#include
#include
#include "sentence.h"
using namespace std;
sentence::sentence(){
front = NULL;
back = NULL;
}
sentence::sentence(const string& s){
cout << "Explicit-value constructor for sentence class called." << endl;
string s1 = s;
front = NULL;
back = NULL; //back pointer set to NULL
int length = static_cast<int>(s.length()); //length variable is length of string
if(length == 0){ //if string is empty, return, nothing can be done with it
return;
}
front = new word;
word* currentWord = front;
int i = length;
while(i != 0){
if(isspace(s1.front())){ //if the first character is space
currentWord->term = s1.front(); //make the term a space
s1 = s1.substr(1); //shorten the string to not include the space anymore
i--;
}else{
int n = static_cast<int>(s1.find_first_of(" "));
if(n == -1) //if there are no more spacing, take the last index
n = static_cast<int>(s1.length());
currentWord->term = s1.substr(0,n); //make the term everything UP to the first space (aka a word)
i -= s1.substr(0,n).length(); //subtract the length of the last segment you substracted
s1 = s1.substr(n); //shorted the string not to include that word anymore
}
if(i <= 0){ //if we are at the last possible index..
currentWord->next = NULL; //have the next pointer be null as there are no other positions
break;
}else{ //if we are not at the end yet and still have more values
currentWord->next = new word; //return a pointer for the next word by allocating space using new
currentWord = currentWord->next; //the new current word is what the next pointer is pointing to
}
}
back = currentWord; //pointer to the back is the currentWord pointer
back->next = NULL;
}
sentence::sentence(const sentence& org){
if(org.front == NULL){
this->front = NULL;
//end the function
return;
}
this->front = new word;
word* ptr1 = new word;
ptr1 = org.front;
word* new_word = new word;
this->front = new_word;
while(ptr1 != NULL){
new_word->term = ptr1->term;
ptr1 = ptr1->next;
if(ptr1 != NULL){
new_word->next = new word;
new_word = new_word->next;
}else{
this->back = new_word;
break;
}
}
}
int sentence::length(){
int count = 0; //declare and initialize count as 0
if(this->front == NULL){ //if front is NULL, as in there is no list
return 0; //return zero as there is no size either
}
word* currentw2 = new word; //allocate space for word
currentw2 = this->front; //have the current word point to front
while(currentw2 != NULL){ //while current word is not null is not null
count += currentw2->term.length(); //increment the count
currentw2 = currentw2->next; //go to the next node
}
return count;
}
ostream& operator<<(ostream& out, const sentence& org){
if(org.front == NULL)
return out;
word* current_word = new word;
current_word = org.front;
while(current_word != NULL){
if(current_word->term.length() != 0)
out << current_word->term;
current_word = current_word->next;
}
return out;
}
sentence::~sentence(){
cout << "Destructor called" << endl;
word* temp = new word;
word* currentw4 = new word;
currentw4 = front;
while(currentw4 != NULL){
temp = currentw4;
currentw4 = currentw4->next;
delete temp;
}
}
bool sentence::isEqual(sentence& B){
if(this->length() != B.length()){
return false;
}
word* tempA = new word;
word* tempB = new word;
tempA = this->front;
tempB = B.front;
while(tempA != NULL && tempB != NULL){
if(tempA->term != tempB->term)
return false;
tempA = tempA->next;
tempB = tempB->next;
}
return true;
}
void sentence::add_back(string& s){
word* new_element = new word;
if(front == NULL){
this->front = new_element;
this->back = new_element;
new_element->term = s;
new_element->next = NULL;
}else{
this->back = new_element;
new_element->term = s;
new_element->next = NULL;
}
}
void sentence::add_front(string& s){
word* new_element = new word;
word* temp = new word;
temp = front;
if(front == NULL){
this->front = new_element;
this->back = new_element;
new_element->term = s;
new_element->next = NULL;
}else{
front = new_element;
new_element->term = s;
new_element->next = temp;
}
}
void sentence::operator=(const string& s){
string s1 = s;
front = NULL;
back = NULL;
int length = s.length();
if(length == 0){
return;
}
front = new word;
word* currentWord = front;
int i = length;
while(i != 0){
if(isspace(s1.front())){
currentWord->term = s1.front();
s1 = s1.substr(1);
i--;
}else{
int n = s1.find_first_of(" ");
currentWord->term = s1.substr(0,n);
i -= s1.substr(0,n).length();
if(i != 0)
s1 = s1.substr(n);
}
if(i == 0){
currentWord->next = NULL;
break;
}else{
currentWord->next = new word;
currentWord = currentWord->next;
}
}
back = currentWord;
//ensure that the back's next pointer is NULL
back->next = NULL;
}
//Assigns values of string to current setence object A
sentence& sentence::operator=(const sentence& w){
//if the orignal linked list is empty, they just make the copied one empty as well
if(w.front == NULL){
this->front = NULL;
//end the function
return *this;
}
//allocate space for front var
this->front = new word;
//allocate space for new temp var which will tranvswer the orignal linked list
word* ptr1 = new word;
//place the trans pointer to the front of the orignal
ptr1 = w.front;
//get a new word pointer which will make your new copied linked list
word* new_word = new word;
//have your new front var point to the new word pointer (your new front)
this->front = new_word;
//while the pointer that is transvering the orginal linked list is not null (aka at the end)
while(ptr1 != NULL){
//have the current word pointer's term be the same as the term from the orignal list
new_word->term = ptr1->term;
//advance the pointer
ptr1 = ptr1->next;
//if the pointer is not null after advancing (its not at the end)
if(ptr1 != NULL){
//alocate space in the copied array for another word(node)
new_word->next = new word;
//advance the copied string to the newly allocated space
new_word = new_word->next;
}else{
//set the back of the new copied sentence to be its last word
this->back = new_word;
break;
}
}
return *this;
}
void sentence::operator+(sentence& B){
//if B is empty, just return as nothing could be added
if(B.length() == 0){
return;
//if A is empty
}else if(this->length() == 0){
//make it equal to B
*this = B;
return;
}
word* ptr1 = new word;
word* ptr2 = new word;
ptr1 = this->getBack();
ptr2 = B.getFront();
while(ptr2 != NULL){
//alocate space for a word
ptr1->next = new word;
//adavnce to the next new node in A
ptr1 = ptr1->next;
//fill the allocated space with the term from B
ptr1->term = ptr2->term;
//advance to the next node in B
ptr2 = ptr2->next;
}
}
//Deletes the first occurrence of string s in the current sentence object
void sentence::remove(const string& s){
//allocate space for two word pointers
word* ptr1 = new word;
word* ptr2 = new word;
//if the string s is empty, jump to end, nothing to remove
if(s.length() == 0 || this->length() == 0)
return;
//one pointer at the front
ptr1 = this->front;
//another right in front of it leading
ptr2 = ptr1->next;
//if the first node matches the string, remove it and make front point to the next element.
if(ptr1->term == s){
//new front is the pointer that is leading
this->front = ptr2;
if(ptr1 != NULL)
delete ptr1;
return;
}
while(ptr2 != NULL){
if(ptr2->term == s && ptr2 != back){
ptr1->next = ptr2->next;
if(ptr2 != NULL)
delete ptr2;
return;
}else{
ptr2 = ptr2->next;
ptr1 = ptr1->next;
}
if(ptr2->term == s && ptr2 == back){
ptr1->next = ptr2->next;
back = ptr1;
back->next = NULL;
if(ptr2 != NULL)
delete ptr2;
return;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started