Question: Please help with the following C++ program and show outputs for each step. This is my code so far: //Code #include #include using namespace std;

Please help with the following C++ program and show outputs for each step.

Please help with the following C++ program and show outputs for each

This is my code so far:

//Code

#include #include using namespace std; //definition of the template class stackType template class stackType { // data memebers of the class private : int maxStackSize; int stackTop; Type *list; // data methods of the class public : void initializeStack(); bool isFullStack() const; bool isEmptyStack() const; void push( const Type& ); void pop(); Type top() const; stackType( int = 20 ); ~stackType();

bool operator==( const stackType& );

}; // end template class stackType

// initialize the stack template void stackType::initializeStack() { stackTop = 0; } // end function initializeStack

// check for stack fullness template bool stackType::isFullStack() const { return ( stackTop == maxStackSize ); } // end function isFullStack

// check for stack empty template bool stackType::isEmptyStack() const { return ( stackTop == 0 ); } // end function isEmptyStack

// insert an element into stack template void stackType::push( const Type& newItem ) { if ( !isFullStack() ) { list[ stackTop ] = newItem; stackTop++; } // end if else cout

// delete an element from the stack template void stackType::pop() { if ( !isEmptyStack() ) stackTop--; else cout

// return the value of stack-top template Type stackType::top() const { assert( stackTop != 0 ); return list[ stackTop - 1 ]; } // end function top

// constructor for the class stackType template stackType::stackType( int stackSize ) { if ( stackSize

stackTop = 0; list = new Type[ maxStackSize ]; } // end constructor stackType

// destructor for the class stackType template stackType::~stackType() { delete[] list; } // end destructor stackType

// overload the equality operator template bool stackType::operator== ( const stackType& right ) { // check for same number of elements if ( this->stackTop != right.stackTop ) return false; //check for equality of elements at corresponding positions for ( int i = 0; i list[ i ] != right.list[ i ] ) return false;

return true; }

//main function int main() { // let the user know about the program cout s1( 12 ); stackType s2( 15 ); // insert elements into the stacks cout

//check and print whether the stacks are equal or not if ( s1 == s2 ) cout

// insert one more element into the second stack cout

//check and print whether the stacks are equal or not if ( s1 == s2 ) cout

cout

9.

//Code

#include #include #include #include

#include "simulation.h"

using namespace std;

void setSimulationParameters(int& sTime, int& numOfServers, int& transTime, int& tBetweenCArrival); bool isCustomerArrived(double arvTimeDiff);

void generateStatistics(serverListType& serverList, queue& CQueue, int numOfCustArrived, int waitTimeServedCustomers); void runSimulation();

int main() { runSimulation();

system("pause"); return 0; }

void setSimulationParameters(int& sTime, int& numOfServers, int& transTime, int& tBetweenCArrival) { cout > sTime; cout

cout > numOfServers; cout

cout > transTime; cout

cout > tBetweenCArrival; cout

bool isCustomerArrived(double arvTimeDiff) { double value;

value = static_cast (rand()) / static_cast(RAND_MAX);

return (value > exp(-1.0 / arvTimeDiff)); }

void runSimulation() { int simulationTime; int numberOfServers; int transactionTime; int timeBetweenCustomerArrival;

queue customerQueue;

customerType customer;

int custNumber = 0;

int totalWaitTimeServedCustomers = 0; int totalWaitTime = 0; int numberOfCustomersServed = 0; int customersLeftInServers = 0; int clock = 0; int serverID;

setSimulationParameters(simulationTime, numberOfServers, transactionTime, timeBetweenCustomerArrival);

serverListType serverList(numberOfServers);

for (clock = 1; clock

if (!customerQueue.empty()) { for (int sz = customerQueue.size(); sz > 0; sz--) { customerType cust = customerQueue.front(); customerQueue.pop(); cust.incrementWaitingTime(); customerQueue.push(cust); } }

if (isCustomerArrived(timeBetweenCustomerArrival)) { custNumber++; customer.setCustomerInfo(custNumber, clock, 0, transactionTime); customerQueue.push(customer); cout

serverID = serverList.getFreeServerID(); if (serverID != -1 && !customerQueue.empty()) { customer = customerQueue.front(); customerQueue.pop(); totalWaitTimeServedCustomers = totalWaitTimeServedCustomers + customer.getWaitingTime(); serverList.setServerBusy(serverID, customer); } }

cout

cout

generateStatistics(serverList, customerQueue, custNumber, totalWaitTimeServedCustomers); }

void generateStatistics(serverListType& serverList, queue& CQueue, int numOfCustArrived, int waitTimeServedCustomers) { int customersLeftInQueue = 0;

int totalWaitTime = waitTimeServedCustomers;

customerType customer;

while (!CQueue.empty()) { customer = CQueue.front(); CQueue.pop(); totalWaitTime = totalWaitTime + customer.getWaitingTime(); customersLeftInQueue++; }

//Find number of customers left in servers int customersLeftInServers = serverList.getNumberOfBusyServers(); //Find number of customers completely served int numberOfCustomersServed = numOfCustArrived - customersLeftInServers - customersLeftInQueue;

double averageWaitTime = 0;

cout

if (numOfCustArrived > 0) // If number of customers arrived is > 0 averageWaitTime = (static_cast(totalWaitTime)) / numOfCustArrived;

cout

// simulation.h

#include #include #include

using namespace std;

//**************** customerType **************** class customerType { public: customerType(int cN = 0, int arrvTime = 0, int wTime = 0, int tTime = 0); //Constructor to initialize the instance variables //according to the parameters //If no value is specified in the object declaration, //the default values are assigned. //Postcondition: customerNumber = cN; // arrivalTime = arrvTime; // waitingTime = wTime; // transactionTime = tTime

void setCustomerInfo(int customerN = 0, int inTime = 0, int wTime = 0, int tTime = 0); //Function to initialize the instance variables. //Instance variables are set according to the parameters. //Postcondition: customerNumber = customerN; // arrivalTime = arrvTime; // waitingTime = wTime; // transactionTime = tTime;

int getWaitingTime() const; //Function to return the waiting time of a customer. //Postcondition: The value of waitingTime is returned.

void setWaitingTime(int time); //Function to set the waiting time of a customer. //Postcondition: waitingTime = time;

void incrementWaitingTime(); //Function to increment the waiting time by one time unit. //Postcondition: waitingTime++;

int getArrivalTime() const; //Function to return the arrival time of a customer. //Postcondition: The value of arrivalTime is returned.

int getTransactionTime() const; //Function to return the transaction time of a customer. //Postcondition: The value of transactionTime is returned.

int getCustomerNumber() const; //Function to return the customer number. //Postcondition: The value of customerNumber is returned.

private: int customerNumber; int arrivalTime; int waitingTime; int transactionTime; };

//************* serverType **************** class serverType { public: serverType(); //Default constructor //Sets the values of the instance variables to their default //values. //Postcondition: currentCustomer is initialized by its // default constructor; status = "free"; and // the transaction time is initialized to 0.

bool isFree() const; //Function to determine if the server is free. //Postcondition: Returns true if the server is free, // otherwise returns false.

void setBusy(); //Function to set the status of the server to busy. //Postcondition: status = "busy";

void setFree(); //Function to set the status of the server to "free". //Postcondition: status = "free";

void setTransactionTime(int t); //Function to set the transaction time according to the //parameter t. //Postcondition: transactionTime = t;

void setTransactionTime(); //Function to set the transaction time according to //the transaction time of the current customer. //Postcondition: // transactionTime = currentCustomer.transactionTime;

int getRemainingTransactionTime() const; //Function to return the remaining transaction time. //Postcondition: The value of transactionTime is returned.

void decreaseTransactionTime(); //Function to decrease the transactionTime by 1 unit. //Postcondition: transactionTime--;

void setCurrentCustomer(customerType cCustomer); //Function to set the info of the current customer //according to the parameter cCustomer. //Postcondition: currentCustomer = cCustomer;

int getCurrentCustomerNumber() const; //Function to return the customer number of the current //customer. //Postcondition: The value of customerNumber of the // current customer is returned.

int getCurrentCustomerArrivalTime() const; //Function to return the arrival time of the current //customer. //Postcondition: The value of arrivalTime of the current // customer is returned.

int getCurrentCustomerWaitingTime() const; //Function to return the current waiting time of the //current customer. //Postcondition: The value of transactionTime is // returned.

int getCurrentCustomerTransactionTime() const; //Function to return the transaction time of the //current customer. //Postcondition: The value of transactionTime of the // current customer is returned.

private: customerType currentCustomer; string status; int transactionTime; };

//************* serverListType **************** class serverListType { public: serverListType(int num = 1); //Constructor to initialize a list of servers //Postcondition: numOfServers = num // A list of servers, specified by num, // is created and each server is // initialized to "free".

~serverListType(); //Destructor //Postcondition: The list of servers is destroyed.

int getFreeServerID() const; //Function to search the list of servers. //Postcondition: If a free server is found, returns // its ID; otherwise, returns -1.

int getNumberOfBusyServers() const; //Function to return the number of busy servers. //Postcondition: The number of busy servers is returned.

void setServerBusy(int serverID, customerType cCustomer, int tTime); //Function to set a server as busy. //Postcondition: The server specified by serverID is set // to "busy", to serve the customer // specified by cCustomer, and the // transaction time is set according to the // parameter tTime.

void setServerBusy(int serverID, customerType cCustomer); //Function to set a server as busy. //Postcondition: The server specified by serverID is set // to "busy", to serve the customer // specified by cCustomer.

void updateServers(ostream& outFile); //Function to update the status of a server. //Postcondition: The transaction time of each busy // server is decremented by one unit. If // the transaction time of a busy server // is reduced to zero, the server is set // to "free". Moreover, if the actual // parameter corresponding to outFile is // cout, a message indicating which customer // has been served is printed on the screen, // together with the customer's departing // time. Otherwise, the output is sent to // a file specified by the user.

private: int numOfServers; serverType *servers; };

