In: Computer Science
(C++ with main to test)
Task 1
Imagine that you are an engineer on a distant planet. You were
tasked with mining the
resources of the planet and have done so through largely primitive
means. However you
have managed to set up an automated train system to gather and
collect the resources
from your distant operations. Your train system collects boxes full
of resources. A box
might contain ores or wood or even liquid fuel. These boxes are
placed into the train
using drones who stack them on each other to save space. You now
need to implement
a management system using the newly researched data structures that
will prove useful.
In particular, the rapid pace of your development means that
resources are accumulated
quickly and fixed sized structures would be unwise. Therefore you
will be implementing
the stack through the use of a linked list.
2.2.1
resrcStack
The class is defined according to the simple UML diagram
below:
resrcStack<T>
-top: stackNode<T>*
2----------------------------
+resrcStack()
+~resrcStack()
+push(t: stackNode<T>*):void
+pop():void
+peek():stackNode <T>*
+print():void
+tallyWeights():int
+calculateStorageRequirements():string
The class variables are defined below:
• top: The current top of the stack. It will start as null but
should refer to the top of
the stack.
The class methods are defined below:
• resrcStack: The class constructor. It will start by initialising
the variables to null.
• ∼resrcStack: The class destructor. It will deallocate all of the
memory assigned by
the class.
• push: This will receive a stackNode to add onto the current
stack. The node is
added from the top.
• pop: This will remove the top resrcNode from the stack. If it is
empty, print out
”EMPTY” with a newline at the end and no quotation marks. When
removing the
node, it should be deleted.
• peek: This will return the top node of the stack but without
removing it from the
stack.
• print: This will print the entire contents of the stack. For each
resrc node in the
stack, print the following information out sequentially, line by
line. The format of
this is as follows:
Resource:
Quantity:
Resource:
Quantity:
Unrefined Ores
12
Refined Alloys
1000
Remember that when printing, the first nodes added will be printed
last. The first
node to be printed should be the last node pushed into the
stack.
• tallyWeights: This function will tally up the weights for all
nodes in the stack and
return their total weight. If empty, return -1.
• calculateStorageRequirements: This function must determine what
the storage re-
quirement for the materials contained in the stack. The resulting
type of storage
unit is returned. The storage requirements are determined as
follows:
31. wooden crate: If the weights of the items together are less
than 100, a wooden
crate should prove sufficient.
2. steel crate: A steel crate will be required if the weights of
the items total 200
or more and the number of nodes, is greater than 5 but less than
10.
3. silo: A silo is required when the number of node is greater than
10 under all
circumstances.
This should return wooden crate, steel crate or silo. If none of
the conditions are
met, return ”LOGISTICS ERROR” without the quotation marks. The
wooden
crate takes precedence over the steel crate which takes precedence
over the silo for
what to return.
2.2.2
stackNode
The class is defined according to the simple UML diagram
below:
stackNode<T>
-resrc:T
+next:stackNode *
-weight: int
------------------------
+stackNode(i:T,w:int)
+~stackNode()
+getResrc():T
+getWeight():int
The class variables are defined below:
• resrc: This is the basic resource unit. It will describe the
resource contained within
the box. It might be a code that describes the contents or just a
description and so
must be a template type to accommodate for a variety of
manifests.
• next: A pointer to the next node of the stack.
• weight: A variable which describes the total weight of the
resources contained within
the class.
The class methods are defined below:
• stackNode: A class constructor. It receives the template type and
the weight for
that item.
• ∼stackNode: The class destructor. It should print out ”Resource
Unit Destroyed”
with no quotation marks and a new line at the end.
• getResrc: This returns the template variable stored within the
class.
You will be allowed to use the following libraries: string,
iostream.
Source code:
#include <iostream>
using namespace std;
//stack node class
template <class T>
class stackNode{
T resrc;
int weight;
public:
stackNode* next;
stackNode(T i, int w){
resrc = i;
weight = w;
next = NULL;
}
T getResrc(){
return resrc;
}
int getWeight(){
return weight;
}
~stackNode(){
cout << "Resource Unit Destroyed" << endl;
}
};
//resrcStack class
template <class T>
class resrcStack{
stackNode<T>* top;
public:
//constructor
resrcStack(){
top = NULL;
}
//destructor
~resrcStack(){
delete top;
}
//push a node
void push(stackNode<T>* t){
if(top == NULL){
top = t;
}
else{
t->next = top;
top = t;
}
}
//pop a node
stackNode<T>* pop(){
stackNode<T>* st = NULL;
if(top == NULL){
cout << "EMPTY" << endl;
}
else{
st = top;
top = top->next;
}
return st;
}
//peek a node
stackNode<T>* peek(){
return top;
}
//print all nodes
void print(){
stackNode<T>* start = top;
while(start != NULL){
cout << "Resource: " << start->getResrc() <<
endl;
cout << "Quantity: " << start->getWeight() <<
endl;
start = start->next;
}
}
//calculate total weight
int tallyWeights(){
if(top == NULL){
return -1;
}
int total = 0;
stackNode<T>* start = top;
while(start != NULL){
total += start->getWeight();
start = start->next;
}
return total;
}
//calculate storage requirements
string calculateStorageRequirements(){
if(top == NULL){
return "LOGISTICS ERROR";
}
int sumWeight = 0;
int sumItems = 0;
stackNode<T>* start = top;
while(start != NULL){
sumWeight += start->getWeight();
sumItems++;
start = start->next;
}
if(sumWeight < 100)
return "wooden crate";
else if(sumWeight >= 200 && (sumItems > 5 &&
sumItems < 10))
return "steel crate";
else if(sumItems > 10)
return "silo";
else
return "LOGISTICS ERROR";
}
};
int main(){
resrcStack<string>* rstk = new
resrcStack<string>();
stackNode<string>* node;
//creating and pushing a node
node = new stackNode<string>("Unrefined Ores", 12);
rstk->push(node);
cout << "\nInserted a node: " << "Unrefined Ores, 12"
<< endl;
//creating and pushing a node
node = new stackNode<string>("Refined Alloys", 80);
rstk->push(node);
cout << "\nInserted a node: " << "Refined Alloys, 80"
<< endl;
//printing all nodes
cout << endl << "Details of all stack items:" <<
endl;
rstk->print();
//printing total weight of items
cout << "\nTotal weight of items: "
<< rstk->tallyWeights() << endl << endl;
//printing storage requirement of items
cout << "Storage requirement: "
<< rstk->calculateStorageRequirements() << endl
<< endl;
return 0;
}
Code
screenshot:
Output: