Question

Implement the following in C++ Imagine that you are an engineer on a distant planet. You...

Implement the following in C++

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.

    resrcStack

The class is defined according to the simple UML diagram below:

resrcStack<T>
-top: stackNode<T>*
----------------------------
+resrcStack()
+~resrcStack()
+push(t: stackNode<T>*):void
+pop():void
+peek():stackNode<T>*
+print():void
+determineTransportRequirement():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 front.

pop: This will remove the top stackNode 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: Unrefined Ores
         Quantity: 3500
         Resource: Refined Alloys
         Quantity: 30000

determineTransportRequirement: This function will be used to determine what sort of transportation will be required to move the resources contained within the stack to their required destinations. The result of this function is a string returned for the following conditions:
                                1. drone: If the number of nodes is less than or equal to 5, drones will required.
                                2. car: If the quantity of the items in total, is greater than 50 and the items number at no more than 10, a car will be required. Quantity of items refers to the sum of the quantity variables in each of the stack nodes.
                                3. train: If the quantity of the items in total of the items is over 100, a train will be required. Quantity of items refers to the sum of the quantity variables in each of the stack nodes.

Return either drone, car or train depending on the conditions present. If none of the conditions are met, return ”LOGISTICS ERROR” without the quotation marks. The train takes precedence over the car which takes precedence over the drones for what to return.

==============stackNode.h=================================

#ifndef STACKNODE_H
#define STACKNODE_H
#include<iostream>
#include<string>
using namespace std;

// Defines a class Node
template <class T>
class stackNode
{
private:
   T resrc;
   int quantity;
public:
   stackNode *next;
   stackNode(T i, int q)
   {
       resrc = i;
       quantity = q;
   }

   ~stackNode()
   {
       delete next;
   }

   T getResrc()
   {
       return resrc;
   }

   int getQuantity()
   {
       return quantity;
   }
};
#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

resrcStack.h

====================

#pragma once
#include "stackNode.h"

template <typename T>
class resrcStack {
private:
   stackNode<T>* top; // The current top of the stack
public:
   resrcStack(); // constructor
   ~resrcStack(); // destructor
   void push(stackNode<T>* t); // add a node at front of list
   void pop(); // removes top node from the stack
   stackNode<T>* peek(); // return the top node of the stack but without removing it
   void print(); // print the entire contents of the stack
   string determineTransportRequirement(); // return either drone, car or train
       // depending on the conditions present
};

template <typename T>
resrcStack<T>::resrcStack() {
   // initialize members
   top = nullptr;
}

template <typename T>
resrcStack<T>::~resrcStack() {
   // create a iterator
   stackNode<T>* itr = top;
   // loop each element in stack
   while (itr != nullptr) {
       //move top to next element in the stack
       top = top->next;
       // delete the element
       delete itr;
       // reassign iterator
       itr = top;
   }
}

template <typename T>
void resrcStack<T>::push(stackNode<T>* t) {
   // check if top is null
   if (top == nullptr) {
       // assign new node to top
       top = t;
   }
   else {
       // assign new node at top ofthe stack
       t->next = top;
       // reassign top of the stack
       top = t;
   }
}

template <typename T>
void resrcStack<T>::pop() {
   // check if top is null
   if (top == nullptr) {
       // print message that idicate the stack is empty
       cout << "EMPTY" << endl;
   }
   else {
       // remove firsrt element from the stack
       // assign temparary pointer to top
       stackNode<T>* temp = top;
       // move top to next element
       top = top->next;
       // delete temparary element
       delete temp;
   }
}

template <typename T>
stackNode<T>* resrcStack<T>::peek() {
   // return pointer to first element in the stack
   return top;
}

template <typename T>
void resrcStack<T>::print() {
   // print the entire contents of the stack, line by line
   // create an iterator to iterate over stack
   stackNode<T>* itr = top;
   while (itr != nullptr) {
       // print element
       cout << "Resource: " << itr->getResrc() << endl;
       cout << "Quantity: " << itr->getQuantity() << endl;
       // move itrerator to next element
       itr = itr->next;
   }
}

