Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this practice you will create, implement and test a sequence4.template file. The material from Ch1 ~ 6 of the textbook can help you tremendously.

In this practice you will create, implement and test a sequence4.template file.

The material from Ch1 ~ 6 of the textbook can help you tremendously. The chapter 6 of the text book is focus on Template class. You need to spend more time on understanding template prefix, template parameter, typename and etc. I call these (template prefix, template parameter, .etc.) are template stuff. Implementing the template class almost the same as implementing a regular class, but you need to add the template stuff in the file and save the file name with a template extension. If you feel confusion with the template stuff in a file you can implement the class as a regular class and add the template stuff at the end of implementation. You are welcome to use more advance skills than the techniques introduce in the textbook to do the assignment.

Following is an introduction to some files in this program.

sequence4.h, the headers file for the new Sequence template class. You can start with the version and add your name and other documentation information at the top. Please pay attention to #include "node2.h" statement at the top of the file, it tells us that this class inherits from node2 class. So it is good idea to look into the note2 class. There is a #include "sequence4.template" statement at the end of the Sequence4.h file. This statement tells the compiler that we implement the sequence4 as a template class.

sequence4.template is a file you need to create/implement. Notice that the name of this file ends in ".template" rather than ".cpp". This is to remind you that template implementation files are never compiled on their own. Section 6.2 gives us more detail on the conversion from a regular class to a template class.

node2.h, a template version, is converted from node1.h, a class version. The file is provided. You can find it in CISP430A3. zip file. You can study the node1.h in chapter 5 to further understand the node class.

node2.template, a template version, is converted from node1.cpp, a class version. The file is provided. You can find it in CISP430A3. zip file. You can study the node1.cpp in chapter 5 to further understand the node class and node template. This file will provide us a good example of a template file.

sequence_test4.cpp is the same style interactive test program that you used with the earlier sequences. If you want to use it with the new sequence, then import it to your directory and open it with your editor.

sequence_exam4.cpp is the same style non-interactive test program as you use in the previous assignment. You can add this file to a compiler to grade the sequence4.template.

CISP430V4A3Exam.exe is an executable file which you can get to the result if you compile and run the node2.h, node2.template, sequence4.h, sequence4.template (proper implemented), sequence_exam4.cpp. When you click it you can see the following results.

pics:

http://imgur.com/a/scaus

CISP430V4A3Test.exe is an executable file which you can get to the interactive result if you compile and run the node2.h, node2.template, sequence4.h, sequence4.template (proper implemented), sequence_test4.cpp. When you click the CISP430V4A3Test.exe file and put in the proper information you should get to the following pictures.

pics:

http://imgur.com/a/0UYGT

Files:

"node2.h"

#ifndef NODE2_H

#define NODE2_H

#include // Provides NULL and size_t

#include // Provides iterator and forward_iterator_tag

namespace CISP430_A3

