Question

c++ Sequence class is a class that supports sequence of integers. Run the project ad verify...

c++
Sequence class is a class that supports sequence of integers. Run the project ad verify that project works for integers. Next convert your sequence class into a template class. Demonstrate in your main function by using a sequence of int, float, string.

sequence.cxx
#include   // Provides assert
using namespace std;

namespace main_savitch_3
{
    sequence::sequence()
    {
        used = 0;
        current_index = 0;
    }

    void sequence::start()
    {
        current_index = 0;
    }

    void sequence::advance()
        // Library facilities used: assert.h
    {
        assert(is_item());
        current_index++;
    }

    void sequence::insert(const value_type& entry)
        // Library facilities used: assert.h
    {
        size_type i;

        // Check the precondition
        assert(size() < CAPACITY);

        // If there is no current item,
        // then set current_index to the front so that
        // the new entry will be placed at the front of the array.
        if (!is_item())
            current_index = 0;

        // Shift other items over, and insert the new entry at current_index
        for (i = used; i > current_index; i--)
            data[i] = data[i - 1];
        data[current_index] = entry;
        used++;
    }

    void sequence::attach(const value_type& entry)
        // Library facilities used: assert.h
    {
        size_type i;

        // Check the precondition
        assert(size() < CAPACITY);

        if (!is_item()) // There is no current item, so insert at end
            data[current_index] = entry;
        else // There is a current item, so move items over to make room
        {
            for (i = used; i > current_index + 1; i--)
                data[i] = data[i - 1];
            data[current_index + 1] = entry;
            current_index++;
        }

        used++;
    }

    void sequence::remove_current()
        // Library facilties used: assert.h
    {
        size_type i;

        assert(is_item());

        for (i = current_index + 1; i < used; i++)
            data[i - 1] = data[i];

        used--;
    }

    sequence::size_type sequence::size() const
    {
        return used;
    }

    bool sequence::is_item() const
    {
        return (current_index < used);
    }

    sequence::value_type sequence::current() const
        // Library facilities used: assert.h
    {
        assert(is_item());
        return data[current_index];
    }
}

sequence1.h

#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include <cstdlib>  // Provides size_t

namespace main_savitch_3
{
    class sequence
    {
    public:
        // TYPEDEFS and MEMBER CONSTANTS
        typedef double value_type;
        typedef std::size_t size_type;
        static const size_type CAPACITY = 30;
        // CONSTRUCTOR
        sequence();
        // MODIFICATION MEMBER FUNCTIONS
        void start();
        void advance();
        void insert(const value_type& entry);
        void attach(const value_type& entry);
        void remove_current();
        // CONSTANT MEMBER FUNCTIONS
        size_type size() const;
        bool is_item() const;
        value_type current() const;
    private:
        value_type data[CAPACITY];
        size_type used;
        size_type current_index;
    };
}

#endif
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks

Main.cpp

// FILE: sequence_test.cxx
// An interactive test program for the new sequence class
#include <cctype> // Provides toupper
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include "sequence.h" // With value_type defined as double
using namespace std;
using namespace main_savitch_3;

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

char getUserCommand( );
// 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.

template <typename T>
void showSequence(sequence<T> display);
// Postcondition: The items on display have been printed to cout (one per line).

double getNumber( );
// 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<double> test; // A sequence that we’ll perform tests on
char choice; // A command character entered by the user

cout << "I have initialized an empty sequence of real numbers." << endl;

do
{
printMenu( );
choice = toupper(getUserCommand( ));
switch (choice)
{
case '1': test.start( );
break;
case '2': test.advance( );
break;
case '3': if (test.is_item( ))
cout << "There is an item." << endl;
else
cout << "There is no current item." << endl;
break;
case '4': if (test.is_item( ))
cout << "Current item is: " << test.current( ) << endl;
else
cout << "There is no current item." << endl;
break;
case '5': showSequence(test);
break;
case '6': cout << "Size is " << test.size( ) << '.' << endl;
break;
case '7': test.insert(getNumber( ));
break;
case '8': test.attach(getNumber( ));
break;
case '9': test.remove_current( );
cout << "The current item has been removed." << endl;
break;
case '10': cout << "Ridicule is the best test of truth." << endl;
break;
default: cout << choice << " is invalid." << endl;
}
}
while ((choice != 'Q'));

return EXIT_SUCCESS;
}

void printMenu( )
// Library facilities used: iostream.h
{
cout << endl; // Print blank line before the menu
cout << "The following choices are available: " << endl;
cout << " 1 Activate the start( ) function" << endl;
cout << " 2 Activate the advance( ) function" << endl;
cout << " 3 Print the result from the is_item( ) function" << endl;
cout << " 4 Print the result from the current( ) function" << endl;
cout << " 5 Print a copy of the entire sequence" << endl;
cout << " 6 Print the result from the size( ) function" << endl;
cout << " 7 Insert a new number with the insert(...) function" << endl;
cout << " 8 Attach a new number with the attach(...) function" << endl;
cout << " 9 Activate the remove_current( ) function" << endl;
cout << " Q Quit this test program" << endl;
}

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

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

return command;
}

template <typename T>
void showSequence(sequence<T> display)
// Library facilities used: iostream
{
for (display.start( ); display.is_item( ); display.advance( ))
cout << display.current( ) << endl;
}

double getNumber( )
// 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;
}

sequence.h

#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include <cstdlib> // Provides size_t

namespace main_savitch_3
{
   template <class value_type>
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
  
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
// CONSTRUCTOR
sequence();
// MODIFICATION MEMBER FUNCTIONS
void start();
void advance();
void insert(const value_type& entry);
void attach(const value_type& entry);
void remove_current();
// CONSTANT MEMBER FUNCTIONS
size_type size() const;
bool is_item() const;
value_type current() const;
private:
value_type data[CAPACITY];
size_type used;
size_type current_index;
};
}

