delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2005/02/09/20:30:27

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
From: "Alan Bashy" <bashyan AT hotmail DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: please, help as soon as possible
Lines: 218
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
Message-ID: <aEyOd.28967$by5.21465@newssvr19.news.prodigy.com>
NNTP-Posting-Host: 149.151.2.5
X-Complaints-To: abuse AT prodigy DOT net
X-Trace: newssvr19.news.prodigy.com 1107998342 ST000 149.151.2.5 (Wed, 09 Feb 2005 20:19:02 EST)
NNTP-Posting-Date: Wed, 09 Feb 2005 20:19:02 EST
Organization: SBC http://yahoo.sbc.com
X-UserInfo1: Q[RGG_CEFBTAS^MRV AT _D]_\@VR]^@B AT MCPWZKB]MPXHZUSAANVUEAE[YETZPIWWI[FCIZA^NBFXZ_D[BFNTCNVPDTNTKHWXKB AT X^B_OCJLPZ AT ET_O[G\XSG AT E\G[ZKVLBL^CJINM AT I_KVIOR\T_M_AW_M[_BWU_HFA_]@A_A^SGFAUDE_DFTMQPFWVW[QPJN
Date: Thu, 10 Feb 2005 01:19:02 GMT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Please, guys, In need help with this. It is due in the next week. I need to
implement the functions in this program especially the first three
constructor. Please, help me about them. I am waiting for you gusy.




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.








- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019