{

template

class node

{

public:

// TYPEDEF

typedef Item value_type;

// CONSTRUCTOR

node(const Item& init_data=Item( ), node* init_link=NULL)

{ data_field = init_data; link_field = init_link; }

// MODIFICATION MEMBER FUNCTIONS

Item& data( ) { return data_field; }

node* link( ) { return link_field; }

void set_data(const Item& new_data) { data_field = new_data; }

void set_link(node* new_link) { link_field = new_link; }

// CONST MEMBER FUNCTIONS

const Item& data( ) const { return data_field; }

const node* link( ) const { return link_field; }

private:

Item data_field;

node *link_field;

};

// FUNCTIONS to manipulate a linked list:

template

void list_clear(node*& head_ptr);

template

void list_copy

(const node* source_ptr, node*& head_ptr, node*& tail_ptr);

template

void list_head_insert(node*& head_ptr, const Item& entry);

template

void list_head_remove(node*& head_ptr);

template

void list_insert(node* & previous_ptr, const Item& entry);

template

size_t list_length(const node* head_ptr);

template

NodePtr list_locate(NodePtr head_ptr, SizeType position);

template

void list_remove(node* & previous_ptr);

template

NodePtr list_search(NodePtr head_ptr, const Item& target);

template

class node_iterator

// : public std::iterator

{

public:

node_iterator(node* initial = NULL)

{ current = initial; }

Item& operator *( ) const

{ return current->data( ); }

node_iterator& operator ++( ) // Prefix ++

{

current = current->link( );

return *this;

}

node_iterator operator ++(int) // Postfix ++

{

node_iterator original(current);

current = current->link( );

return original;

}

bool operator ==(const node_iterator other) const

{ return current == other.current; }

bool operator !=(const node_iterator other) const

{ return current != other.current; }

private:

node* current;

};

template

class const_node_iterator

// : public std::iterator

{

public:

const_node_iterator(const node* initial = NULL)

{ current = initial; }

const Item& operator *( ) const

{ return current->data( ); }

const_node_iterator& operator ++( ) // Prefix ++

{

current = current->link( );

return *this;

}

const_node_iterator operator ++(int) // Postfix ++

{

const_node_iterator original(current);

current = current->link( );

return original;

}

bool operator ==(const const_node_iterator other) const

{ return current == other.current; }

bool operator !=(const const_node_iterator other) const

{ return current != other.current; }

private:

const node* current;

};

}

#include "node2.template"

#endif

"sequence_exam4.cpp"

#include // Provides cout.

#include // Provides size_t.

#include "sequence4.h" // Provides the template sequence class

using namespace std;

using namespace CISP430_A3;

// Descriptions and points for each of the tests:

const size_t MANY_TESTS = 6;

const int POINTS[MANY_TESTS+1] = {

200, // Total points for all tests.

40, // Test 1 points

30, // Test 2 points

30, // Test 3 points

40, // Test 4 points

30, // Test 5 points

30 // Test 6 points

};

const char DESCRIPTION[MANY_TESTS+1][256] = {

"tests for sequence Class with a linked sequence",

"Testing insert, attach, and the constant member functions",

"Testing situations where the cursor goes off the sequence",

"Testing remove_current",

"Testing the copy constructor",

"Testing the assignment operator",

"Testing insert/attach for somewhat larger sequences"

};

// **************************************************************************

// bool test_basic(const sequence& test, size_t s, bool has_cursor)

// Postcondition: A return value of true indicates:

// a. test.size() is s, and

// b. test.is_item() is has_cursor.

// Otherwise the return value is false.

// In either case, a description of the test result is printed to cout.

// **************************************************************************

bool test_basic(const sequence& test, size_t s, bool has_cursor)

{

bool answer;

cout << "Testing that size() returns " << s << " ... ";

cout.flush( );

answer = (test.size( ) == s);

cout << (answer ? "Passed." : "Failed.") << endl;

if (answer)

{

cout << "Testing that is_item() returns ";

cout << (has_cursor ? "true" : "false") << " ... ";

cout.flush( );

answer = (test.is_item( ) == has_cursor);

cout << (answer ? "Passed." : "Failed.") << endl;

}

return answer;

}

// **************************************************************************

// bool test_items(sequence& test, size_t s, size_t i, double items[])

// The function determines if the test sequence has the correct items

// Precondition: The size of the items array is at least s.

// Postcondition: A return value of true indicates that test.current()

// is equal to items[i], and after test.advance() the result of

// test.current() is items[i+1], and so on through items[s-1].

// At this point, one more advance takes the cursor off the sequence.

// If any of this fails, the return value is false.

// NOTE: The test sequence has been changed by advancing its cursor.

// **************************************************************************

bool test_items(sequence& test, size_t s, size_t i, double items[])

