C++: Simulating a 3 server, 1 queue system

Discussion in 'Software' started by someguy85, Mar 2, 2010.

Thread Status:
Not open for further replies.
  1. someguy85

    someguy85 Private E-2

    Hello everybody, I'm working on my final assignment for this term and I'm at a complete loss. I am to simulate a 1 queue, 3 server system for serving customers. The assignment reads:

    "Write a program that determines how long it takes to serve all arriving customers using one queue and 3 servers. Use the customerList.txt file to fill the queue. Add the customer to the queue at the appropriate arrival time. Use 3 servers to process the one queue based on the customer's given arrival time and the length of time needed to serve that customer. Report the time that is required to fully process all the customers in the file. "

    My problem is that I don't have the slightest clue of what the server header file should look like. I'm assuming it should be a templated class since "Queue" and "Node" are templated? Should one of the private data members be "Node" or "Customer"? I'm starting to think that maybe "Customer" should be a private data member in Server.h and then in the driver I will create three Server variables? It would be great if someone could lend some help. I will attach the customerList.txt file.

    Node.h
    Code:
    #ifndef NODE_H
    #define NODE_H
    
    //**********************************************************************************
    
    template <typename TYPE>
    struct Node
    {
    	TYPE data;
    	Node<TYPE> * prev;
    	Node<TYPE> * next;						
    	Node();										
    	Node ( TYPE d, Node<TYPE> * n = NULL, Node<TYPE> * p = NULL);	
    };
    
    //**********************************************************************************
    
    template < typename TYPE >
    Node < TYPE > :: Node()
    {
    	data = 0;
    	prev = NULL;
    	next = NULL;
    };
    
    //**********************************************************************************
    
    template < typename TYPE >
    Node < TYPE > :: Node ( TYPE d, Node<TYPE> * n, Node<TYPE> * p)
    {
    	data = d;
    	next = n;
    	prev = p;
    };
    
    #endif
    

    Queue.h
    Code:
    #ifndef QUEUE_H
    #define QUEUE_H
    #include "Node.h"
    
    //**********************************************************************************
    
    template <typename TYPE>
    class Queue
    {
    private:
    	Node <TYPE> * front;
    	Node <TYPE> * rear;
    	int count;
    
    public:
    	Queue();
    	~Queue();
    	bool enqueue(const TYPE & dataIn);
    	bool dequeue(TYPE & dataOut);
    	bool viewFront(TYPE & dataOut) const;
    	bool viewRear(TYPE & dataOut) const;
    	bool isEmpty() const;
    	bool isFull()const;
    	int getCount() const;
    };
    
    //**********************************************************************************
    
    template <typename TYPE>
    Queue <TYPE> :: Queue()
    {
    	front = NULL;
    	rear = NULL;
    	count = 0;
    }
    
    //**********************************************************************************
    
    template <typename TYPE>
    Queue <TYPE> :: ~Queue()
    {
    	Node < TYPE > * pTemp = front;
    	Node < TYPE > *ptr2;
    	
    
    	while ( pTemp )
    	{
    		ptr2 = pTemp->next;
    		delete pTemp;
    		pTemp = ptr2;
    	}
    
    }
    
    //**********************************************************************************
    
    template <typename TYPE> 
    bool Queue <TYPE> :: enqueue(const TYPE & dataIn)
    {
    	bool success = false;
    	Node<TYPE> * pNew;
    	if(pNew = new Node<TYPE>(dataIn))
    	{
    		if(rear)
    			rear -> next = pNew;
    		
    		else
    			front = pNew;
    	}
    
    		rear = pNew;
    		count++;
    		success = true;
    
    		return success;
    }
    
    //**********************************************************************************
    
    template <typename TYPE>
    bool Queue <TYPE> :: dequeue(TYPE & dataOut)
    {
    	bool success = false;
    	Node<TYPE> * pTemp = front;
    
    	if(front)
    	{
    		dataOut = front -> data;
    		front = front -> next;
    		delete pTemp;
    		count--;
    		success = true;
    
    		if(isEmpty())
    		{
    			rear = NULL;
    		} 
    	}
    	return success;
    }
    
    //**********************************************************************************
    
    template <typename TYPE>
    bool Queue <TYPE> :: viewFront(TYPE & dataOut) const
    {
    	bool success = false;
    	Node<TYPE> * pTemp = front;
    	
    	if(front)
    	{
    		dataOut = front -> data;
    		success = true;
    	}
    	return success;
    }
    
    //**********************************************************************************
    
    template <typename TYPE>
    bool Queue <TYPE> :: viewRear(TYPE & dataOut) const
    {
    	bool success = false;
    	Node<TYPE> * pTemp = front;
    	
    	if(rear)
    	{
    		dataOut = rear -> data;
    		success = true;
    	}
    	return success;
    }
    
    //**********************************************************************************
    
    template < typename TYPE >
    bool Queue <TYPE> :: isEmpty() const
    {
    	bool empty = false;
    
    	if ( front == NULL )
    		empty = true;
    
    	return empty;
    }
    
    //**********************************************************************************
    
    template < typename TYPE >
    bool Queue <TYPE> :: isFull() const
    {
    	bool full = true;
    	Node < TYPE > *ptr = new Node < TYPE >;
    
    	if ( ptr )
    	{
    		full = false;
    		delete ptr;
    	}
    
    	return full;
    }
    
    //**********************************************************************************
    
    template <typename TYPE>
    int Queue <TYPE> :: getCount() const
    {
    	return count;
    }
    
    //**********************************************************************************
    
    
    #endif
    
    Customer.h
    Code:
    #ifndef CUSTOMER_H
    #define CUSTOMER_H
    
    //**********************************************************************************
    
    struct Customer
    {
    int id; 
    int arrivalTime; 
    int serviceTime; // length of time to serve this customer
    
    Customer();
    }; 
    
    //**********************************************************************************
    
    Customer :: Customer()
    {
    	id = 0;
    	arrivalTime = 0;
    	serviceTime = 0;
    }
    
    //**********************************************************************************
    
    #endif
    
     

    Attached Files:

  2. someguy85

    someguy85 Private E-2

    Solved

    I finished the assignment. I'll post the finished code.

    Server.h
    Code:
    #ifndef SERVER_H
    #define SERVER_H
    
    
    //**********************************************************************************
    
    struct Server
    {
    int id;
    int arrivalTime;
    int stopServiceT;
    int remainingTime;
    
    
    Server();
    }; 
    
    //**************************************************************************************
    
    Server::Server()
    {
    	id = 0;
    	arrivalTime = 0;
    	stopServiceT = 0;
    	remainingTime = 0;	
    }
    
    //**************************************************************************************
    #endif
    
    

    sim1q3s.cpp
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    #include "Server.h"
    #include "Customer.h"
    #include "Queue.h"
    
    //**********************************************************************************
    
    void fillQueue(Queue<Customer> &, Customer &, const int &, ifstream &, ofstream &);
    void process(Queue<Customer> &, Customer &, Server [], const int, ofstream &, const int);
    void testServer(Server [], const int, bool &);
    
    //**************************************************************************************
    
    
    int main()
    {
    	Queue<Customer> customerList;
    	const int numServers = 3;
    	Server server[numServers];
    	Customer customerIn;
    	Customer customerOut;
    	int time = 0;
    	bool timeRemaining;
    
    	ifstream customerData;
    	ofstream outputFile;
    
    	customerData.open("customerlist.txt");
    	outputFile.open("trace.txt");
    
    	if(!customerData || !outputFile)
    	{
    		cout << "Error opening file.\n\n";
    		exit(0);
    	}
    
    	customerData >> customerIn.id 
    				 >> customerIn.arrivalTime 
    				 >> customerIn.serviceTime;
    
    	while(!customerData.eof() || timeRemaining)
    	{
    		outputFile << "t = " << time << endl;
    		fillQueue(customerList, customerIn, time, customerData, outputFile);
    		process(customerList, customerOut, server, numServers, outputFile, time);
    		testServer(server, numServers, timeRemaining);
    		outputFile << endl;		
    
    		time++;
    
    		if(!timeRemaining)
    		{
    			outputFile << "t = " << time << endl;
    			process(customerList, customerOut, server, numServers, outputFile, time);
    			testServer(server, numServers, timeRemaining);
    			outputFile << endl;	
    		}
    	}
    
    	customerData.close();
    	outputFile.close();
    
    	return 0;
    }
    
    //**************************************************************************************
    
    
    void fillQueue(Queue<Customer> & custList, Customer & custIn, const int & t, ifstream & custData, ofstream & outFile)
    {
    	while(!custData.eof() && custIn.arrivalTime == t)
    	{
    		custList.enqueue(custIn);
    		outFile << "Cust# " << custIn.id << " added to Q" << endl;
    		custData >> custIn.id >> custIn.arrivalTime >> custIn.serviceTime;
    	}
    	if(custData.eof() && custIn.arrivalTime == t)
    	{
    		custList.enqueue(custIn);
    		outFile << "Cust# " << custIn.id << " added to Q" << endl;
    	}
    }
    
    //**************************************************************************************
    
    void process(Queue<Customer> & custList, Customer & custOut, Server serv[], const int size, ofstream &outFile, const int t)
    {
    	if(custList.getCount() > 0 && serv[0].id == 0)
    	{
    		custList.dequeue(custOut);
    		serv[0].id = custOut.id;
    		serv[0].arrivalTime = custOut.arrivalTime;
    		serv[0].remainingTime = custOut.serviceTime;
    		serv[0].stopServiceT = custOut.serviceTime + t;
    		outFile << "S0 start serving cust# " << serv[0].id << ", service length = " 
    			    << serv[0].remainingTime << " Service ends at t = " 
    			    << serv[0].stopServiceT << endl;
    	}
    	else if(custList.getCount() >= 0 && serv[0].id > 0)
    		outFile << "S0 serving cust# " << serv[0].id << " until t = " 
    				<< serv[0].stopServiceT << endl;
    	else if(serv[0].id == 0)
    		outFile << "S0 not busy" << endl;
    
    	if(custList.getCount() > 0 && serv[1].id == 0)
    	{
    		custList.dequeue(custOut);
    		serv[1].id = custOut.id;
    		serv[1].arrivalTime = custOut.arrivalTime;
    		serv[1].remainingTime = custOut.serviceTime;
    		serv[1].stopServiceT = custOut.serviceTime + t;
    		outFile << "S1 start serving cust# " << serv[1].id << ", service length = " 
    			    << serv[1].remainingTime << " Service ends at t = " 
    			    << serv[1].stopServiceT << endl;
    	}
    	else if(custList.getCount() >= 0 && serv[1].id > 0)
    		outFile << "S1 serving cust# " << serv[1].id << " until t = " 
    		        << serv[1].stopServiceT << endl;
    	else if(serv[1].id == 0)
    		outFile << "S1 not busy" << endl;
    
    	if(custList.getCount() > 0 && serv[2].id == 0)
    	{
    		custList.dequeue(custOut);
    		serv[2].id = custOut.id;
    		serv[2].arrivalTime = custOut.arrivalTime;
    		serv[2].remainingTime = custOut.serviceTime;
    		serv[2].stopServiceT = custOut.serviceTime + t;
    		outFile << "S2 start serving cust# " << serv[2].id << ", service length = " 
    				<< serv[2].remainingTime << " Service ends at t = " 
    				<< serv[2].stopServiceT << endl;
    	}
    	else if(custList.getCount() >= 0 && serv[2].id > 0)
    		outFile << "S2 serving cust# " << serv[2].id << " until t = " 
    				<< serv[2].stopServiceT << endl;
    	else if(serv[2].id == 0) 
    		outFile << "S2 not busy" << endl;
    }
    
    //**************************************************************************************
    
    void testServer(Server serv[], const int size, bool & tRemaining)
    {
    	if(serv[0].remainingTime > 0)
    	{
    		serv[0].remainingTime--;
    		if(serv[0].remainingTime == 0)
    			serv[0].id = 0;
    	}
    	if(serv[1].remainingTime > 0)
    	{
    		serv[1].remainingTime--;
    		if(serv[1].remainingTime == 0)
    			serv[1].id = 0;
    	}
    	if(serv[2].remainingTime > 0)
    	{
    		serv[2].remainingTime--;
    		if(serv[2].remainingTime == 0)
    			serv[2].id = 0;
    	}
    
    	if(serv[0].remainingTime || serv[1].remainingTime || serv[2].remainingTime)
    		tRemaining = true;
    	else
    		tRemaining = false;
    }
    
    //**************************************************************************************
    
     
Thread Status:
Not open for further replies.

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds