EACH COMMAND REQUIRES IT\'S OWN FUNCTION Program creating book-store database. E
ID: 3889015 • Letter: E
Question
EACH COMMAND REQUIRES IT'S OWN FUNCTION
Program creating book-store database. Each command requires separate function. struct Book string ISBN; string title; int quantity; int edition; double price; Program needs to read and process a transaction file "storeTrans.txt". This file contains several commands such as: display, add, searchBook, delete, updatePrice, updateQuantity, sort and save. Each command requires new line Example of display: ISBN Title Edition Quantity Price 123456789 C++ForDummies 3 5 150.00 Add bookISBN bookTitle bookQuantity bookPrice Adds new book record to the database . Should read information from the transaction file Should check for duplicate book entries (same ISBN) If book doesn't exist: add new entry Should display message that the book was successfully added If book already exists, do not add. Display message ° use push back function to add to the end of vector “123456789 successfully added to the database “123456789 already exists. The number of copies is now 5…” " · . . SearchBook bookTitle Searches and returns all books with matching title. Exact title OR keyword found Display message . . . "Searching for book... 123456789 C++ForDummies 5” If no entry found ."Searching for book. Sorry, no matching book existsExplanation / Answer
//main.cpp
#include "MyString.h"
#include <iostream>
using namespace std;
int main()
{
MyString s1; //creating instances of MyString using the different constructors
MyString s2 = "Pepperoni";
MyString s3 = "Pizza";
MyString s4(s2);
//Verifying constructors work
cout << "The value of MyString s1 with default constructor is: "; s1.Put();
cout << endl << "The value of MyString s2, copy contructor of a cstring: "; s2.Put();
cout << endl << "The value of MyString s4, initialized with the contents of s2: "; s4.Put();
cout << endl << "Now lets test the operator overloading. Testing s2 + s3........" << endl << "s2 now contains: ";
s2 + s3; s2.Put();
cout << endl << "And now testing s1 = s2....." << endl << "s1 now contains: ";
s1 = s2; s1.Put();
cout << endl << "And Finally we will Test the reverse function. The reverse of s1 is: ";
s1.Reverse(); s1.Put();
cout << endl << "That is all. The program will now automatically call the destructor functions. Freeing memory. " << endl;
return 0;
}
===========================================================================
//MyString.cpp
#ifndef MYSTRING_CPP
#define MYSTRING_CPP
#include <string.h>
#include <iostream>
#include "MyString.h"
using namespace std;
MyString::MyString()
{
length = 0; //initializing array to size of 0. Will later be overwritten
pData = new char[NULL];
pData[length] = '';
}
/*
Input - c-string
copy this c-string into MyString. update critical value in MyString
output - returns nothing.
*/
MyString::MyString(char *cString) //copy constructor of an existing c-string
{
length = strlen(cString); //using build in function to get the length of string we are copying
this->pData = new char[length + 1]; //creating are array of the appropriate length to hold the c-string
for (int i = 0; i < length; i++) //copying array contents one by one
{
pData[i] = cString[i];
}
pData[length] = '';
}
/*
Input - MyString
initialize MyString to the same values of the inputed MyString
OutPut - nothing
*/
MyString::MyString(MyString const& s) //we already have MyString just need to copy. Will copy one element at a time until NULL
{
length = s.length; //our cstring will be at least one character long
this->pData = new char[length + 1];
for (int i = 0; i < length; i++)
{
pData[i] = s.pData[i]; //copying passed mystring into curret cstring
}
pData[length] = ''; //adding null character to end of array
}
/*
Input - nothing
de-allocate memory
Output - nothing
*/
MyString::~MyString() //deletes dynamic array and deletes references to it
{
if (pData != NULL)
{
delete [] pData;
pData = NULL;
length = NULL;
}
}
/*
Input - MyString - constant we do(MyString1 = MyString2)
We assign the inputed MyString2 to MyString1. Update length of MyString1. Copy element by element
Output - MyString1
*/
MyString& MyString::operator=(MyString const& s)
{
length = s.length;
for (int i = 0; i < s.length; i++)
{
this->pData[i] = s.pData[i];
}
pData[length] = '';
return *this;
}
/*
Input - MyString constant
Get Length of left and right side arrays. Add them together to get the new length. Update length in left side array.
Add the characters from the right side array to the end of the left side array. Add terminating character to end of
array.
Output - Return the left side array, which is now leftside + rightside
*/
MyString& MyString::operator+(MyString const& s)
{
int lLength = length;
length = lLength + s.length;
for (int i = lLength; i < length; i++)
{
this->pData[i] = s.pData[i-lLength];
}
this->pData[length] = '';
return *this;
}
/*
Input - Nothing
Output - pData at indicated element until termination character is met
*/
void MyString::Put()
{
for(int i = 0; pData[i] != ''; i++)
{
cout << pData[i];
}
}
/*
Input - Nothing
Reverse function. Create a temp value to hold value to be swapped. -1 from length to account for termination character. Swap 0 with max.
Increment low value. Decrement High Value. Check to see that high-low is >=1. If true continue. Else Exit. Rinse. Repeat.
output - Nothing
*/
void MyString::Reverse()
{
int max = length - 1; //dont want to be moving the null character
char temp = NULL;
for(int i = 0; (max - i) >= 1; i++)
{
temp = pData[i];
this->pData[i] = this->pData[max];
this->pData[max] = temp;
max--;
}
}
#endif
============================================================================
//MyString.h
#ifndef MYSTRING_H
#define MYSTRING_H
class MyString
{
private:
char *pData; //pointer to simple C-style representation of the string
//(i.e., sequence of characters terminated by null)
//pData is only a pointer. You must allocate space for
//the actual character data
int length; //length of the string
//
//possible other private data
public:
MyString(); //constructor --- create empty string
MyString(char *cString); //constructor --- create a string whose data is a copy of
//cString
~MyString(); //destructor -- don't forget to free space allocated by the constructor
//i.e., the space allocated for the character data
MyString(MyString const& s); //override the default copy constructor --- why?
//important -- think about it -- possible test question
MyString& operator = (MyString const& s); //override default assignment operator
void Put(); //output string
void Reverse(); //reverse the string
MyString& operator + (MyString const& s); //concatenation operator
// //other useful member functions
//as you wish
};
#endif /
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.