{

bool answer = true;

cout << "The cursor should be at item [" << i << "]" << " of the sequence ";

cout << "(counting the first item as [0]). I will advance the cursor ";

cout << "to the end of the sequence, checking that each item is correct...";

cout.flush( );

while ((i < s) && test.is_item( ) && (test.current( ) == items[i]))

{

i++;

test.advance( );

}

if ((i != s) && !test.is_item( ))

{ // The test.is_item( ) function returns false too soon.

cout << " Cursor fell off the sequence too soon." << endl;

answer = false;

}

else if (i != s)

{ // The test.current( ) function returned a wrong value.

cout << " The item [" << i << "] should be " << items[i] << ", ";

cout << " but it was " << test.current( ) << " instead. ";

answer = false;

}

else if (test.is_item( ))

{ // The test.is_item( ) function returns true after moving off the sequence.

cout << " The cursor was moved off the sequence,";

cout << " but is_item still returns true." << endl;

answer = false;

}

cout << (answer ? "Passed." : "Failed.") << endl;

return answer;

}

// **************************************************************************

// bool correct

// (sequence& test, size_t s, size_t cursor_spot, double items[])

// This function determines if the sequence (test) is "correct" according to

// these requirements:

// a. it has exactly s items.

// b. the items (starting at the front) are equal to

// items[0] ... items[s-1]

// c. if cursor_spot < s, then test's cursor must be at

// the location given by cursor_spot.

// d. if cursor_spot >= s, then test must not have a cursor.

// NOTE: The function also moves the cursor off the sequence.

// **************************************************************************

bool correct

(sequence& test, size_t size, size_t cursor_spot, double items[])

{

bool has_cursor = (cursor_spot < size);

// Check the sequence's size and whether it has a cursor.

if (!test_basic(test, size, has_cursor))

{

cout << "Basic test of size() or is_item() failed." << endl << endl;

return false;

}

// If there is a cursor, check the items from cursor to end of the sequence.

if (has_cursor && !test_items(test, size, cursor_spot, items))

{

cout << "Test of the sequence's items failed." << endl << endl;

return false;

}

// Restart the cursor at the front of the sequence and test items again.

cout << "I'll call start() and look at the items one more time..." << endl;

test.start( );

if (has_cursor && !test_items(test, size, 0, items))

{

cout << "Test of the sequence's items failed." << endl << endl;

return false;

}

// If the code reaches here, then all tests have been passed.

cout << "All tests passed for this sequence." << endl << endl;

return true;

}

// **************************************************************************

// int test1( )

// Performs some basic tests of insert, attach, and the constant member

// functions. Returns POINTS[1] if the tests are passed. Otherwise returns 0.

// **************************************************************************

int test1( )

{

sequence empty; // An empty sequence

sequence test; // A sequence to add items to

double items1[4] = { 5, 10, 20, 30 }; // These 4 items are put in a sequence

double items2[4] = { 10, 15, 20, 30 }; // These are put in another sequence

// Test that the empty sequence is really empty

cout << "Starting with an empty sequence." << endl;

if (!correct(empty, 0, 0, items1)) return 0;

// Test the attach function to add something to an empty sequence

cout << "I am now using attach to put 10 into an empty sequence." << endl;

test.attach(10);

if (!correct(test, 1, 0, items2)) return 0;

// Test the insert function to add something to an empty sequence

cout << "I am now using insert to put 10 into an empty sequence." << endl;

test = empty;

test.insert(10);

if (!correct(test, 1, 0, items2)) return 0;

// Test the insert function to add an item at the front of a sequence

cout << "I am now using attach to put 10,20,30 in an empty sequence. ";

cout << "Then I move the cursor to the start and insert 5." << endl;

test = empty;

test.attach(10);

test.attach(20);

test.attach(30);

test.start( );

test.insert(5);

if (!correct(test, 4, 0, items1)) return 0;

// Test the insert function to add an item in the middle of a sequence

cout << "I am now using attach to put 10,20,30 in an empty sequence. ";

cout << "Then I move the cursor to the start, advance once, ";

cout << "and insert 15." << endl;

test = empty;

test.attach(10);

test.attach(20);

test.attach(30);

test.start( );

test.advance( );

test.insert(15);

if (!correct(test, 4, 1, items2)) return 0;

// Test the attach function to add an item in the middle of a sequence

cout << "I am now using attach to put 10,20,30 in an empty sequence. ";

cout << "Then I move the cursor to the start and attach 15 ";

cout << "after the 10." << endl;

test = empty;

test.attach(10);

test.attach(20);

test.attach(30);

test.start( );

test.attach(15);

if (!correct(test, 4, 1, items2)) return 0;

// All tests have been passed

cout << "All tests of this first function have been passed." << endl;

return POINTS[1];

}

// **************************************************************************

// int test2( )

// Performs a test to ensure that the cursor can correctly be run off the end

// of the sequence. Also tests that attach/insert work correctly when there is

// no cursor. Returns POINTS[2] if the tests are passed. Otherwise returns 0.

// **************************************************************************

int test2( )

{

const size_t TESTSIZE = 30;

sequence test;

size_t i;

// Put three items in the sequence

cout << "Using attach to put 20 and 30 in the sequence, and then calling ";

cout << "advance, so that is_item should return false ... ";

cout.flush( );

test.attach(20);

test.attach(30);

test.advance( );

if (test.is_item( ))

{

cout << "failed." << endl;

return 0;

}

cout << "passed." << endl;

// Insert 10 at the front and run the cursor off the end again

cout << "Inserting 10, which should go at the sequence's front." << endl;

cout << "Then calling advance three times to run cursor off the sequence ...";

cout.flush( );

test.insert(10);

test.advance( ); // advance to the 20

test.advance( ); // advance to the 30

test.advance( ); // advance right off the sequence

if (test.is_item( ))

{

cout << " failed." << endl;

return false;

}

cout << " passed." << endl;

// Attach more items until the sequence becomes full.

// Note that the first attach should attach to the end of the sequence.

cout << "Calling attach to put the numbers 40, 50, 60 ...";

cout << TESTSIZE*10 << " at the sequence's end." << endl;

for (i = 4; i <= TESTSIZE; i++)

test.attach(i*10);

// Test that the sequence is correctly filled.

cout << "Now I will test that the sequence has 10, 20, 30, ...";

cout << TESTSIZE*10 << "." << endl;

test.start( );

for (i = 1; i <= TESTSIZE; i++)

{

if ((!test.is_item( )) || test.current( ) != i*10)

{

cout << " Test failed to find " << i*10 << endl;

return 0;

}

test.advance( );

}

if (test.is_item( ))

{

cout << " There are too many items on the sequence." << endl;

return false;

}

// All tests passed

cout << "All tests of this second function have been passed." << endl;

return POINTS[2];

}

// **************************************************************************

// int test3( )

// Performs basic tests for the remove_current function.

// Returns POINTS[3] if the tests are passed.

// Otherwise returns 0.

// **************************************************************************

int test3( )

{

const size_t TESTSIZE = 30;

sequence test;

// Within this function, I create several different sequences using the

// items in these arrays:

double items1[1] = { 30 };

double items2[2] = { 10, 30 };

double items3[3] = { 10, 20, 30 };

size_t i; // for-loop control variable

// Build a sequence with three items 10, 20, 30, and remove the middle,

// and last and then first.

cout << "Using attach to build a sequence with 10,30." << endl;

test.attach(10);

test.attach(30);

cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl;

test.insert(20);

if (!correct(test, 3, 1, items3)) return 0;

cout << "Remove the 20, so entire sequence is now 10,30." << endl;

test.start( );

test.advance( );

test.remove_current( );

if (!correct(test, 2, 1, items2)) return 0;

cout << "Remove the 30, so entire sequence is now just 10 with no cursor.";

cout << endl;

test.start( );

test.advance( );

test.remove_current( );

if (!correct(test, 1, 1, items2)) return 0;

cout << "Set the cursor to the start and remove the 10." << endl;

test.start( );

test.remove_current( );

if (!correct(test, 0, 0, items2)) return 0;

// Build a sequence with three items 10, 20, 30, and remove the middle,

// and then first and then last.

cout << "Using attach to build another sequence with 10,30." << endl;

test.attach(10);

test.attach(30);

cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl;

test.insert(20);

if (!correct(test, 3, 1, items3)) return 0;

cout << "Remove the 20, so entire sequence is now 10,30." << endl;

test.start( );

test.advance( );

test.remove_current( );

if (!correct(test, 2, 1, items2)) return 0;

cout << "Set the cursor to the start and remove the 10," << endl;

cout << "so the sequence should now contain just 30." << endl;

test.start( );

test.remove_current( );

if (!correct(test, 1, 0, items1)) return 0;

cout << "Remove the 30 from the sequence, resulting in an empty sequence." << endl;

test.start( );

test.remove_current( );

if (!correct(test, 0, 0, items1)) return 0;

// Build a sequence with three items 10, 20, 30, and remove the first.

cout << "Build a new sequence by inserting 30, 10, 20 (so the sequence ";

cout << "is 20, then 10, then 30). Then remove the 20." << endl;

test.insert(30);

test.insert(10);

test.insert(20);

test.remove_current( );

if (!correct(test, 2, 0, items2)) return 0;

test.start( );

test.remove_current( );

test.remove_current( );

// Just for fun, fill up the sequence, and empty it!

cout << "Just for fun, I'll empty the sequence then fill it up, then ";

cout << "empty it again. During this process, I'll try to determine ";

cout << "whether any of the sequence's member functions access the ";

cout << "array outside of its legal indexes." << endl;

for (i = 0; i < TESTSIZE; i++)

test.insert(0);

for (i = 0; i < TESTSIZE; i++)

test.remove_current( );

// All tests passed

cout << "All tests of this third function have been passed." << endl;

return POINTS[3];

}

// **************************************************************************

// int test4( )

// Performs some tests of the copy constructor.

// Returns POINTS[4] if the tests are passed. Otherwise returns 0.

// **************************************************************************

int test4( )

{

const size_t TESTSIZE = 30;

sequence original; // A sequence that we'll copy.

double items[2*TESTSIZE];

size_t i;

// Set up the items array to conatin 1...2*TESTSIZE.

for (i = 1; i <= 2*TESTSIZE; i++)

items[i-1] = i;

// Test copying of an empty sequence. After the copying, we change original.

cout << "Copy constructor test: for an empty sequence." << endl;

sequence copy1(original);

original.attach(1); // Changes the original sequence, but not the copy.

if (!correct(copy1, 0, 0, items)) return 0;

// Test copying of a sequence with current item at the tail.

cout << "Copy constructor test: for a sequence with cursor at tail." << endl;

for (i=2; i <= 2*TESTSIZE; i++)

original.attach(i);

sequence copy2(original);

original.remove_current( ); // Delete tail from original, but not the copy

original.start( );

original.advance( );

original.remove_current( ); // Delete 2 from original, but not the copy.

if (!correct

(copy2, 2*TESTSIZE, 2*TESTSIZE-1, items)

)

return 0;

// Test copying of a sequence with cursor near the middle.

cout << "Copy constructor test: with cursor near middle." << endl;

original.insert(2);

for (i = 1; i < TESTSIZE; i++)

original.advance( );

// Cursor is now at location [TESTSIZE] (counting [0] as the first spot).

sequence copy3(original);

original.start( );

original.advance( );

original.remove_current( ); // Delete 2 from original, but not the copy.

if (!correct

(copy3, 2*TESTSIZE-1, TESTSIZE, items)

)

return 0;

// Test copying of a sequence with cursor at the front.

cout << "Copy constructor test: for a sequence with cursor at front." << endl;

original.insert(2);

original.start( );

// Cursor is now at the front.

sequence copy4(original);

original.start( );

original.advance( );

original.remove_current( ); // Delete 2 from original, but not the copy.

if (!correct

(copy4, 2*TESTSIZE-1, 0, items)

)

return 0;

// Test copying of a sequence with no current item.

cout << "Copy constructor test: for a sequence with no current item." << endl;

original.insert(2);

while (original.is_item( ))

original.advance( );

// There is now no current item.

sequence copy5(original);

original.start( );

original.advance( );

original.remove_current( ); // Delete 2 from original, but not the copy.

if (!correct

(copy5, 2*TESTSIZE-1, 2*TESTSIZE, items)

)

return 0;

// All tests passed

cout << "All tests of this fourth function have been passed." << endl;

return POINTS[4];

}