//*************** customerType ************

void customerType::setCustomerInfo(int customerN, int arrvTime, int wTime, int tTime) { customerNumber = customerN; arrivalTime = arrvTime; waitingTime = wTime; transactionTime = tTime; }

customerType::customerType(int customerN, int arrvTime, int wTime, int tTime) { setCustomerInfo(customerN, arrvTime, wTime, tTime); }

int customerType::getWaitingTime() const { return waitingTime; }

void customerType::incrementWaitingTime() { waitingTime++; }

void customerType::setWaitingTime(int time) { waitingTime = time; }

int customerType::getArrivalTime() const { return arrivalTime; }

int customerType::getTransactionTime() const { return transactionTime; }

int customerType::getCustomerNumber() const { return customerNumber; } //**************** serverType **********

serverType::serverType() { status = "free"; transactionTime = 0; }

bool serverType::isFree() const { return (status == "free"); }

void serverType::setBusy() { status = "busy"; }

void serverType::setFree() { status = "free"; }

void serverType::setTransactionTime(int t) { transactionTime = t; }

void serverType::setTransactionTime() { int time;

time = currentCustomer.getTransactionTime();

transactionTime = time; }

void serverType::decreaseTransactionTime() { transactionTime--; }

int serverType::getRemainingTransactionTime() const { return transactionTime; }

void serverType::setCurrentCustomer(customerType cCustomer) { currentCustomer = cCustomer; }

int serverType::getCurrentCustomerNumber() const { return currentCustomer.getCustomerNumber(); }

int serverType::getCurrentCustomerArrivalTime() const { return currentCustomer.getArrivalTime(); }

int serverType::getCurrentCustomerWaitingTime() const { return currentCustomer.getWaitingTime(); }

int serverType::getCurrentCustomerTransactionTime() const { return currentCustomer.getTransactionTime(); }

//************** serverListType ***********

serverListType::serverListType(int num) { numOfServers = num; servers = new serverType[num]; }

serverListType::~serverListType() { delete[] servers; }

int serverListType::getFreeServerID() const { int serverID = -1;

int i;

for (i = 0; i

return serverID; }

int serverListType::getNumberOfBusyServers() const { int busyServers = 0;

int i;

for (i = 0; i

return busyServers; }

void serverListType::setServerBusy(int serverID, customerType cCustomer, int tTime) { servers[serverID].setBusy(); servers[serverID].setTransactionTime(tTime); servers[serverID].setCurrentCustomer(cCustomer); }

void serverListType::setServerBusy(int serverID, customerType cCustomer) { int time;

time = cCustomer.getTransactionTime();

servers[serverID].setBusy(); servers[serverID].setTransactionTime(time); servers[serverID].setCurrentCustomer(cCustomer); }

void serverListType::updateServers(ostream& outFile) { int i;

for (i = 0; i

if (servers[i].getRemainingTransactionTime() == 0) { outFile

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 of the function template to overload this operator. 1. 2. Repeat Exercise 1 for the class linkedStackType. 3. a Add the following operation to the class stackType: void reverseStack (stackType &otherStack); This operation copies the elements of a stack in reverse order onto another stack. Consider the following statements: stackType stackl; stackType stack2; The statement stackl.reverseStack (stack2) copies the elements of stackl onto stack2 in reverse order. That is, the top element of stackl is the bottom element of stack2, and so on. The old contents of stack2 are destroyed and stackl is unchanged. b. Write the definition of the function template to implement the opera tion reverseStack. 4. Repeat Exercises 3a and 3b for the class linkedStackType. 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 of the function template to overload this operator. 1. 2. Repeat Exercise 1 for the class linkedStackType. 3. a Add the following operation to the class stackType: void reverseStack (stackType &otherStack); This operation copies the elements of a stack in reverse order onto another stack. Consider the following statements: stackType stackl; stackType stack2; The statement stackl.reverseStack (stack2) copies the elements of stackl onto stack2 in reverse order. That is, the top element of stackl is the bottom element of stack2, and so on. The old contents of stack2 are destroyed and stackl is unchanged. b. Write the definition of the function template to implement the opera tion reverseStack. 4. Repeat Exercises 3a and 3b for the class linkedStackType

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!