By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,679 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,679 IT Pros & Developers. It's quick & easy.

Please,help as soon as you can.

P: n/a
Please, guys, In need help with this. It is due in the next week. Please,
help me to implement the functions in this programm especially the first
three constructor. I need them guys. Please, help me.

This was inspired by Exercise 7 and Programming Problem 8 in Chapter 3 of
our text.

I have done Exercise 7 for you: Below you will find the ADT specification
for a string
of characters. It represents slightly more that a minimal string class.

//--------------------------------------------------------------------------
-
#ifndef MyStringH
#define MyStringH
//--------------------------------------------------------------------------
-
typedef char CharType; // Allow for other character types

class String
{
public:
// Constructors and Destructor
String( int capacity = 50 ); // Make empty String with given capacity
String( const String & str ); // Copy str
String( const CharType * str, int capacity = 50 ); // Make str a String
~String();

// Queries
unsigned Length() const { return count_; }
bool IsEmpty() const { return (count_ == 0); }
bool IsFull() const { return count_ == capacity_; }
int IndexOf( const String & str );
String SubString( unsigned start, unsigned count ) const; // Copy out
const CharType * c_str() const;

// Modifiers
CharType & operator[]( int index ) { return string_[index]; } // Unsafe

bool Delete( unsigned start, unsigned count );

bool Insert( unsigned pos, const String & str );

private:
// You supply the private data members
};
//--------------------------------------------------------------------------
-
#endif

Note that this is nearly the header file (*.h) you would use to declare the
class. The private data for the class is missing, but there are a few hints
in the above code to show you what data is required.

The Constructors and Destructor

String( int capacity = 50 );
This constructor creates an empty String with the given capacity. If an
argument is missing for the parameter capacity, it is given the default
value 50 by the compiler.

String( const String & str );
This constructor is the copy constructor. It creates an exact duplicate of
its argument str. This includes duplicating the array of characters owned
by the object str.

String( const CharType * str, int capacity = 50 );
This constructor receives a pointer to a null terminated array of characters
of type CharType, which is set to char in the header file, but could also be
other types. It also receives an optional argument specifying a capacity.
The constructor creates a String object of the given capacity and sets its
internal array of characters equal to the array str by copying the
characters.

The Query Functions

The functions Length(), IsEmpty() and IsFull() return the number of
characters in the String object, whether the String is empty, and whether
the string is full (length = capacity) respectively. Note these functions
are implemented in the class declarations and give you some hints about
other class details.

int IndexOf( const String & str );
This functions returns the zero-based index of its String argument str
within the String object *this that is used to call the function. If str
can not be found as a substring within *this, the value -1 is returned. For
example, String("ab").IndexOf( String("b") ) returns 1.

String SubString( unsigned start, unsigned count ) const;
This function returns a String object, the value of which is the substring
that begins at index position start and contains count characters. The
value given for start must lie within the String object, and if it doesn't
the string String("") is returned. The value of count must not extend
beyond the end of the String object, and if it does then count is truncated
to correspond to the end of the string. For example,
String("ab").SubString( 0, 1 ) returns String("a"),
String("ab").SubString( 0, 3 ) returns String("ab"),
String("ab").SubString( 3, 1 ) returns String("").

const CharType * c_str() const;
This function returns a pointer to a dynamically allocated C++ array of
characters of type CharType. The array is a null terminated string
containing a copy of the characters in its String object. For example,
String("ab").c_str() returns a pointer to "ab". It is the responsibility of
the calling code to delete [ ] the string so returned.

The Modifier Functions

CharType & operator[]( int index ) { return string_[index]; }
The overloaded operator[] returns a reference to the character at position
index which allows individual characters to be read and changed. For
example,
String("abc")[2] = 'a' becomes String("aba"),
CharType ch = String("abc")[2] produces ch == 'a'.
Note that this function is already implemented and provides some hints about
class details.

bool Delete( unsigned start, unsigned count );
Function Delete deletes count characters from the string object that called
the function, starting at index position start. Arguments start and count
obey the same rules as described for function SubString. Use function
SubString to preview the substring that would be deleted. Use function
IndexOf to find a substring to delete.

bool Insert( unsigned pos, const String & str );
Function Insert inserts its second argument at the position index in the
string that called the function. This function may be used to append or
prepend strings to a given string. For example,
String("abd").Insert( 0, "xyz" ) produces String("xyzabd"),
String("abd").Insert( 3, "xyz" ) produces String("abdxyz"),
String("abd").Insert( 2, "c" ) produces String("abcd").

What You Do

