Please help with this program main.cpp: // make NO CHANGES to this file #pragma
ID: 3543114 • Letter: P
Question
Please help with this program
main.cpp:
// make NO CHANGES to this file
#pragma warning(disable:4996) // disable warnings about use of strcpy()
#include "memoryleakdetect.h" // this must be the first #include in each of your .cpp files
#include <iostream>
#include "winery.h"
#include "list.h"
using namespace std;
static List *wineries;
// Ask list to insert a winery into the doubly-threaded linked list.
// Note what this function does with memory, which has implications for
// how you will (and will not) be able to use the winery instance that's
// passed to wineries->insert.
static void insertWinery(char *name, char *location, int acres, int rating)
{
Winery *w;
char *nm = new char[strlen(name) + 1];
char *loc = new char[strlen(location) + 1];
strcpy(nm, name);
strcpy(loc, location);
w = new Winery(nm, loc, acres, rating);
wineries->insert(*w);
delete[] nm;
delete[] loc;
delete w;
}
// Display all wineries in the list,
// first in order by name, then in order by rating.
static void displayWineries(ostream& out)
{
out << "+++ list by name" << endl;
wineries->displayByName(out);
out << endl << "+++ list by rating" << endl;
wineries->displayByRating(out);
}
int main(int argc, char **argv)
{
Winery *wPtr;
cout << "CS260 - Lab1 - " << Winery::YOUR_NAME << endl << endl;
wineries = new List();
insertWinery("Lopez Island Vineyard", "San Juan Islands", 7, 95);
insertWinery("Gallo", "Napa Valley", 200, 25);
insertWinery("Cooper Mountain", "Willamette Valley", 100, 47);
insertWinery("Duck Pond Cellars", "Willamette Valley", 845, 70);
insertWinery("Del Rio", "Bear Creek Valley", 200, 37);
insertWinery("Weisinger's of Ashland", "Bear Creek Valley", 6, 83);
insertWinery("LongSword", "Applegate Valley", 16, 85);
displayWineries(cout);
cout << endl << ">>> removing Cooper Mountain" << endl << endl;
wineries->remove("Cooper Mountain");
displayWineries(cout);
cout << endl << ">>> inserting San Juan Vineyards" << endl << endl;
insertWinery("San Juan Vineyards", "San Juan Islands", 20, 90);
displayWineries(cout);
cout << endl << ">>> search for "Gallo"" << endl << endl;
wPtr = wineries->find("Gallo");
if (wPtr != 0)
cout << wPtr;
else
cout << "not found" << endl;
cout << endl << ">>> search for "No Such"" << endl << endl;
wPtr = wineries->find("No Such");
if (wPtr != 0)
cout << wPtr;
else
cout << "not found" << endl;
cout << endl;
delete wineries;
// report on memory leaks in the Output Window
#ifdef _DEBUG
if (argc == 2) {
_CrtSetReportMode( _CRT_WARN , _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_WARN , _CRTDBG_FILE_STDERR );
}
_CrtDumpMemoryLeaks();
#endif
return 0;
}
list.cpp :
#include "memoryleakdetect.h" // this must be the first #include in each of your .cpp files
#include <iostream>
#include "list.h"
using namespace std;
List::List()
{
// your code here, or in this constructor's initialization list
}
List::~List()
{
// your code here
}
/*
You will need to uncomment this constructor and write the code for it.
List::Node::Node(const Winery& winery) :
// your initialization list here
{
// your code here
}
*/
void List::displayByName(ostream& out) const
{
// your code here
}
void List::displayByRating(ostream& out) const
{
// your code here
}
void List::insert(const Winery& winery)
{
// your code here
}
Winery * const List::find(const char * const name) const
{
// your code here, return the appropriate value
return 0;
}
bool List::remove(const char * const name)
{
// your code here, return the appropriate value
return false;
}
list.h :
// make NO CHANGES to this file
#pragma once // include this .h file file only once
#include <ostream>
#include "winery.h"
class List
{
public:
List(void); // constructor
virtual ~List(void); // destructor
// Print out the wineries in alphabetical order by name,
// by calling winery's operator<< for each winery.
void displayByName(std::ostream& out) const;
// Print out the wineries from highest to lowest rating,
// by calling winery's operator<< for each winery.
void displayByRating(std::ostream& out) const;
// Insert a winery into both the names and ratings threads.
// The names thread should be in alphabetical order by name.
// The ratings thread should be in order from highest rating
// to lowest rating.
void insert(const Winery& winery);
// Return a const pointer to the winery instance it finds in
// the list, or 0 if it didn't find a winery with that name.
// Because the pointer is declared const, there is no danger
// that find's caller will be able to use the returned pointer
// to change the instance of winery.
Winery * const find(const char * const name) const;
// Remove the winery with the specified name from both the name
// thread and the ratings thread. Returns true if it found and
// removed the winery, false if it did not find the winery.
bool remove(const char * const name);
private:
// defines each node in the doubly-threaded linked list.
struct Node
{
Node(const Winery& winery); // constructor
Winery item; // an instance of winery
// (NOT a pointer to an instance)
Node *nextByName; // next node in the name thread
Node *nextByRating; // next node in the rating thread
};
Node *headByName; // first node in the name thread
Node *headByRating; // first node in the rating thread
};
winery.cpp :
#include "memoryleakdetect.h" // this must be the first #include in each of your .cpp files
#include "winery.h"
using namespace std;
// change the value of this variable to be your own name instead of "I. Forgot"
const char Winery::YOUR_NAME[] = "I. Forgot";
Winery::Winery(const char * const name, const char * const location, const int acres, const int rating)
{
// your code here, or in this constructor's initialization list
}
Winery::~Winery()
{
// your code here
}
void Winery::displayColumnHeadings(ostream& out)
{
// print out column headings for lists of wineries, as specified by lab1output.txt
}
ostream& operator<<(ostream& out, Winery *w)
{
// print out a winery, as specified by lab1output.txt
return out;
}
winery.h :
// make NO CHANGES to this file
#pragma once // include this .h file file only once
#include <ostream>
class Winery
{
public:
static const char YOUR_NAME[]; // used for printing out programmer's name
Winery(const char * const name, const char * const location, const int acres, const int rating);
virtual ~Winery(void);
// complete implementations for the following 4 functions, nothing needed in the .cpp file
const char * const getName() const { return name; }
const char * const getLocation() const { return location; }
const int getAcres() const { return acres; }
const int getRating() const { return rating; }
// print out column headings for lists of wineries, as specified by lab1output.txt
static void displayColumnHeadings(std::ostream& out);
// print out a winery, as specified by lab1output.txt
friend std::ostream& operator<<(std::ostream& out, Winery *w);
private:
char *name;
char *location;
int acres;
int rating;
};
Explanation / Answer
struct Node
{
Node(const Winery& winery); // constructor
Winery item;
Node *nextByName;
Node *nextByRating;
};
class List
{
...
private:
Node *headByName;
Node *headByRating;
};
The List class needs to provide operations to:
add a winery
remove a winery
display all the wineries sorted by their ratings
display all the wineries sorted by their names
search for a winery by name
Strings
For storing and printing strings, you should use C strings. Your code should #include <cstring> and use the functions defined in that header, such as:
int strlen(char *str)
// given a string pointer, returns the length of that string,
// which does not include the terminating '' character
void strcpy(char *dst, char *src)
// copies the characters in src to dst,
// including the terminating '' character
Using strcpy will cause a compiler warning. You will need to get rid of this warning by adding one of the following as the first line of .cpp files that use strcpy:
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
Printed Output
Your code