// **************************************************************************

// int test5( )

// Performs some tests of the assignment operator.

// Returns POINTS[5] if the tests are passed. Otherwise returns 0.

// **************************************************************************

int test5( )

{

const size_t TESTSIZE = 30;

sequence original; // A sequence that we'll copy.

double items[2*TESTSIZE];

size_t i;

// Set up the items array to conatin 1...2*TESTSIZE.

for (i = 1; i <= 2*TESTSIZE; i++)

items[i-1] = i;

// Test copying of an empty sequence. After the copying, we change original.

cout << "Assignment operator test: for an empty sequence." << endl;

sequence copy1;

copy1 = original;

original.attach(1); // Changes the original sequence, but not the copy.

if (!correct(copy1, 0, 0, items)) return 0;

// Test copying of a sequence with current item at the tail.

cout << "Assignment operator test: cursor at tail." << endl;

for (i=2; i <= 2*TESTSIZE; i++)

original.attach(i);

sequence copy2;

copy2 = original;

original.remove_current( ); // Delete tail from original, but not the copy

original.start( );

original.advance( );

original.remove_current( ); // Delete 2 from original, but not the copy.

if (!correct

(copy2, 2*TESTSIZE, 2*TESTSIZE-1, items)

)

return 0;

// Test copying of a sequence with cursor near the middle.

cout << "Assignment operator test: with cursor near middle." << endl;

original.insert(2);

for (i = 1; i < TESTSIZE; i++)

original.advance( );

// Cursor is now at location [TESTSIZE] (counting [0] as the first spot).

sequence copy3;

copy3 = original;

original.start( );

original.advance( );

original.remove_current( ); // Delete 2 from original, but not the copy.

if (!correct

(copy3, 2*TESTSIZE-1, TESTSIZE, items)

)

return 0;

// Test copying of a sequence with cursor at the front.

cout << "Assignment operator test: with cursor at front." << endl;

original.insert(2);

original.start( );

// Cursor is now at the front.

sequence copy4;

copy4 = original;

original.start( );

original.advance( );

original.remove_current( ); // Delete 2 from original, but not the copy.

if (!correct

(copy4, 2*TESTSIZE-1, 0, items)

)

return 0;

// Test copying of a sequence with no current item.

cout << "Assignment operator test: with no current item." << endl;

original.insert(2);

while (original.is_item( ))

original.advance( );

// There is now no current item.

sequence copy5;

copy5 = original;

original.start( );

original.advance( );

original.remove_current( ); // Deletes 2 from the original, but not copy.

if (!correct

(copy5, 2*TESTSIZE-1, 2*TESTSIZE, items)

)

return 0;

cout << "Checking correctness of a self-assignment x = x;" << endl;

original.insert(2);

original = original;

if (!correct

(original, 2*TESTSIZE-1, 1, items)

)

return 0;

// All tests passed

cout << "All tests of this fifth function have been passed." << endl;

return POINTS[5];

}

// **************************************************************************

// int test6( )

// Performs some basic tests of insert and attach for the case where the

// current capacity has been reached.

// Returns POINTS[6] if the tests are passed. Otherwise returns 0.

// **************************************************************************

int test6( )

{

const size_t TESTSIZE = 30;

sequence testa, testi;

double items[2*TESTSIZE];

size_t i;

// Set up the items array to conatin 1...2*TESTSIZE.

for (i = 1; i <= 2*TESTSIZE; i++)

items[i-1] = i;

cout << "Testing to see that attach works correctly when the ";

cout << "current capacity is exceeded." << endl;

for (i = 1; i <= 2*TESTSIZE; i++)

testa.attach(i);

if (!correct

(testa, 2*TESTSIZE, 2*TESTSIZE-1, items)

)

return 0;

cout << "Testing to see that insert works correctly when the ";

cout << "current capacity is exceeded." << endl;

for (i = 2*TESTSIZE; i >= 1; i--)

testi.insert(i);

if (!correct

(testi, 2*TESTSIZE, 0, items)

)

return 0;

// All tests passed

cout << "All tests of this sixth function have been passed." << endl;

return POINTS[6];

}

int run_a_test(int number, const char message[], int test_function( ), int max)

{

int result;

cout << endl << "START OF TEST " << number << ":" << endl;

cout << message << " (" << max << " points)." << endl;

result = test_function( );

if (result > 0)

{

cout << "Test " << number << " got " << result << " points";

cout << " out of a possible " << max << "." << endl;

}

else

cout << "Test " << number << " failed." << endl;

cout << "END OF TEST " << number << "." << endl << endl;

return result;

}

// **************************************************************************

// int main( )

// The main program calls all tests and prints the sum of all points

// earned from the tests.

// **************************************************************************

int main( )

{

int sum = 0;

cout << "Running " << DESCRIPTION[0] << endl;

sum += run_a_test(1, DESCRIPTION[1], test1, POINTS[1]); cout << sum << endl;

sum += run_a_test(2, DESCRIPTION[2], test2, POINTS[2]); cout << sum << endl;

sum += run_a_test(3, DESCRIPTION[3], test3, POINTS[3]); cout << sum << endl;

sum += run_a_test(4, DESCRIPTION[4], test4, POINTS[4]); cout << sum << endl;

sum += run_a_test(5, DESCRIPTION[5], test5, POINTS[5]); cout << sum << endl;

sum += run_a_test(6, DESCRIPTION[6], test6, POINTS[6]); cout << sum << endl;

cout << "If you submit this sequence now, you will have ";

cout << sum << " points out of the " << POINTS[0];

cout << " points from this test program. ";

system("PAUSE");

return EXIT_SUCCESS;

}

"sequence_test4.cpp"

// FILE: sequence_test.cpp // An interactive test program for the new sequence class #include // Provides toupper #include // Provides cout and cin #include // Provides EXIT_SUCCESS #include "sequence4.h" // With value_type defined as double using namespace std; using namespace CISP430_A3;

// PROTOTYPES for functions used by this test program: void print_menu( ); // Postcondition: A menu of choices for this program has been written to cout.

char get_user_command( ); // Postcondition: The user has been prompted to enter a one character command. // The next character has been read (skipping blanks and newline characters), // and this character has been returned.

void show_sequence(sequence display); // Postcondition: The items on display have been printed to cout (one per line).

double get_number( ); // Postcondition: The user has been prompted to enter a real number. The // number has been read, echoed to the screen, and returned by the function.

int main( ) { sequence test; // A sequence that well perform tests on char choice; // A command character entered by the user cout << "I have initialized an empty sequence of real numbers." << endl;

do { print_menu( ); choice = toupper(get_user_command( )); switch (choice) { case '!': test.start( ); break; case '+': test.advance( ); break; case '?': if (test.is_item( )) cout << "There is an item." << endl; else cout << "There is no current item." << endl; break; case 'C': if (test.is_item( )) cout << "Current item is: " << test.current( ) << endl; else cout << "There is no current item." << endl; break; case 'P': show_sequence(test); break; case 'S': cout << "Size is " << test.size( ) << '.' << endl; break; case 'I': test.insert(get_number( )); break; case 'A': test.attach(get_number( )); break; case 'R': test.remove_current( ); cout << "The current item has been removed." << endl; break; case 'Q': cout << "Ridicule is the best test of truth." << endl; break; default: cout << choice << " is invalid." << endl; } } while ((choice != 'Q'));

return EXIT_SUCCESS; }

void print_menu( ) // Library facilities used: iostream.h { cout << endl; // Print blank line before the menu cout << "The following choices are available: " << endl; cout << " ! Activate the start( ) function" << endl; cout << " + Activate the advance( ) function" << endl; cout << " ? Print the result from the is_item( ) function" << endl; cout << " C Print the result from the current( ) function" << endl; cout << " P Print a copy of the entire sequence" << endl; cout << " S Print the result from the size( ) function" << endl; cout << " I Insert a new number with the insert(...) function" << endl; cout << " A Attach a new number with the attach(...) function" << endl; cout << " R Activate the remove_current( ) function" << endl; cout << " Q Quit this test program" << endl; }

char get_user_command( ) // Library facilities used: iostream { char command;

cout << "Enter choice: "; cin >> command; // Input of characters skips blanks and newline character

return command; }

void show_sequence(sequence display) // Library facilities used: iostream { for (display.start( ); display.is_item( ); display.advance( )) cout << display.current( ) << endl; }

double get_number( ) // Library facilities used: iostream { double result; cout << "Please enter a real number for the sequence: "; cin >> result; cout << result << " has been read." << endl; return result; }

"sequence4.h"

#ifndef SEQUENCE4_H

#define SEQUENCE4_H

#include // Provides size_t

#include "node2.h" // Provides node class

namespace CISP430_A3

{

template

class sequence

{

public:

// TYPEDEFS and MEMBER CONSTANTS

typedef Item value_type;

typedef size_t size_type;

// CONSTRUCTORS and DESTRUCTOR

sequence( );

sequence(const sequence& source);

~sequence( );

// MODIFICATION MEMBER FUNCTIONS

void start( );

void advance( );

void insert(const value_type& entry);

void attach(const value_type& entry);

void operator =(const sequence& source);

void remove_current( );

// CONSTANT MEMBER FUNCTIONS

size_type size( ) const { return many_nodes; }

bool is_item( ) const { return (cursor != NULL); }

value_type current( ) const;

private:

node *head_ptr;

node *tail_ptr;

node *cursor;

node *precursor;

size_type many_nodes;

};

}

// The implementation of a template class must be included in its header file:

#include "sequence4.template"

#endif

"node2.template"

#include // Provides assert

#include // Provides NULL and size_t

namespace CISP430_A3

{

template

void list_clear(node*& head_ptr)

// Library facilities used: cstdlib

{

while (head_ptr != NULL)

list_head_remove(head_ptr);

}

template

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 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( );

}

}

template

void list_head_insert(node*& head_ptr, const Item& entry)

{

head_ptr = new node(entry, head_ptr);

}

template

void list_head_remove(node*& head_ptr)

{

node *remove_ptr;

remove_ptr = head_ptr;

head_ptr = head_ptr->link( );

delete remove_ptr;

}

template

void list_insert(node* & previous_ptr, const Item& entry)

{

node *insert_ptr;

insert_ptr = new node(entry, previous_ptr->link( ));

previous_ptr->set_link(insert_ptr);

}

template

size_t list_length(const node* head_ptr)

// Library facilities used: cstdlib

{

const node *cursor;

std::size_t answer;

answer = 0;

for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))

++answer;

return answer;

}

template

NodePtr list_locate(NodePtr head_ptr, SizeType position)

// Library facilities used: cassert, cstdlib

{

NodePtr cursor;

SizeType i;

assert(0 < position);

cursor = head_ptr;

for (i = 1; (i < position) && (cursor != NULL); ++i)

cursor = cursor->link( );

return cursor;

}

template

void list_remove(node* & previous_ptr)

{

node *remove_ptr;

remove_ptr = previous_ptr->link( );

previous_ptr->set_link(remove_ptr->link( ));

delete remove_ptr;

}

template

NodePtr list_search(NodePtr head_ptr, const Item& target)

// Library facilities used: cstdlib

{

NodePtr cursor;

for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))

if (target == cursor->data( ))

return cursor;

return NULL;

}

}

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

Students also viewed these Databases questions

Question

3. How much information do we need to collect?

Answered: 1 week ago