#endif

Sequence.cpp

#include"sequence.h" // Provides assert
#include<assert.h>
using namespace std;

namespace main_savitch_3
{
   template <typename value_type>
sequence<value_type>::sequence()
{
used = 0;
current_index = 0;
}

   template <typename value_type>
void sequence<value_type>::start()
{
current_index = 0;
}

   template <typename value_type>
void sequence<value_type>::advance()
// Library facilities used: assert.h
{
assert(is_item());
current_index++;
}

   template <typename value_type>
void sequence<value_type>::insert(const value_type& entry)
// Library facilities used: assert.h
{
size_type i;

// Check the precondition
assert(size() < CAPACITY);

// If there is no current item,
// then set current_index to the front so that
// the new entry will be placed at the front of the array.
if (!is_item())
current_index = 0;

// Shift other items over, and insert the new entry at current_index
for (i = used; i > current_index; i--)
data[i] = data[i - 1];
data[current_index] = entry;
used++;
}

   template <typename value_type>
void sequence<value_type>::attach(const value_type& entry)
// Library facilities used: assert.h
{
size_type i;

// Check the precondition
assert(size() < CAPACITY);

if (!is_item()) // There is no current item, so insert at end
data[current_index] = entry;
else // There is a current item, so move items over to make room
{
for (i = used; i > current_index + 1; i--)
data[i] = data[i - 1];
data[current_index + 1] = entry;
current_index++;
}

used++;
}

   template <typename value_type>
void sequence<value_type>::remove_current()
// Library facilties used: assert.h
{
size_type i;

assert(is_item());

for (i = current_index + 1; i < used; i++)
data[i - 1] = data[i];

used--;
}

   template <typename value_type>
size_t sequence<value_type>::size() const
{
return used;
}

   template <typename value_type>
bool sequence<value_type>::is_item() const
{
return (current_index < used);
}

   template <typename value_type>
value_type sequence<value_type>::current() const
// Library facilities used: assert.h
{
assert(is_item());
return data[current_index];
}

   template class sequence<int>;
   template class sequence<float>;
   template class sequence<double>;
}

Output:

Add a comment
Know the answer?
Add Answer to:
c++ Sequence class is a class that supports sequence of integers. Run the project ad verify...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The purpose of this lab is to help reinforce container class concepts and linked list concepts...

    The purpose of this lab is to help reinforce container class concepts and linked list concepts in C++. Specifically, the lab to repeat the sequence lab from last week except to use a linked list. You need to use the author's files sequence3.h and sequence_exam3.cpp. Author - Michael Main, Book - Data Structures and other objects using c++, 4th edition // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is the header file for the...

  • The purpose of this program is to help reinforce container class concepts and linked list concepts...

    The purpose of this program is to help reinforce container class concepts and linked list concepts in C++. Specifically, the task is to implement the sequence class using a linked list. You need to use the author's file sequence3.h to create your implementation file named sequence3.cpp. Please make your code as efficient and reusable as possible. Please make sure code can pass any tests. sequence3.h // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is...

  • PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and...

    PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and my code for bag.cpp. Also I have provided errors from linux for bag.cpp. Please use that code and fix my errors please. Thank you The goal of assignment 3 is to reinforce implementation of container class concepts in C++. Specifically, the assignment is to do problem 3.5 on page 149 of the text. You need to implement the set operations union, intersection, and relative...

  • The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal...

    The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal is to create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Your program must compile. Use test_set.cpp as your test program. Set.h & Test_Set.cpp is code that is already given. All I need is for you add comments to Set.cpp describing each function. FILE: SET.H #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream>...

  • The goal of this task is to reinforce the implementation of container class concepts using linked...

    The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...

  • C++ Create a static implementation of a set. Add the efficiency of each function to the...

    C++ Create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Use test_set.cpp as your test program. Header: /************************************ This class models a mathematical set. */ #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; set(); // postcondition: empty set has been created void insert (const value_type& entry); // precondition: if entry is not in...

  • Was converting the main into a template but I am recieving errors, what should I be...

    Was converting the main into a template but I am recieving errors, what should I be doing? #include<iostream> #include <cstdlib> #include "set.h" using namespace std; int main() {    set<Item> list;    list.insert(4);    list.insert(7);    list.insert(9);    list.insert(3); cout<< "The list contains "<<list.contains(4)<<" number of fours"<<endl;    list.erase(4); cout<< "The list contains "<<list.contains(4)<<" number of fours"<<endl; } #ifndef SET_H #define SET_H #include<cstdlib> namespace std {    template<class Item> class set_item { public: typedef Item value_type; set_item( ) { next...

  • Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides...

    Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides size_t namespace main_savitch_11 { template <class Item> class set { public: // TYPEDEFS typedef Item value_type; // CONSTRUCTORS and DESTRUCTOR set( ); set(const set& source); ~set( ) { clear( ); } // MODIFICATION MEMBER FUNCTIONS void operator =(const set& source); void clear( ); bool insert(const Item& entry); std::size_t erase(const Item& target); // CONSTANT MEMBER FUNCTIONS std::size_t count(const Item& target) const; bool empty( ) const...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • Please Help This: please follow style guidelines Rule 1: use const where appropriate Rule 2: every...

    Please Help This: please follow style guidelines Rule 1: use const where appropriate Rule 2: every member function must include a description in the header file including precondition and postcondition. Rule 3: every member variable must include a description of what the variable holds. Rule 4: Classes should be capitalized. Bag, not bag. Person, not person. (I know this is different than how the book does it) Rule 5: member variables of classes should be preceded by “m_”. If it’s...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT