* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Download LL_starsCatalog
Timeline of astronomy wikipedia , lookup
Aquarius (constellation) wikipedia , lookup
Cygnus (constellation) wikipedia , lookup
Stellar evolution wikipedia , lookup
Dyson sphere wikipedia , lookup
Perseus (constellation) wikipedia , lookup
Star formation wikipedia , lookup
Corvus (constellation) wikipedia , lookup
Linked Lists I
This assignment demonstrates linked lists and how they can be managed from an object
oriented design perspective.
Create a StarCatalog application that maintains a list of stars and some information about
them. The information must be encapsulated in a class called Star. The following code
should be the contents of your Star.h file:
const int ENTRY_SZ = 256;
class Star
{
private:
char name_[ENTRY_SZ];
long temperature_;
double luminosity_;
double mass_;
double radius_;
public:
// Store the path name of the star catalog file
static char filePath_[ENTRY_SZ];
Star();
Star(char* name);
void SetTemperature(long temp);
void SetLuminosity(double lum);
void SetMass(double mass);
void SetRadius(double rad);
void SetTemperature(char* temp);
void SetLuminosity(char* lum);
void SetMass(char* mass);
void SetRadius(char* rad);
void PrintToConsole();
void AppendToFile();
};
Note that each Set method is overloaded so that they may be called with numeric or
character parameters. Your program does not have to call both of these but it is good to
include them both so that they are available to be used.
The application must use an unordered linked list to hold all the stars in memory. We will
create a linked list without using templates to illustrate an alternative method from what
is presented in the text book. Our linked list will be generic in that it will not refer to the
Star class at all. That way, your linked list class can be used to store any type of data,
without the use of templates. In this case the linked list will be able to store a list of any
kind of objects, including Star objects. The linked list will even be able to store a
mixture of different types of objects.
Your program must use the Node struct and LinkedList class shown below, defined
in LinkedList.h. The Node class has a data_ property that points to a Star object, but
notice that it is of type void*. A void pointer is used so that this class does not have to
know about the Star class. The LinkedList class has a first_ property that points to
the first Node in the list and a last_ property that points to the last Node in the list.
struct Node
{
void* data_;
Node* next_;
Node()
{
data_ = 0;
next_ = 0;
}
};
class LinkedList
{
private:
Node* first_;
Node* last_;
long listLen_;
public:
LinkedList();
~LinkedList();
void AddLinkToBack(void* ptr);
void* RemoveLinkFromFront();
Node* GetFirstNode();
long GetListLength();
};
A memory picture of these three constructs (Star class, LinkedList class, and the
Node struct) is shown below. The Node objects are contained in the linked list. The
LinkedList class can directly access the first and last entries in the list. The data_
property of the Node class is of type void*, which means it can contain the address of
any type of object. You are going to store the address of Star objects in them. As you
can see, there is one Node object associated with each Star object. So for each star
that your program reads in from a file or gets from the console, it must create a new
Star object and a new Node object.
first_
last_
data_
next_
name_
temp_
lum…
mass_
radius_
data_
next_
name_
temp_
lum…
mass_
radius_
data_
next_
name_
temp_
lum…
mass_
radius_
LinkedList
data_
next_
Nodes
name_
temp_
lum…
mass_
radius_
Stars
When the application runs it must display a console menu that looks like this:
a)
b)
c)
d)
e)
Open a star catalog file
Add a new star
Print all star data
Clear list
Quit
When a) is selected, the program must prompt the user to enter a file path for an input
star catalog file. It should open the file and read the data for each star from the file one at
a time. Note that each star is on a line where each value is comma separated, such as:
Orionis, 33000, 30000, 18.0, 5.90
Spica, 22000, 8300, 10.5, 5.10
For that star, it should dynamically create a Star object (using new), store the star data
into the object, and add that object to the linked list (which means it must dynamically
create a Node object as well). A star catalog file is provided and is called starCatalog.txt.
The Star object must be created and deleted in the main function, and the Node objects
should be created and deleted in the LinkedList class. It is generally better to prevent
the main function from needing to know anything about the Node type. That is a storage
detail and should be hidden. However, that requires a little more complexity. Therefore,
for this homework it is OK for the main function to know about the Node type.
When a) is selected a second time, the program must delete every link in the list before
reading the new star catalog file. Remember that the Star and the Node objects must be
deleted using the delete keyword.
The b) option should prompt the user to enter the data for a new star. The program must
dynamically create a Star object (using new), store the data into the object, and add that
object to the linked list (which means it must dynamically create a Node object as well).
After that link is added, the program must append the contents of the linked list to
the same star file.
The c) option should print the star data to the console with appropriate formatting.
The d) option should empty the linked list by calling a recursive function. Note that
because it must delete each Star object associated with each list node, this function must
not be in the LinkedList class.
Some Details:
If your program opens and closes a file more than one time, which it probably
does, add the following clear statement each time after closing it (otherwise, some
compilers, like mine, will not allow the file to be reopened):
inFile.close();
inFile.clear(std::ios_base::goodbit);
The LinkedList class must not contain any reference to the Star class. The
LinkedList.h and LinkedList.cpp files should also not contain #include
“Star.h”.
When adding links to the list, use the AddLinkToBack() method.
When deleting links, use the RemoveLinkFromFront() method.
The main function should look very similar to this:
void main()
{
bool terminate = false;
char selection;
while (!terminate)
{
selection = DisplayMenu();
switch(selection)
{
case 'a':
LoadTextFile();
break;
case 'b':
AddNewStar();
break;
case 'c':
PrintAllStarData();
break;
case 'd':
ClearList();
break;
case 'e':
terminate = true;
break;
default:
cout << "That is not a valid choice!\n";
}
}
}
Usage Examples:
User entries are bolded:
Please make a selection:
a) Open a star catalog file
b) Add a new star
c) Print all star data
d) Clear list
e) Quit
a
Enter the full file path:
C:\Miscellaneous\PCC\CS162\Hmwk3\starCatalog.txt
Loaded file and read 9 star objects.
Please make a selection:
a) Open a star catalog file
b) Add a new star
c) Print all star data
d) Clear list
e) Quit
c
Orionis, 33000, 30000, 18, 5.9
Spica, 22000, 8300, 10.5, 5.1
Rigel, 12500, 130, 3.5, 2.7
Sirius, 9500, 63, 2.6, 2.3
Altair, 8700, 24, 1.9, 1.8
AlphaCentauri, 5900, 1.45, 1.08, 1
Sun, 5800, 1, 1, 1
Pollux, 5100, 0.36, 0.83, 0.8
Wolf, 3000, 0.0002, 0.1, 0.1
Please make a selection:
a) Open a star catalog file
b) Add a new star
c) Print all star data
d) Clear list
e) Quit
b
Enter the
Enter the
Enter the
Enter the
Enter the
star
star
star
star
star
name: Becrux
temperature: 30000
luminosity: 16000
mass: 16.0
radius: 5.70
Please make a selection:
a) Open a star catalog file
b) Add a new star
c) Print all star data
d) Clear list
e) Quit
c
Orionis, 33000, 30000, 18, 5.9
Spica, 22000, 8300, 10.5, 5.1
Rigel, 12500, 130, 3.5, 2.7
Sirius, 9500, 63, 2.6, 2.3
Altair, 8700, 24, 1.9, 1.8
AlphaCentauri, 5900, 1.45, 1.08, 1
Sun, 5800, 1, 1, 1
Pollux, 5100, 0.36, 0.83, 0.8
Wolf, 3000, 0.0002, 0.1, 0.1
Becrux, 30000, 16000, 16, 5.7
File Organization:
Put the Star class declaration in a header file called Star.h. Put the Star class method
definitions in a source file called Star.cpp. Put the Node and LinkedList class
definitions in corresponding files LinkedList.h and LinkedList.cpp and the main program
into a main file called main.cpp.
Deliverables:
Upload the Star.cpp, Star.h, LinkedList.cpp, LinkedList.h and main.cpp files only. Any
other files, including zip files, will not be accepted.