// FILE: sequence1.h // CLASS PROVIDED: sequence (part of the namespace main_sav
ID: 3640366 • Letter: #
Question
- // FILE: sequence1.h
- // CLASS PROVIDED: sequence (part of the namespace main_savitch_3)
- // There is no implementation file provided for this class since it is
- // an exercise from Section 3.2 of "Data Structures and Other Objects Using C++"
- //
- // TYPEDEFS and MEMBER CONSTANTS for the sequence class:
- // typedef ____ value_type
- // sequence::value_type is the data type of the items in the sequence. It
- // may be any of the C++ built-in types (int, char, etc.), or a class with a
- // default constructor, an assignment operator, and a copy constructor.
- //
- // typedef ____ size_type
- // sequence::size_type is the data type of any variable that keeps track of
- // how many items are in a sequence.
- //
- // static const size_type CAPACITY = _____
- // sequence::CAPACITY is the maximum number of items that a sequence can hold.
- //
- // CONSTRUCTOR for the sequence class:
- // sequence( )
- // Postcondition: The sequence has been initialized as an empty sequence.
- //
- // MODIFICATION MEMBER FUNCTIONS for the sequence class:
- // void start( )
- // Postcondition: The first item on the sequence becomes the current item
- // (but if the sequence is empty, then there is no current item).
- //
- // void advance( )
- // Precondition: is_item returns true.
- // Postcondition: If the current item was already the last item in the
- // sequence, then there is no longer any current item. Otherwise, the new
- // current item is the item immediately after the original current item.
- //
- // void insert(const value_type& entry)
- // Precondition: size( ) < CAPACITY.
- // Postcondition: A new copy of entry has been inserted in the sequence
- // before the current item. If there was no current item, then the new entry
- // has been inserted at the front of the sequence. In either case, the newly
- // inserted item is now the current item of the sequence.
- //
- // void attach(const value_type& entry)
- // Precondition: size( ) < CAPACITY.
- // Postcondition: A new copy of entry has been inserted in the sequence after
- // the current item. If there was no current item, then the new entry has
- // been attached to the end of the sequence. In either case, the newly
- // inserted item is now the current item of the sequence.
- //
- // void remove_current( )
- // Precondition: is_item returns true.
- // Postcondition: The current item has been removed from the sequence, and the
- // item after this (if there is one) is now the new current item.
- //
- // CONSTANT MEMBER FUNCTIONS for the sequence class:
- // size_type size( ) const
- // Postcondition: The return value is the number of items in the sequence.
- //
- // bool is_item( ) const
- // Postcondition: A true return value indicates that there is a valid
- // "current" item that may be retrieved by activating the current
- // member function (listed below). A false return value indicates that
- // there is no valid current item.
- //
- // value_type current( ) const
- // Precondition: is_item( ) returns true.
- // Postcondition: The item returned is the current item in the sequence.
- //
- // VALUE SEMANTICS for the sequence class:
- // Assignments and the copy constructor may be used with sequence objects.
- #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
Explanation / Answer
If people would only search the internet. PLEASE RATE AND RATE WELL. THANK YOU
See the following:
http://pastebin.com/7cVvtxbY
******************************************
001 #include
002 #include
003 #include "sequence.h"
004
005 using namespace std;
006
007 // TYPEDEFS and MEMBER CONSTANTS
008 typedef int value_type;
009 typedef std::size_t size_type;
010 static const size_type CAPACITY = 30;
011
012 value_type data[CAPACITY];
013 size_type used;
014 size_type current_index;
015
016 main_savitch_3::Sequence::Sequence()
017 {
018 used = 0;
019 current_index = 50;
020 }
021
022 main_savitch_3::Sequence main_savitch_3::operator + (main_savitch_3::Sequence first, main_savitch_3::Sequence second)
023 {
024 main_savitch_3::Sequence answer;
025 first.start();
026 second.start();
027 answer.start();
028 int carry = 0;
029 while((first.current() == 0 || first.current() == 1) && (second.current() == 0 || second.current() == 1))
030 {
031 if((first.current() + second.current() + carry) == 0)
032 {
033 answer.insert (0);
034 carry = 0;
035 }
036 else
037 {
038 if((first.current() + second.current() + carry) == 1)
039 {
040 answer.insert (1);
041 carry = 0;
042 }
043 else
044 {
045 if((first.current() + second.current() + carry) == 2)
046 {
047 answer.insert(0);
048 carry = 1;
049 }
050 else
051 {
052 if((first.current() + second.current() + carry) == 3)
053 {
054 answer.insert(1);
055 carry = 1;
056 }
057 }
058 }
059 }
060 first.advance();
061 second.advance();
062 }
063
064 while(first.current() == 0 || first.current() == 1)
065 {
066 if((first.current() + carry) == 0)
067 {
068 answer.insert(0);
069 carry = 0;
070 }
071 else
072 {
073 if((first.current() + carry) == 1)
074 {
075 answer.insert(1);
076 carry = 0;
077 }
078 else
079 {
080 if((first.current() + carry) == 2)
081 {
082 answer.insert(0);
083 carry = 1;
084 }
085 }
086 }
087 first.advance();
088 }
089 while(second.current() == 0 || second.current() == 1)
090 {
091 if((second.current() + carry) == 0)
092 {
093 answer.insert(0);
094 carry = 0;
095 }
096 else
097 {
098 if((second.current() + carry) == 1)
099 {
100 answer.insert(1);
101 carry = 0;
102 }
103 else
104 {
105 if((second.current() + carry) == 2)
106 {
107 answer.insert(0);
108 carry = 1;
109 }
110 }
111 }
112 second.advance();
113 }
114 if(carry == 1)
115 {
116 answer.insert(1);
117 }
118 return answer;
119 }
120
121 std::ostream& operator<<(std::ostream& outData, main_savitch_3::Sequence & inData)
122 {
123 // data.start();
124 // for(int i = 0; i < data.size(); i++)
125 // {
126 // outData << data.current();
127 // data.advance();
128 // }
129 int dataCopy[CAPACITY];
130 for(int i = 0; i < inData.size(); i++)
131 {
132 dataCopy[i] = data[i];
133 outData << data[i];
134 }
135 return outData;
136 }
137
138 // Postcondition: The first item on the sequence becomes the current item
139 // (but if the sequence is empty, then there is no current item).
140 void main_savitch_3::Sequence::start()
141 {
142 if(used > 0)
143 {
144 current_index = 0;
145 }
146 else
147 {
148 current_index = 50;
149 }
150 }
151
152 // Precondition: is_item returns true.
153 // Postcondition: If the current item was already the last item in the
154 // sequence, then there is no longer any current item. Otherwise, the new
155 // current item is the item immediately after the original current item.
156 void main_savitch_3::Sequence::advance()
157 {
158 if(current_index == (used - 1) || current_index == 50)
159 {
160 current_index = 50;
161 }
162 else
163 {
164 current_index += 1;
165 }
166 }
167
168 // Precondition: size( ) < CAPACITY.
169 // Postcondition: A new copy of entry has been inserted in the sequence
170 // before the current item. If there was no current item, then the new entry
171 // has been inserted at the front of the sequence. In either case, the newly
172 // inserted item is now the current item of the sequence.
173 void main_savitch_3::Sequence::insert(const value_type& entry)
174 {
175 if(current_index == 50)
176 {
177 int usedCopy = used;
178 while((usedCopy - 1) > 0)
179 {
180 data[usedCopy - 1] = data[usedCopy];
181 usedCopy--;
182 }
183 data[0] = entry;
184 current_index = 0;
185 }
186 else
187 {
188 int usedCopy = used;
189 while((usedCopy - 1) > current_index)
190 {
191 data[usedCopy] = data[usedCopy - 1];
192 usedCopy--;
193 }
194 // current_index++;
195 if(used != 0)
196 {
197 data[current_index + 1] = data[current_index];
198 }
199 data[current_index] = entry;
200 }
201 used++;
202 }
203
204 // Precondition: size( ) < CAPACITY.
205 // Postcondition: A new copy of entry has been inserted in the sequence after
206 // the current item. If there was no current item, then the new entry has
207 // been attached to the end of the sequence. In either case, the newly
208 // inserted item is now the current item of the sequence.
209 void main_savitch_3::Sequence::attach(const value_type& entry)
210 {
211 if(current_index == 50)
212 {
213 data[used] = entry;
214 current_index = used - 1;
215 }
216 else
217 {
218 int copyUsed = used;
219 while(current_index > copyUsed)
220 {
221 data[copyUsed] = data [copyUsed - 1];
222 copyUsed--;
223 }
224 data[current_index + 1] = entry;
225 current_index++;
226 }
227 used++;
228 }
229
230 // void remove_current( )
231 // Precondition: is_item returns true.
232 // Postcondition: The current item has been removed from the sequence, and the
233 // item after this (if there is one) is now the new current item.
234 void main_savitch_3::Sequence::remove_current( )
235 {
236 data[current_index] = NULL;
237 int copyUsed = used;
238 int indexCopy = current_index;
239 while((copyUsed - 1) > indexCopy)
240 {
241 data[indexCopy] = data[indexCopy + 1];
242 indexCopy++;
243 }
244 used--;
245 }
246
247 //Postcondition: The return value is the number of items in the sequence.
248 size_type main_savitch_3::Sequence::size( )
249 {
250 return used;
251 }
252
253 //Postcondition: A true return value indicates that there is a valid
254 // "current" item that may be retrieved by activating the current
255 // member function (listed below). A false return value indicates that
256 // there is no valid current item.
257 bool main_savitch_3::Sequence::is_item( )
258 {
259 return current();
260 }
261
262 // Precondition: is_item( ) returns true.
263 // Postcondition: The item returned is the current item in the sequence.
264 value_type main_savitch_3::Sequence::current( )
265 {
266 return data[current_index];
267 }
*****************************************************8
#include
02 #include
03 #include "sequence.h"
04
05 using namespace main_savitch_3;
06
07 int main()
08 {
09 //Initialize input/output
10 std::ofstream outData;
11 std::ifstream inData;
12 inData.open("input.txt");
13 outData.open("output.txt");
14
15 //Check that files are open and valid
16 if(!inData.is_open() || !outData.is_open())
17 {
18 return 1;
19 }
20
21 //Data entry and processing until there is not more data to process
22 while(!inData.eof())
23 {
24 //Initialize sequence files
25 Sequence first;
26 Sequence second;
27
28 int input = inData.get();
29 while(input == 48 || input == 49)
30 {
31 first.insert((input - 48));
32 input = inData.get();
33 }
34 input = inData.get();
35 while(input == 48 || input == 49)
36 {
37 second.insert((input - 48));
38 input = inData.get();
39 }
40 first.start();
41 for(int i = 0; i < first.size(); i++)
42 {
43 outData << first.current();
44 first.advance();
45 }
46 outData << " ";
47 second.start();
48 for(int i = 0; i < second.size(); i++)
49 {
50 outData << second.current();
51 second.advance();
52 }
53 outData << std::endl;
54
55 outData << std::endl;
56 Sequence answer = first + second;
57 outData << answer;
58 outData << std::endl;
59 outData << std::endl;
60 }
61
62 return 0;
63 }
64
65 std::ostream& operator<<(std::ostream &outData, Sequence &data)
66 {
67 data.start();
68 for(int i = 0; i < data.size(); i++)
69 {
70 outData << data.current();
71 data.advance();
72 }
73 return outData;
74 }
***************************************************
http://www.dreamincode.net/forums/topic/216091-overloading-an-operator-friend-function/
********************************************
FILE: sequence.h
CLASS PROVIDED: sequence (part of the namespace main_savitch_3)
TYPEDEFS and MEMBER CONSTANTS for the sequence class
typedef____ value_type
sequence :: value_type is the data the type of the the items in the the sequence.
It may be any of the C + + built-in The types (int, char, etc.), or a class
with a the default the constructor, an assignment operator and a the copy
constructor.
typedef____ size_type
sequence :: size_type is the data type of any variable that keeps track of
the how a many items are in a the sequence.
static const size_type CAPACITY = _____
sequence :: CAPACITY is the maximum number of items that a sequence can
hold.
CONSTRUCTOR for the the sequence class:
sequence ()
Postcondition: The sequence has been initialized as an empty sequence.
MODIFICATION MEMBER FUNCTIONS for the the sequence the class:
void start ()
Postcondition: The first item on the sequence becomes the current item
(But if the the sequence is empty, then there is no current item).
void advance ()
Precondition: is_item returns true.
Postcondition: If the current item was already the last item in the sequence, then there is no longer any current item. Otherwise, the new current item
is the item immediately after the original current item.
void insert (const the value_type of & entry)
Precondition: size () Postcondition: A new copy of entry has been inserted in the sequence before
the current item. the If there was no current item, then the new entry has
been inserted at the front of the the sequence. In either case, the newly
The inserted item is now the current item of the the sequence.
void attach (const the value_type of entry)
Precondition: size () Postcondition: A new copy of entry has been inserted in the sequence
after the current item. the If there was no current item, then the new entry
has been attached to the end of the the sequence. In either case, the newly
The inserted item is now the current item of the the sequence.
void remove_current ()
Precondition: is_item returns true.
Postcondtion: The current item has been removed from the sequence, and
the item after this (if there is one) is now the new current item.
CONSTANT MEMBER FUNCTIONS for the the sequence the class:
size_type size () const
Postcondition: The return value is the number of items in the sequence.
bool is_item () const
Postcondition: A true return value indicates that there is a valid
"Current" item that may be retrieved by activating the current member
function (listed below). to false the return value indicates that there is no
valid current item.
the value_type of current () const
Precondition: is_item () returns true.
Postcondition: The item returned is the current item in the sequence.
The VALUE SEMANTICS for the the sequence the class:
Assignments and the copy constructor may be used withe sequence objects.
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
# Ifndef MAIN_SAVITCH_SEQUENCE_H
# Define MAIN_SAVITCH_SEQUENCE_H
The # include / / Provides size_t is
namespace main_savitch_3
{
the class the sequence
{
public:
/ / TYPEDEFS and MEMBER CONSTANTS
typedef for the value_type of a double;
typedef for the std :: size_t is size_type;
static const size_type CAPACITY = 30;
/ / CONSTRUCTOR
sequence ()
{
used = 0;
current_index = 0;
}
/ / MODIFICATION MEMBER FUNCTIONS
void start ();
void advance ();
void insert (const the value_type of & entry);
void attach (const the value_type of entry);
void remove_current ();
/ / CONSTANT MEMBER FUNCTIONS
size_type size () const;
bool is_item () const;
the value_type of current () const;
private:
the value_type of the data [CAPACITY];
size_type used;
size_type current_index;
;}
}
# Endif
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
# The include
# Include
using namespace std;
# Include "sequence.h
namespace main_savitch_3
{
/ / MODIFICATION MEMBER FUNCTIONS
void sequence :: start ()
{
if (size () == 0)
cout << "the sequence item is empty, then there is no current item;
else
current_index = 0;
}
void sequence :: The advance ()
{
if (current_index == size ())
cout << "There is is no current item / n";
else
current_index + +;
}
void sequence :: insert (const the value_type of & entry)
{
the value_type of t -;
size_type i;
if (size () == 0)
{
data [0] = entry;
}
else if (current_index == size ())
{
for (i = size (); i> 0; i -)
{
t = data [i];
data [i] = data [i-1];
data [i-1] = t;
}
data [0] = entry;
}
else
{
for (i = size (); i> current_index; i -)
{
t = data [i];
data [i] = data [i-1];
data [i-1] = t;
}
data [current_index] = entry;
}
used + +;
}
void sequence :: attach (const the value_type of entry)
{
the value_type of t -;
size_type i;
if (size () == 0)
data [0] = entry;
else if (current_index == size () -1)
data [size ()] = entry;
else
{
for (i = size (); i> current_index +1; i -)
{
t = data [i];
data [i] = data [i-1];
data [i-1] = t;
}
data [current_index +1] = entry;
}
used + +;
}
void sequence :: remove_current ()
{
the value_type of t -;
size_type i;
if (current_index == used-1)
current_index -;
else
for (i = current_index +1; i data [i-1] = data [i];
used -;
}
the sequence of the sequence :: size_type :: size () const
{
return used;
}
/ / CONSTANT MEMBER FUNCTIONS for the the sequence class:
bool sequence :: is_item () const
{
if (current_index return true;
else
return false;
}
sequence :: value_type sequence :: current () const
{
if (is_item () == true)
return data [current_index];
else
cout << "There is no current data n";
}
}
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / FILE: sequence_test.cpp
/ / An interactive test program the sequence the class for the new
# Include / / Provides toupper
# Include / / Provides cout and cin
The # include / / 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 programs:
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 perline).
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 the sequence that we'll perform tests on
char choice; / / A command character entered by the the user
cout << "I have initialized an empty sequence of real numbers." << endl;
do
{
print_menu ();
choice = toupper (get_user_command ());
the 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 is no current item." << endl;
break;
case 'C': if (test.is_item ())
cout << "Current item is:" << test.current () << 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 << "! the Activate the start () function" << endl;
cout << "+ the Activate the 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 << "Insert a new number with the the insert (...) function" << endl;
cout << "A the Attach a new number with the attach (...) function" << endl;
cout << "R Activate the remove_current () function" << endl;
cout << "Q-the Quit this test program" << endl;
}
char get_user_command ()
/ / Library facilities used: the iostream
{
a char command;
cout << "Enter choice:";
cin >> command ;/ / the Input of characters skips blanks and newline character
the return command;
}
void show_sequence (sequence display)
/ / Library facilities used: the iostream
{
for (display.start (); display.is_item (); display.advance ())
cout << display.current () << endl;
}
the double get_number ()
/ / Library facilities used: the iostream
{
a double result;
cout << "Please enter a real number for the the sequence:";
cin >> result;
cout << result << "has been the read." << endl;
return result;
}
*********************************************************
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.