HI, I am having trouble finsihing this program i was wondering if someone could help me. Thanks in adavnce.
Here is the code that I have so far.....
//Stack.h
// implementation file for the stack class
#include <iostream>
using namespace std;
const int stack_size = 100;
class stack
{
private:
char data [stack_size]; // elements in the stack
int top; // index of the top element of the stack
public:
stack (); // constructor creates an empty stack
void push (char item); // adds an element to the top of the
stack
char pop (); // removes and returns the top element
bool empty (); // returns true if stack is empty
bool full (); // returns true if stack is full
};
// constructor creates an empty stack
stack::stack ()
{
top = -1;
}
// push adds an element, item, to the top of the stack
void stack::push (char item)
{
// if the stack is full, print an error message
if (full ())
{
cout << "\n\nStack Class Error: Pushing on a full
stack";
cout << "\nElement being pushed is " << item;
}
else // OK to push the new element
{
top++;
data [top] = item;
}
}
// pop removes and returns the top element from the stack
char stack::pop ()
{
// if the stack is empty, print an error message
if (empty ())
{
cout << "\n\nStack Class Error: Popping an empty
stack";
cout << "\nReturning a ?";
return '?';
}
else // OK to pop the stack
{
top--;
return data [top + 1];
}
}
// empty returns true if the stack is empty, else it returns
false
bool stack::empty ()
{
return top == -1;
}
// full returns true if the stack is full, else it returns
false
bool stack::full ()
{
return top == stack_size - 1;
}
--------------
// Inventory.cpp
#include <cstdlib>
#include <iostream>
#include <ctime>
#include "stack.h"
using namespace std;
int main()
{
int total_days = 10, // total number of days in the
simulation
print_days = 5, // print results every print_days days
max_package_size = 100, // largest vaccine package in terms of how
many vaccines
// are in it (packages sizes will be randomly
// set from 1 to max_package_size)
add_packages = 10, // number of packages added each minute
total_vaccines = 250, // total number of vaccines a lab technical
can process
// test and ship a day
Lab_techs; // number of postal workers read from user
return 0;
}
. Make the following changes to the stack class:
Make all necessary changes to have the class store integers on the stack instead of characters. pop should return -1 if called when the stack is empty.
Add a print member function that prints the values in the stack from top to bottom, one per line. print should not change the stack.
Write an elements member function that returns the number of elements on the stack. elements should not print anything. elements should not change the stack.
Write a peek member function that returns the top element on the stack. peek returns -1 if the stack is empty. peek should not change the stack.
2. Write a simulation of package handling at the HealthTime warehouse in
the file Inventory.cpp. There will be a stack of packages to be handled.
The following variables are used in the program. A number of them have
set values. Your program should still work properly if the values are
changed.
// total number of days in the simulation
int total_days = 10,
// print results every print_days days
print_days = 5,
// largest vaccine package in terms of how many vaccines
// are in it (packages sizes will be randomly
// set from 1 to max_package_size)
max_package_size = 100,
// number of packages added to inventory each day
add_packages = 10,
// total number of vaccines a lab technician can process
// test and ship a day
total_vaccines = 250,
// number of lab technicians read from user
Lab_techs;
The simulation will run for total_days simulated days. The current state of the stack will be printed every print_days days. Packages on the stack will be represented by integers indicating their how many vaccine doses are in them. They are (random values from 50 to max_package_size). There will be add_packages random packages added each day. Each lab technician can only handle total_vaccines each day taken from the top of the stack. So, if total_vaccines is 250, and the current stack contains these packages:
92 vaccines
84 vaccines
51 vaccines
68 vaccines
the lab technician could only handle the top three packages. The forth package would be handled be a second lab tech or stay in the stack. There will be Lab_techs (entered by the user) employees working.
A general algorithm for the simulation would thus be:
read number of Lab_techs
for (days from 1 to total_days)
{
for (number of add_packages)
{
generate random number between 50 and max_pkg_size
push random number to the stack of vaccines
}
for (number of lab techs)
{
while(sum < total_vaccines)
{
add vaccines at top of stack to sum
if sum < total_vaccines
- pop element
- subtract 1 from elements in stack
else
break
}
}
if(it is time to print based on print_days)
{
print the current day
print the stack of packages
print the number of packages in the stack
}
}
The following page has a sample run. To make the run shorter,
total_days was set equal to 5).
Feel free to change the variable values to help test/debug your
program. The simulation should generate different sets of random
package sizes (between 50 and 100) each run (that is, use
srand appropriately).
Please enter the number of Lab technicians: 1
Time: 1 Days
Packages in stack:
89 vaccines
58 vaccines
72 vaccines
84 vaccines
57 vaccines
98 vaccines
55 vaccines
59 vaccines
Number of packages in stack: 8
Time: 2 Days
Packages in stack:
99 vaccines
79 vaccines
96 vaccines
60 vaccines
54 vaccines
94 vaccines
70 vaccines
91 vaccines
89 vaccines
58 vaccines
72 vaccines
84 vaccines
57 vaccines
98 vaccines
55 vaccines
59 vaccines
Number of packages in stack: 16
Time: 3 Days
Packages in stack:
50 vaccines
63 vaccines
91 vaccines
90 vaccines
70 vaccines
100 vaccines
63 vaccines
99 vaccines
79 vaccines
96 vaccines
60 vaccines
54 vaccines
94 vaccines
70 vaccines
91 vaccines
89 vaccines
58 vaccines
72 vaccines
84 vaccines
57 vaccines
98 vaccines
55 vaccines
59 vaccines
Number of packages in stack: 23
Time: 4 Days
Packages in stack:
97 vaccines
61 vaccines
56 vaccines
76 vaccines
63 vaccines
97 vaccines
55 vaccines
50 vaccines
63 vaccines
91 vaccines
90 vaccines
70 vaccines
100 vaccines
63 vaccines
99 vaccines
79 vaccines
96 vaccines
60 vaccines
54 vaccines
94 vaccines
70 vaccines
91 vaccines
89 vaccines
58 vaccines
72 vaccines
84 vaccines
57 vaccines
98 vaccines
55 vaccines
59 vaccines
Number of packages in stack: 30
#include <iostream> using namespace std; const int stack_size = 100; class stack { private: int data [stack_size]; // elements in the stack int top; // index of the top element of the stack public: stack (); // constructor creates an empty stack void push (int item); // adds an element to the top of the stack int pop (); // removes and returns the top element bool empty (); // returns true if stack is empty bool full (); // returns true if stack is full void print(); int elements(); int peek(); }; // constructor creates an empty stack stack::stack () { top = -1; } // push adds an element, item, to the top of the stack void stack::push (int item) { // if the stack is full, print an error message if (full ()) { cout << "\n\nStack Class Error: Pushing on a full stack"; cout << "\nElement being pushed is " << item; } else // OK to push the new element { top++; data [top] = item; } } // pop removes and returns the top element from the stack int stack::pop () { // if the stack is empty, print an error message if (empty ()) { cout << "\n\nStack Class Error: Popping an empty stack"; cout << "\nReturning a ?"; return -1; } else // OK to pop the stack { top--; return data [top + 1]; } } // empty returns true if the stack is empty, else it returns false bool stack::empty () { return top == -1; } // full returns true if the stack is full, else it returns false bool stack::full () { return top == stack_size - 1; } void stack::print() { if(empty()) { cout << "Stack is empty" << endl; } else { cout << "Stack content:" << endl; for(int i=top; i>=0; i--) { cout << data[i] << endl; } } } int stack::elements() { if(empty()) { return 0; } else { return top + 1; } } int stack::peek() { if(empty()) { return -1; } else { return data[top]; } } int main() { // total number of days in the simulation int total_days = 5, // print results every print_days days print_days = 1, // largest vaccine package in terms of how many vaccines // are in it (packages sizes will be randomly // set from 1 to max_package_size) max_package_size = 100, // number of packages added to inventory each day add_packages = 10, // total number of vaccines a lab technician can process // test and ship a day total_vaccines = 250, // number of lab technicians read from user lab_techs; stack vaccines; cout << "Please enter the number of Lab technicians: "; cin >> lab_techs; for(int days = 1; days <= total_days; days++) { for (int i=0; i< add_packages; i++) { int size = 50 + rand() % (max_package_size - 50 + 1); vaccines.push(size); } for(int i=0; i < lab_techs; i++) { int sum = 0; while(sum < total_vaccines) { sum += vaccines.peek(); if(sum < total_vaccines) { vaccines.pop(); } else { break; } } } cout << endl << endl; if(days % print_days == 0) { cout << "Time: " << days << " Days:" << endl; cout << "packages in stack:" << endl; vaccines.print(); cout << "number of packages in stack: " << vaccines.elements() << endl; } } return 0; }
Hi. please find the answer above.. i have given comments so that it is very easy for you to understand the flow. In case of any doubts, please ask in comments. If the answer helps you, please upvote. Thanks!
HI, I am having trouble finsihing this program i was wondering if someone could help me....
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...
Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...
I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out .println("Enter a sentence, I will tell you if it is a palindrome: ");...
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...
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:...
Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DEFINITIONS NEEDED for the OPERATIONS. OUTPUT: PRINT ALL THE ELEMENTS ON THE STACK. Stack Operations initializestack: Initializes the stack to an empty state. isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true; otherwise, it returns the value false. isFul1stack: Determines whether the stack...
HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...
(C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...
C++ Please ensure code is fully working, will rate Question to be answered: Repeat Exercise 1 for the class linkedStackType question one: Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...
C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...