Your job is to implement and test this ADT using an array allocated with the
new operator in the constructors and de-allocated with the delete []
operator in the destructor. You will find that three data members will be
sufficient for the job. At a minimum, you must test each function with
enough different arguments to show it works.

You are allowed to define additional helper functions, member functions or
otherwise. For example, the following function allows you to use the <<
operator to display a String.

ostream & operator<<( ostream & os, const String & str )
{
os << str.c_str();
return os;
}

With this function the following works:

String s("My string");
cout << s << '\n';

Also, the following function compares two C-style "strings" up to n
"characters" and returns a + value if s1 > s2, a - value if s1 < s2, and
zero if s1 == s2.

int MemCmp( const CharType * s1, const CharType * s2, unsigned n )
{
// Assume bin op - is defined for type CharType
for ( unsigned c = 0; c < n; ++c )
{
if ( (s1[c] - s2[c]) != 0 )
return (s1[c] - s2[c]); // s1 > or < s2
}
return 0; // s1 == s2
}

When you need a null character of type CharType use this: CharType('\0')

How You Do This
Important: do not attempt to implement this all at once. Start with the
constructors and destructor. Implement them one at a time and use the
functions Length(), IsEmpty() and IsFull() to output the length of an
object, whether the object is empty, and whether the object is full. When
you get correct results for each constructor, you may proceed to the next
phase.

Implement and test function c_str(). This will allow you to output a string
using operator <<.

Use the implemented operator [] to display and then change and display one
or two strings created in the first phase. When all appears correct you may
proceed to the next phase.

Implement and test IndexOf().

Implement and test SubString().

Implement and test Delete().

Implement and test Insert().
Your Grade

Your grade will be based on 1) correctness of your code, 2) good style
(indentation, variable names, etc.), 3) adequate testing, and 4) amount
completed.

It is always better to turn in something rather than nothing. Late papers
are not accepted. Incomplete but timely papers may be improved and
re-submitted within a week.

Failure to attempt 2 or more labs results in failure of the course.


Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

Alan Bashy wrote:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help me.

This was inspired by Exercise 7 and Programming Problem 8 in Chapter 3 of our text.

I have done Exercise 7 for you: Below you will find the ADT specification for a string
of characters. It represents slightly more that a minimal string class.
//-------------------------------------------------------------------------- -
#ifndef MyStringH
#define MyStringH
//-------------------------------------------------------------------------- -
typedef char CharType; // Allow for other character types

class String
{
public:
// Constructors and Destructor
String( int capacity = 50 ); // Make empty String with given capacity String( const String & str ); // Copy str
String( const CharType * str, int capacity = 50 ); // Make str a String ~String();

// Queries
unsigned Length() const { return count_; }
bool IsEmpty() const { return (count_ == 0); }
bool IsFull() const { return count_ == capacity_; }
int IndexOf( const String & str );
String SubString( unsigned start, unsigned count ) const; // Copy out const CharType * c_str() const;

// Modifiers
CharType & operator[]( int index ) { return string_[index]; } // Unsafe
bool Delete( unsigned start, unsigned count );

bool Insert( unsigned pos, const String & str );

private:
// You supply the private data members
};
//-------------------------------------------------------------------------- -
#endif

Note that this is nearly the header file (*.h) you would use to declare the class. The private data for the class is missing, but there are a few hints in the above code to show you what data is required.

The Constructors and Destructor

String( int capacity = 50 );
This constructor creates an empty String with the given capacity. If an argument is missing for the parameter capacity, it is given the default value 50 by the compiler.

String( const String & str );
This constructor is the copy constructor. It creates an exact duplicate of its argument str. This includes duplicating the array of characters owned by the object str.

String( const CharType * str, int capacity = 50 );
This constructor receives a pointer to a null terminated array of characters of type CharType, which is set to char in the header file, but could also be other types. It also receives an optional argument specifying a capacity. The constructor creates a String object of the given capacity and sets its internal array of characters equal to the array str by copying the
characters.

The Query Functions

The functions Length(), IsEmpty() and IsFull() return the number of
characters in the String object, whether the String is empty, and whether the string is full (length = capacity) respectively. Note these functions are implemented in the class declarations and give you some hints about other class details.

int IndexOf( const String & str );
This functions returns the zero-based index of its String argument str within the String object *this that is used to call the function. If str can not be found as a substring within *this, the value -1 is returned. For example, String("ab").IndexOf( String("b") ) returns 1.

String SubString( unsigned start, unsigned count ) const;
This function returns a String object, the value of which is the substring that begins at index position start and contains count characters. The value given for start must lie within the String object, and if it doesn't the string String("") is returned. The value of count must not extend beyond the end of the String object, and if it does then count is truncated to correspond to the end of the string. For example,
String("ab").SubString( 0, 1 ) returns String("a"),
String("ab").SubString( 0, 3 ) returns String("ab"),
String("ab").SubString( 3, 1 ) returns String("").

const CharType * c_str() const;
This function returns a pointer to a dynamically allocated C++ array of characters of type CharType. The array is a null terminated string
containing a copy of the characters in its String object. For example, String("ab").c_str() returns a pointer to "ab". It is the responsibility of the calling code to delete [ ] the string so returned.

The Modifier Functions

CharType & operator[]( int index ) { return string_[index]; }
The overloaded operator[] returns a reference to the character at position index which allows individual characters to be read and changed. For example,
String("abc")[2] = 'a' becomes String("aba"),
CharType ch = String("abc")[2] produces ch == 'a'.
Note that this function is already implemented and provides some hints about class details.

bool Delete( unsigned start, unsigned count );
Function Delete deletes count characters from the string object that called the function, starting at index position start. Arguments start and count obey the same rules as described for function SubString. Use function SubString to preview the substring that would be deleted. Use function IndexOf to find a substring to delete.

bool Insert( unsigned pos, const String & str );
Function Insert inserts its second argument at the position index in the string that called the function. This function may be used to append or prepend strings to a given string. For example,
String("abd").Insert( 0, "xyz" ) produces String("xyzabd"),
String("abd").Insert( 3, "xyz" ) produces String("abdxyz"),
String("abd").Insert( 2, "c" ) produces String("abcd").

What You Do

Your job is to implement and test this ADT using an array allocated with the new operator in the constructors and de-allocated with the delete []
operator in the destructor. You will find that three data members will be sufficient for the job. At a minimum, you must test each function with enough different arguments to show it works.

You are allowed to define additional helper functions, member functions or otherwise. For example, the following function allows you to use the << operator to display a String.

ostream & operator<<( ostream & os, const String & str )
{
os << str.c_str();
return os;
}

With this function the following works:

String s("My string");
cout << s << '\n';

Also, the following function compares two C-style "strings" up to n
"characters" and returns a + value if s1 > s2, a - value if s1 < s2, and zero if s1 == s2.

int MemCmp( const CharType * s1, const CharType * s2, unsigned n )
{
// Assume bin op - is defined for type CharType
for ( unsigned c = 0; c < n; ++c )
{
if ( (s1[c] - s2[c]) != 0 )
return (s1[c] - s2[c]); // s1 > or < s2
}
return 0; // s1 == s2
}

When you need a null character of type CharType use this: CharType('\0')
How You Do This
Important: do not attempt to implement this all at once. Start with the constructors and destructor. Implement them one at a time and use the functions Length(), IsEmpty() and IsFull() to output the length of an
object, whether the object is empty, and whether the object is full. When you get correct results for each constructor, you may proceed to the next phase.

Implement and test function c_str(). This will allow you to output a string using operator <<.

Use the implemented operator [] to display and then change and display one or two strings created in the first phase. When all appears correct you may proceed to the next phase.

Implement and test IndexOf().

Implement and test SubString().

Implement and test Delete().

Implement and test Insert().
Your Grade

Your grade will be based on 1) correctness of your code, 2) good style (indentation, variable names, etc.), 3) adequate testing, and 4) amount completed.

It is always better to turn in something rather than nothing. Late papers are not accepted. Incomplete but timely papers may be improved and
re-submitted within a week.

Failure to attempt 2 or more labs results in failure of the course.


we ARE NOT a homework group!!!!
I have seen certain website where you can submit your assignments, and
they don't charge a great deal. Good Luck!

Jul 23 '05 #2

P: n/a
Alan Bashy wrote:

[homework stuff snipped]

To quote the FAQ:

"If I did your homework for you, then you might pass your class without
learning how to write a program like this. Then you might graduate and
get your degree without learning how to write a program like this. You
might become a professional programmer without knowing how to write a
program like this. Someday you might work on a project with me without
knowing how to write a program like this. Then I would have to do you
serious bodily harm." [Thanks to Jack Klein]

--
Regards,

Karsten
Jul 23 '05 #3

P: n/a
This might help:

---- CODE ----

#include <iostream>

using namespace std;

int main() {
cout << "Do your own homework.\n";
return 0;
}

---- /CODE ----
Jul 23 '05 #4

P: n/a
Alan Bashy wrote:
Your Grade

Your grade will be based on 1) correctness of your code, 2) good style
(indentation, variable names, etc.), 3) adequate testing, and 4) amount
completed.


That was a nice assignment that the OP must do by himself, however I
think (3) is too ambiguous (but very common style in assignments).
What kind of testing should one provide in such homework?


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5

P: n/a
"Alan Bashy" writes:
Please, guys, In need help with this. It is due in the next week. Please,
help me to implement the functions in this programm especially the first
three constructor. I need them guys. Please, help me.

This was inspired by Exercise 7 and Programming Problem 8 in Chapter 3 of
our text.

I have done Exercise 7 for you: Below you will find the ADT specification
for a string
of characters. It represents slightly more that a minimal string class.

//--------------------------------------------------------------------------
-
#ifndef MyStringH
#define MyStringH
//--------------------------------------------------------------------------
-
typedef char CharType; // Allow for other character types

class String
{
public:
// Constructors and Destructor
String( int capacity = 50 ); // Make empty String with given capacity
[elided]
Your job is to implement and test this ADT using an array allocated with
the
new operator in the constructors and de-allocated with the delete []
operator in the destructor. You will find that three data members will be
sufficient for the job.


It appears he wants you to make a "Pascal style" string, which is able to
store a value of binary zero for a character. So, something like this:

private:
char* ca; // character array. allocated via new.
// Which means there must also be a destructor to release
the space.
int sz_max; // 50 for default
int sz_curr; // 0 for default

Now write the first contructor.

The assignment is very chatty, he's trying to be nice. :-( Try to
extract the essence, one chunk at a time, as I have done here.
Jul 23 '05 #6

P: n/a
Alan Bashy wrote:

Please, guys, In need help with this. It is due in the next week. Please,
help me to implement the functions in this programm especially the first
three constructor. I need them guys. Please, help me.
Hmm. The only question you are asking is: "Can someone do this for me"?
I doubt that somebody will do this in this group.

But: If you try to implement one of the constructors (don't write all
of them in one rush. Write one and test it) and have problems with it,
you can always come back and ask specific questions. And then you will
see that the group is more then willing to help.
In fact the rule is: If you show signs of an attempt of yourself to solve
your problem, you will get plenty of help. If you show no such sign, nobody
is going to help.

Your instructor even gave you a hint on how to proceed, which is IMHO
very good advice. Why don't you follow it?

How You Do This
Important: do not attempt to implement this all at once. Start with the
constructors and destructor. Implement them one at a time and use the
functions Length(), IsEmpty() and IsFull() to output the length of an
object, whether the object is empty, and whether the object is full. When
you get correct results for each constructor, you may proceed to the next
phase.

Implement and test function c_str(). This will allow you to output a string
using operator <<.

Use the implemented operator [] to display and then change and display one
or two strings created in the first phase. When all appears correct you may
proceed to the next phase.

Implement and test IndexOf().

Implement and test SubString().

Implement and test Delete().

Implement and test Insert().


--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #7

P: n/a
On Thu, 10 Feb 2005 01:15:12 GMT, "Alan Bashy" <ba*****@hotmail.com>
wrote:
Please, guys, In need help with this. It is due in the next week.
In most jobs you are required to be able to manage you own time
effectively. Now you know why. It is certainly a skill worth having.
Please, help me to implement the functions in this programm especially the first
three constructor.
Certainly we will help, but *only* if you show us what you have done
so far. Show us what you got for the data members of the class and
what you have done for the first constructor. Explain which parts you
are having a problem with.
[Snip]

How You Do This
Important: do not attempt to implement this all at once. Start with the
constructors and destructor. Implement them one at a time and use the
functions Length(), IsEmpty() and IsFull() to output the length of an
object, whether the object is empty, and whether the object is full. When
you get correct results for each constructor, you may proceed to the next
phase.
This is good advice. I suggest that you follow it. Do the work in
small bite-sized pieces. Even experts work this way, it is just that
they can take bigger bites than beginners. Work on one function at a
time. Implement one of the constructors, if you get problems then
show us your code and we will help. When you think it is working then
test it with different inputs. Repeat for the other constructors and
the destructor. Then implement Length(), IsEmpty() and IsFull() one
at a time. Test each one as you write it to make sure that it works.
You can hand in your testing code with the assignment as it is part of
the work you are told to do.
[Snip]

Your Grade

Your grade will be based on 1) correctness of your code, 2) good style
(indentation, variable names, etc.), 3) adequate testing, and 4) amount
completed.

It is always better to turn in something rather than nothing. Late papers
are not accepted. Incomplete but timely papers may be improved and
re-submitted within a week.


Take note of these two paragraphs, especially the second.
rossum

--

The ultimate truth is that there is no Ultimate Truth
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.