Question: Our project is the development of a miniature text-editing program. This program will allow only a few simple commands and is, therefore, quite primitive in

Our project is the development of a miniature text-editing program. This program will allow only a few simple commands and is, therefore, quite primitive in comparison with a modern text editor or word processor. Even so, it illustrates some of the basic ideas involved in the construction of much larger and more sophisticated text editors.

The Node Class

Chapter 5 of your text introduced a Node class and linked list toolkit of functions that could be used to create and maintain a singly linked list using this class. I have provided a version of this class and toolkit functions. You are to add five functions to this toolkit (prototypes to the .h file, function definitions to the .cpp file ).

void list_tail_remove(node*& headptr);

// Effect: The tail (last) node has been removed and returned to the heap;

// headptr is the head pointer of the new, shorter linked list.

// Precondition: headptr is the head pointer of a linked list (list has at least one node)

void list_tail_insert(node*& headptr, const node::value_type& entry);

// Effect: A new node containing the given entry has been added at

// the tail of the linked list; headptr points to the head of the new,

// longer linked list.

// Precondition: headptr is the head pointer of a linked list (list may be empty)

node* getLast(node* headptr)

// Effect: finds the last node in the list

// Precondition: headptr is the head pointer of a linked list with at least one node

// Returns: a pointer to the last node in the list

void outNode(ostream& out, const node* headPtr)

// Effect: prints the data contains in node pointed to by headPtr to output stream out

void outList(ostream& out, const node* headPtr)

//Precondition: list may be empty

//Effect: prints the data contained in a linked list. headPtr points to the 1st node of list.

You will want to code a simple driver to make sure that these functions work correctly before you go on. The driver can simply create a linked list and then call the functions verifying that they work. Test functions with empty lists where applicable. This driver is not to be handed in, but to help you (the final application is easier to debug if you are already assured that this class functions correctly).

The LineEditor class

A LineEditor object stores the contents of a textfile, and allows simple editing of the file. The textfile contents are stored as a collections of strings. There is always a current line number stored. You are to implement the LineEditor class using a linked list of strings, and an int currentLine as the two member variables (other member variables if you want, although not necessary)

Functions for LineEditor class:

Effect: LineEditor object is created

Postcondition: no strings are in the list (the list is empty). Current line is 0.

LineEditor::LineEditor()

Effect: LineEditor object initialized with data

Precondition: name is a ifstream object for a file that HAS BEEN SUCCESSFULLY OPENED

Postcondition: Lines from the file have been stored in the linked list (one line per node). The

current line has been set to the first line (if the file is empty, current line is 0)

void LineEditor::fill(ifstream& name)

Effect: determines the number of lines stored in the file

Precondition: None

Postcondition: object is unchanged

Returns: the number of lines in the file

int LineEditor::numLines()

Effect: access function for current line number

Precondition: None

Postcondtion: object is unchanged

int LineEditor::currentLine()

Effect: prints the current line to the filestream out; if object is empty, prints No Lines Stored

Precondition: None

Postcondition: object is unchanged

void LineEditor:: printCurrent(ostream& out)

Effect: adds new first line

Precondition: None

Postcondition: a new line has been added prior to existing lines

The current line has been reset to the first line (ie. 1)

void LineEditor::insertFirst(string line)

Effect: adds new last line

Precondition: None

Postcondition: a new line has been added following existing lines

The current line has been reset to the last line

void LineEditor::insertLast(string line)

Effect: adds a new line following the current line

Precondition: list is not empty

Postcondition: a new line has been inserted preceding the current line. The current

line number does not change

void LineEditor::insertCur (string line)

Effect: deletes the first line

Precondition: the exists 1 or more line in the file

Postcondition: the first line has been removed. The current line is now the new

first line. If there are no line remaining, the current line is 0.

void LineEditor::delFirst()

Effect: deletes the last line

Precondition: there is at least one line

Postcondition: the last line has been removed. The current line has been reset to

the new last line. If there are no lines remaining, the current line is set to 0.

void LineEditor::delLast()

Effect: deletes the current line

Precondition: list is not empty

Postcondition: the current line has been removed. If there are no more lines, the

current line is set to 0.

void LineEditor::delCur()

Effect: finds the first occurrence of a substring within the file

Precondition: None

Postcondition: If the substring was found, the current line has been changed to

the line in which that substring occurred

Returns: true if substring was found, false otherwise

bool LineEditor::findStr(const string& str)

Effect: replaces first occurrence of substring old with substring new in current line

Precondition: current line is not 0

Postcondition: current line may have been updated (if old occurred in line)

void LineEditor::replaceStr(const string& old, const string& new)

Effect: prints out contents of LineEditor, each line separated by

Precondition: None

Postcondition: LineEditor object is unchanged

friend ostream& operator<<(ostream& out, const LineEditor& obj)

The Application

You are to write a simple text editor, which does the following:

Asks the user for the name of file to be edited. If that file cannot be opened, the program gives a message and exits.

Creates a LineEditor object and then fills that object with text from the file

Allows the user to choose any of the following options, until he/she wants to exit

Allows the user to print out the entire file to the screen

Allows the user to print out the current number and current line to the screen

allows the user to add a new first line to the file

allows the user to add a new last line to the file

allows the user to insert a new line as current line

allows the user to delete the first line

allows the user to delete the last line

allows the user to delete the current line

allows the user to find the first occurrence in file of a substring

allows the user to replace one string from current line with another

allows the user to print out the number of lines in the file

Your program should print the current line following the processing of each user choice.

Once the user has chosen to exit, the file is written back be sure you have closed the input file, then reopen it for output. (You may want to use a separate file for output until you are sure your program works.)

Here is the node1.h:

#include // Provides size_t and NULL #include #include using namespace std;

class node { public: // TYPEDEF typedef string value_type; // CONSTRUCTOR node( const value_type& init_data = value_type( ), node* init_link = NULL ) { data_field = init_data; link_field = init_link; }

// Member functions to set the data and link fields: void set_data(const value_type& new_data) { data_field = new_data; } void set_link(node* new_link) { link_field = new_link; }

// Constant member function to retrieve the current data: value_type data( ) const { return data_field; }

// Two slightly different member functions to retreive // the current link: const node* link( ) const { return link_field; } node* link( ) { return link_field; } private: value_type data_field; node* link_field; };

// FUNCTIONS for the linked list toolkit std::size_t list_length(const node* head_ptr); void list_head_insert(node*& head_ptr, const node::value_type& entry); void list_insert(node* previous_ptr, const node::value_type& entry); node* list_search(node* head_ptr, const node::value_type& target); const node* list_search (const node* head_ptr, const node::value_type& target); node* list_locate(node* head_ptr, std::size_t position); const node* list_locate(const node* head_ptr, std::size_t position); void list_head_remove(node*& head_ptr); void list_remove(node* previous_ptr); void list_clear(node*& head_ptr); void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);;

Here is the node1.cpp:

#include "node1.h" #include // Provides assert #include // Provides NULL and size_t using namespace std;

size_t list_length(const node* head_ptr) // Library facilities used: cstdlib { const node *cursor; size_t answer;

answer = 0; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( )) ++answer; return answer; } void list_head_insert(node*& head_ptr, const node::value_type& entry) { head_ptr = new node(entry, head_ptr); }

void list_insert(node* previous_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, previous_ptr->link( )); previous_ptr->set_link(insert_ptr); }

node* list_search(node* head_ptr, const node::value_type& target) // Library facilities used: cstdlib { node *cursor; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( )) if (target == cursor->data( )) return cursor; return NULL; }

const node* list_search(const node* head_ptr, const node::value_type& target) // Library facilities used: cstdlib { const node *cursor; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( )) if (target == cursor->data( )) return cursor; return NULL; }

node* list_locate(node* head_ptr, size_t position) // Library facilities used: cassert, cstdlib { node *cursor; size_t i; assert (0 < position); cursor = head_ptr; for (i = 1; (i < position) && (cursor != NULL); i++) cursor = cursor->link( ); return cursor; }

const node* list_locate(const node* head_ptr, size_t position) // Library facilities used: cassert, cstdlib { const node *cursor; size_t i; assert (0 < position); cursor = head_ptr; for (i = 1; (i < position) && (cursor != NULL); i++) cursor = cursor->link( ); return cursor; }

void list_head_remove(node*& head_ptr) { node *remove_ptr;

remove_ptr = head_ptr; head_ptr = head_ptr->link( ); delete remove_ptr; }

void list_remove(node* previous_ptr) { node *remove_ptr;

remove_ptr = previous_ptr->link( ); previous_ptr->set_link( remove_ptr->link( ) ); delete remove_ptr; }

void list_clear(node*& head_ptr) // Library facilities used: cstdlib { while (head_ptr != NULL) list_head_remove(head_ptr); }

void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr) // Library facilities used: cstdlib { head_ptr = NULL; tail_ptr = NULL;

// Handle the case of the empty list. if (source_ptr == NULL) return; // Make the head node for the newly created list, and put data in it. list_head_insert(head_ptr, source_ptr->data( )); tail_ptr = head_ptr; // Copy the rest of the nodes one at a time, adding at the tail of new list. source_ptr = source_ptr->link( ); while (source_ptr != NULL) { list_insert(tail_ptr, source_ptr->data( )); tail_ptr = tail_ptr->link( ); source_ptr = source_ptr->link( ); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!