I REALLY HOPE SOMEONE CAN HELP ME ON THIS!!!! I have only today left to complete this project; and I'm lacking in some key areas to say the least. Here's the assignment:
"Implement an event driven simulation of a bank. (Just customers in line - and 1 teller). A queue of arrival events will represent the line of customers in the bank. Maintain the arrival events and departure events in an ADT event list, (provided for us by instructor- as we've been using the same one all semester), sorted by the time of the event. Use a pointer based implementation for the event ADT list. The input file is a list of (in my code-a 2-d array - which feeds a list called List li), arrival and transaction (duration) times. Each line of the file contains the arrival time and required transaction time for a customer. The arrival times are ordered by increasing time. Program must count customers and keep track of their cum waiting time. THese stats are sufficient to compute the average waiting time after last event has been processed. Display a trace of the events executed and a summary of the computed stats (total number of arrivals and average time spent waiting in line).
This is the basic idea:
A customers' input is grouped as an arrival time x, with corresponding duration y. So, the following data represents groups of x,y (or arrivals and their corresponding durations)::{1,5,2,5,6,7,4,5}
. and so on. The list li will only hold 2 events at any given time; so only a departure and and arrival or either may be present. As each time data is accessed, arrivals are added to the queue or removed if a departure is processed. Departure times are derived from adding the first arrival time to it's duration; hence 1 + 5 = depart of 6. Departure cannot be processed until time 6 has either occurred or 'been passed.' So; if(departure_time is < arrival_time) then add another arrival until if(departure_time is => arrival_time) then remove the appropriate arrival/duratiion from the queue. Now; a new departure must be computed; so the previous dep of 6 must be added to the very next arrival's duration which is in queue ; so using the above sample data; the new departure would be 11.
The basic ADT's are working in my code; the pointer queue and List. Rick'sEvent.cpp is the meat and potatoes. I am currently only getting output of a 6, 5 and a 1. Which is the first correct data; however; I can no longer figure out how to keep my list data; since it must be removed (except that now I created a new list called li_2 with data to be inserted and possibly retrieved somehow) This is bigger than me and I desperately need to finish this.
I am including all of my files; starting with my implementation first:
// RicksEventProj.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Queue.h"
const int MAXINDEX = 10;
int tA[MAXINDEX][2];
int nextIn = 0;
int DepTime = 0;
void InitTa()
{
tA[0][0] = 1;
tA[0][1] = 5;
tA[1][0] = 2;
tA[1][1] = 5;
tA[2][0] = 4;
tA[2][1] = 5;
tA[3][0] = 20;
tA[3][1] = 5;
tA[4][0] = 22;
tA[4][1] = 5;
tA[5][0] = 24;
tA[5][1] = 5;
tA[6][0] = 26;
tA[6][1] = 5;
tA[7][0] = 28;
tA[7][1] = 5;
tA[8][0] = 30;
tA[8][0] = 5;
tA[9][0] = 88;
tA[9][1] = 3;
}
Event getNextIn()
{
Event e;
e.time = tA[nextIn][0];
e.duration = tA[nextIn][1];
e.eType = 'A';
nextIn++;
return e;
}
int _tmain(int argc, _TCHAR* argv[])
{
InitTa();
queue q;
List li;
List li_2;
Event e;
e = getNextIn();
bool s;
li.insert(1, e, s);
li_2.insert(1,e, s);
int cumTime = 0;
while(!li.isEmpty())
{
if(li.getLength() >= 2)
{
Event e1;
li.retrieve(1, e1, s);
cumTime = e1.time;
q.enqueue(e1);
//Event e2;
//li.retrieve(2, e2, s);
//if(e1.time < e2.time)
//{
//li.remove(1, s);
//}
//else
//{
// cumTime = e2.time;
// q.enqueue(e2);
// li.remove(2, s);
//}
}
else
{
Event e1;
li.retrieve(1, e1, s);
if(e1.eType == 'A')
{
Event eD;
eD.time = e1.time + e1.duration + cumTime;
eD.duration = -1;
eD.eType = 'D';
// li.remove(1,s);
li.insert(2, eD, s);
DepTime = eD.time;
q.displaylist();
li.display();
//system("pause");
}
Event eD;
li.retrieve(1,eD,s);
li.retrieve(1,e1,s);
if(DepTime <= e1.time)
{
//li.retrieve(1,e1,s);
DepTime += e1.duration;
//d = DepTemp;
li.insert(2,eD,s);
cumTime = DepTime - e1.time;
li.remove(1,s);
//q.displaylist();
}
else
{
Event eA = getNextIn();
li.insert(2, eA, s);
}
}
}//while(!li.isEmpty())
return 0;
}
ListA.h:
// *********************************************************
// Header file ListA.h for the ADT list
// Array-based implementation
// 2004 0119 sbw added #ifnedef and namespace...
// *********************************************************
// Must define ListItemType and MAX_LIST before compilation
#ifndef LISTA_H
#define LISTA_H
const int MAX_LIST = 10; //MODIFY!!!
#include <iostream>
#include <string>
using namespace std;
struct Event
{
int time;
int duration;
char eType;
};
typedef Event ListItemType; //MODIFY!!!
class List
{
public:
List(); // default constructor
// destructor is supplied by compiler
// list operations:
bool isEmpty() const;
// Determines whether a list is empty.
// Precondition: None.
// Postcondition: Returns true if the list is empty;
// otherwise returns false.
int getLength() const;
// Determines the length of a list.
// Precondition: None.
// Postcondition: Returns the number of items
// that are currently in the list.
void insert(int index, ListItemType newItem,
bool& success);
// Inserts an item into the list at position index.
// Precondition: index indicates the position at which
// the item should be inserted in the list.
// Postcondition: If insertion is successful, newItem is
// at position index in the list, and other items are
// renumbered accordingly, and success is true;
// otherwise success is false.
// Note: Insertion will not be successful if
// index < 1 or index > getLength()+1.
void remove(int index, bool& success);
// Deletes an item from the list at a given position.
// Precondition: index indicates where the deletion
// should occur.
// Postcondition: If 1 <= index <= getLength(),
// the item at position index in the list is
// deleted, other items are renumbered accordingly,
// and success is true; otherwise success is false.
void retrieve(int index, ListItemType& dataItem,
bool& success) const;
// Retrieves a list item by position.
// Precondition: index is the number of the item to
// be retrieved.
// Postcondition: If 1 <= index <= getLength(),
// dataItem is the value of the desired item and
// success is true; otherwise success is false.
void display();
private:
ListItemType items[MAX_LIST]; // array of list items
int size; // number of items in list
int translate(int index) const;
// Converts the position of an item in a list to the
// correct index within its array representation.
}; // end List class
#endif
// End of header file.
ListA.cpp:
// *********************************************************
// Implementation file ListA.cpp for the ADT list
// Array-based implementation //Carrano 3rd
// *********************************************************
#include "ListA.h" //header file
#include "stdafx.h"
List::List() : size(0)
{
} // end default constructor
bool List::isEmpty() const
{
return bool(size == 0);
} // end isEmpty
int List::getLength() const
{
return size;
} // end getLength
void List::insert(int index, ListItemType newItem,
bool& success)
{
success = bool( (index >= 1) &&
(index <= size+1) &&
(size < MAX_LIST) );
if (success)
{ // make room for new item by shifting all items at
// positions >= index toward the end of the
// list (no shift if index == size+1)
for (int pos = size; pos >= index; --pos)
items[translate(pos+1)] = items[translate(pos)];
// insert new item
items[translate(index)] = newItem;
++size; // increase the size of the list by one
} // end if
} // end insert
void List::remove(int index, bool& success)
{
success = bool( (index >= 1) && (index <= size) );
if (success)
{ // delete item by shifting all items at positions >
// index toward the beginning of the list
// (no shift if index == size)
for (int fromPosition = index+1;
fromPosition <= size; ++fromPosition)
items[translate(fromPosition-1)] =
items[translate(fromPosition)];
--size; // decrease the size of the list by one
} // end if
} // end remove
void List::retrieve(int index, ListItemType& dataItem,
bool& success) const
{
success = bool( (index >= 1) &&
(index <= size) );
if (success)
dataItem = items[translate(index)];
} // end retrieve
int List::translate(int index) const
{
return index-1;
} // end translate
void List::display()
{
ListItemType dataItem;
bool s;
cout << " --- Event List contains " << getLength() << endl;
for(int i=1; i<=getLength(); i++)
{
retrieve(i, dataItem, s);
if(dataItem.eType = 'A')
{
cout << dataItem.duration << dataItem.time << endl;
}
else
{
cout << dataItem.eType << endl;
}
}
// cout << "---------end of list______" << endl;
}
queue:
#ifndef STRUCTS
#define STRUCTS
#include <iostream>
#include "Lista.h"
using namespace std;
class queue
{
public:
queue(); //constructor
~queue() {};
bool isempty(); //returns true is list is empty
void enqueue(ListItemType ch); //adds ch to end of list
ListItemType dequeue(); //removes from beginning of list
void displaylist();
private:
struct elt //enumerated list type - a node
{
ListItemType data;
elt* next;
};
elt *head, *tail;
};
#endif
queue.cpp:
#include<iostream>
#include "Lista.h"
#include "stdafx.h"
#include "queue.h"
//using namespace std;
queue::queue()
{
head = NULL;
tail = NULL;
}
bool queue::isempty()
{
return (head == NULL);
}
void queue::enqueue(ListItemType ch)
{
elt* nu = new elt;
nu->data = ch;
nu->next = NULL;
if(head==NULL)
{
head = nu;
tail=nu;
}
else
{
tail->next = nu;
tail = nu;
}
}
ListItemType queue::dequeue()
{
ListItemType temp;
elt* cur = new elt;
if (head==NULL)
{
cout<<"List is empty" <<endl;
exit (1);
}
else
{
temp = head->data;
cur=head;
head=cur->next;
delete cur;
if (head==NULL)
{
tail=NULL;
}
}
return temp;
}
void queue::displaylist()
{
ListItemType qdata;
elt* cur = new elt;
if(isempty())
{
cout<<"[-------]";
}
cur = head;
while((!isempty()) && (cur != NULL))
{
qdata = cur->data;
cout<<"[" <<qdata.eType <<", " <<qdata.time <<", " <<qdata.duration <<"]" <<" ";
cur = cur->next;
}
//delete nu;
cout<<"\t\t\t";
code:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
#endif
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
This must be done utilizing the array. Additional ADT's are o.k.
Thanks for any and all help as always
Rick