template <typename T>
string resrcStack<T>::determineTransportRequirement() {
   // get number of elements
   int number_of_item = 0;
   // get total quantity of item
   int total_quantity = 0;
   // create iterator to iterate over stack
   stackNode<T>* itr = top;
   while (itr != nullptr) {
       // increase item number
       number_of_item++;
       // increase item quantity
       total_quantity = total_quantity + itr->getQuantity();
       // set iterator to next element
       itr = itr->next;
   }
   // check if cargo canbe transported with drone
   if (number_of_item < 6 && total_quantity <= 50) {
       return "Drone";
   }
   else if (number_of_item < 11 && total_quantity > 50 && total_quantity <= 100) {
       return "Car";
   }
   else if (total_quantity > 100) {
       return "Train";
   }
   else {
       return "LOGISTICS ERROR";
   }
}

====================

main.cpp

====================

#include "resrcStack.h"

int main() {
   cout << "Establishing automated train system" << endl;
   resrcStack<string>* stack = new resrcStack<string>();
   cout << "Mining ores" << endl;
   stackNode<string>* ore = new stackNode<string>("Unrefined Ore", 10);
   cout << "Putting ores on stack" << endl;
   stack->push(ore);
   cout << "Current stack is:" << endl;
   stack->print();
   cout << "Required transport is: " << stack->determineTransportRequirement() << endl;
   cout << endl;

   cout << "Gathering wood" << endl;
   stackNode<string>* wood = new stackNode<string>("Logs", 45);
   cout << "Putting wood on stack" << endl;
   stack->push(wood);
   cout << "Current stack is:" << endl;
   stack->print();
   cout << "Required transport is: " << stack->determineTransportRequirement() << endl;
   cout << endl;

   cout << "Mining fuel" << endl;
   stackNode<string>* fuel = new stackNode<string>("Unrefined Oil", 70);
   cout << "Putting fuel on stack" << endl;
   stack->push(fuel);
   cout << "Current stack is:" << endl;
   stack->print();
   cout << "Required transport is: " << stack->determineTransportRequirement() << endl;
   cout << endl;

   cout << "First item on stack is: " << stack->peek()->getResrc() << endl;
   cout << "Removing first item from stack." << endl;
   stack->pop();
   cout << "Current stack is:" << endl;
   stack->print();

   return 0;
}

====================

stackNode.h

====================

#ifndef STACKNODE_H
#define STACKNODE_H
#include<iostream>
#include<string>
using namespace std;

// Defines a class Node
template <class T>
class stackNode {
private:
   T resrc;
   int quantity;
public:
   stackNode* next;
   stackNode(T i, int q) {
       resrc = i;
       quantity = q;
   }

   ~stackNode() {
       // why its deleting next node?
       // it should delete current node
       // delete next;
       // there is no dynamicaly assign memory in this class
       // so no deletion should be done
   }

   T getResrc() {
       return resrc;
   }

   int getQuantity() {
       return quantity;
   }
};
#endif

the stackNode class requires modification in its destructor , i have added comments in code.

let me know if have any doubts or problem running code.

Add a comment
Know the answer?
Add Answer to:
Implement the following in C++ Imagine that you are an engineer on a distant planet. You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with a c++ code, i am new learner on stack, please help me...

    I need help with a c++ code, i am new learner on stack, please help me write Stack.cpp according to Stack.h provided, thanks(Notes: That data structure is a singly linked list in which pushed items are placed at the tail of the linked list. Similarly, popped items will be removed from the tail of the list.) Actually,I can write a regular one, but i'm not sure how to use the tail in the question. class Stack { private: // Desc:...

  • I have added a little Code but I need help with the rest. /** A class...

    I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> {    // Data fields    private Node<T> topNode; // references the first node in the chain    private int numberOfEntries;       public...

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

  • PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement...

    PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement an algorithm to determine the next greater element of an element in an array in Θ(n) time, where 'n' is the number of elements in the array. You could use the Stack ADT for this purpose. The next greater element (NGE) for an element at index i in an array A is the element that occurs at index j (i < j) such that...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)   ...

    Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)    at InfixExpression.Evaluate(InfixExpression.java:65)    at InfixExpression.setWholeExpr(InfixExpression.java:24)    at InfixExpression.<init>(InfixExpression.java:17)    at Main.testHW1(Main.java:17)    at Main.main(Main.java:6)" I need to get this as my output: "Testing InfixExpression: When passing null, the String and double = Infix String: , result: 0.0 When passing a valid String, the String and double = Infix String: ( 234.5 * ( 5.6 + 7.0 ) ) / 100.2, result: 29.488023952095805 ..." I...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • For this computer assignment, you are to write a C++ program to implement a class for...

    For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT