Question
Program Note: For this assignment the normal C++ string and cstring functions can not be used (except >> and You must write all the functions
Program Note: For this assignment the normal C++ string and cstring functions can not be used (except >> and You must write all the functions that will be used.
*Programming Note: Do not use the push_back() member function of vector, because this won't work for this program (it calls the copy constructor of our MYString class, which we haven't written).
Program Description: The MYString objects will hold a cstring and allow it to be used and changed. We will be changing this class over the next couple programs, to be adding more features to it (and correcting some problems that the program has in this simple version).
Below will be the details of the class. Your first job will be to create and test the MYString class.
Inside the class we will have the following data members:
Member Data | Description |
---|---|
char * str | pointer to dynamic memory for storing the string |
int cap | size of the memory that is available to be used (start with 20 char's and then increase by increments of 20 whenever this is not enough) |
int end | index of the end of the string (the '\0' char) |
The class will store the string in dynamic memory that is pointed to with the pointer. When you first create an MYString object you should allocate 20 spaces of memory (using the new command). The string will be stored as a cstring in this memory. {NOTE: Here on Canvas in Files\Review Lectures from 131\ there is a pdf of the pointer and cstring lecture slides from CS 131 to use as a review}.
For this program, your MYString variables will never need to grow beyond length 20, in program 3 you will need to be allow your string that is held in the class to be able to be larger than 20 chars. So you may want to start allowing your strings to be able to grow.....if you have extra time. If you are optionally building in the ability to grow, it should start with a capacity of 20, but when needed it would grow in increments of 20. The capacity should always be a multiple of 20.
For example if we were storing the string "cat" in a MYString object, our data member would have the following values:
str | starting addr of dynamic array |
---|---|
cap | 20 |
end | 3 |
c | a | t | \0 | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? |
Programming Note: Write and test one or two functions at a time | |
---|---|
Member Functions : return type | Description |
MYString( ) | Default Constructor: creates an empty string |
MYString (const char* cstr) | creates a string that contains the information from the argument example: MYString greeting( "hello there wise one"); |
length( ) : int | the length of the string ( "cat" would return 3) |
capacity( ) : int | the total amount of memory available for use (always 20 in this version, but in the next version, this will change) |
at( int index) : char | returns the character at a certain location ( at( 0 ) for a "cat" would return 'c' ). If the index is not inside the string (negative or too large) then return '\0' |
read( istream& istrm) : bool | read one string from the istream argument (could be from cin or an ifstream variable). This should work just like the >> operator. When reading in, you can assume that you will not read in a string longer than 99 characters. This function will return true if it was able to read (remember >> operator will return true if it is able to read from a file). For simplicity sake, you could create a local char array variable 100 that you first read into and then you could copy from this char array into your dynamic memory. |
write( ostream& ostrm) : void | write the string out to the ostream argument, but do not add any end of line (could be cout or an ofstream variable) |
compareTo( const MYString& argStr) : int | return an int based on the following: this is assuming the function is called in the following way: objStr.compareTo( argStr ); negative number if objStr argStr zero if objStr == argStr |
c_str( ) : const char * | return a pointer to a constant cstring version of the MYString object. This can be nice for simple outputs: cout |
setEqualTo(const MYString& argStr): void | this does the assignment operation objStr.setEqualTo( argStr ) would change objStr so that it would contain the same information as argStr |
Main Program Requirements:
create a vector of MYStrings that is size 100
read each of the words from the file called "infile2.txt" (the file is out in Files section under Program Resources). You can call the read function directly on the indexed vector
as you are reading the words in, keep a count of how many words were read in.
After you have read in all the words from the file, resize your vector to the correct size based on your count of the number of words read in.
sort the MYStrings from smallest to largest (this will be based on the ASCII encoding....meaning capital letters will sort before lower case letters) using Bubble Sort
output the sorted words to outfile.txt file
6 words per line ( use setw(13), which is part of
Bubble Sort Algorithm:
create bool name notDone = true; loop until notDone == true notDone = false loop through the elements of the array starting with first index compare two elements to see if they are in proper order, if not in order than swamp them, mark notDone = true increment your index so you can check the next two
+++++++++++++++++++++++++++++++++++++++++++ infile2.txt +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
They seemed amazingly busy. I began to ask myself what they could be. Were they intelligent mechanisms? Such a thing I felt was impossible. Or did a Martian sit within each, ruling, directing, using, much as a man's brain sits and rules in his body? I began to compare the things to human machines, to ask myself for the first time in my life how an ironclad or a steam engine would seem to an intelligent lower animal.
+++++++++++++++++++++++++++++++++++++++++++ main.cpp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ MYString.h ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #ifndef MYSTRING_H #define MYSTRING_H #include +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ MYString.cpp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #include "MYString.h" MYString::MYString() { cap = 20; end = 0; str = new char[cap]; str[end] = '\0'; } MYString::MYString(const char* cstr) { for (end = 0; cstr[end] != '\0'; ++end); //empty loop cap = 20; //TODO: needs to potentially grow for prog3 str = new char[cap]; for (int i = 0; i = 0 && index > inputWord) { for (end = 0; inputWord[end] != '\0'; ++end); //empty loop // cap = ??; //TODO: needs to potentially grow for prog3 for (int i = 0; i } bool MYString::lessThan(const MYString& argStr) { // TODO: you need to write. return false; } bool MYString::greaterThan(const MYString& argStr) { //TODO: you need to write. return false; } bool MYString::equals(const MYString& argStr) { //TODO: you need to write. return false; } void MYString::setEqualTo(const MYString& argStr) { end = argStr.end; // cap = ??; //TODO: needs to potentially grow for prog3 for (int i = 0; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started