Question: 2. Specification Input specification The only user input to this program is a file name. Your program should open and read the specified file, which
2. Specification
Input specification
The only user input to this program is a file name. Your program should open and read the specified file, which always contains 10 input lines representing 10 customers. Each line contains the following information:
- A single character representing the lane: 'N' for normal, 'E' for express
- An unsigned integer representing the customer's arrival time
- File contents are sorted in order of arrival time
- An unsigned integer representing the customer's service time (how long it takes to serve a customer once they reach the front of the line)
So, for example, the first three lines of the input filefile1.txtrepresent two express lane customers and one normal lane customer arriving at times 2, 7, and 8, with service times 3, 1, and 4:
E 2 3 E 7 1 N 8 4
Output specification and grading rubric
Your program is essentially broken into two parts: reading the file and filling the queues, then emptying the queues to simulate serving customers. The test cases use two separate input files,file1.txtandfile2.txt. I've collected the full program output based on each input file in a pair of files,file1_output.txtandfile2_output.txt, which are provided below as downloadable files. Remember, your program doesn't print to a file--it prints to the screen!
Reading the file (20 points)
To show that your program reads the file correctly, the program must do the following:
- Prompt for and read the file name
- Print a line indicating you've started reading the file
- As each line of the file is read, print a message listing:
- The customer number
- The lane to which they're going
- The customer's arrival and service times
- Print a line indicating you're done reading the file
For example, here's the output my program generates while readingfile1.txt. The file name on the first line is user input; the program prints everything else:
Enter input file name: file1.txt Reading file1.txt ... Customer 1 to normal lane (A = 1, S = 5) Customer 2 to express lane (A = 2, S = 3) Customer 3 to express lane (A = 3, S = 2) Customer 4 to normal lane (A = 5, S = 7) Customer 5 to express lane (A = 6, S = 1) Customer 6 to express lane (A = 8, S = 2) Customer 7 to normal lane (A = 9, S = 4) Customer 8 to express lane (A = 11, S = 3) Customer 9 to normal lane (A = 12, S = 3) Customer 10 to express lane (A = 13, S = 2) Done reading file1.txt
Serving customers (80 points)
This part of the program is a loop that runs as long as at least one of four conditions is satisfied:
- You're serving a customer from the express lane
- You're serving a customer from the normal lane
- There are customers waiting in the express lane
- There are customers waiting in the normal lane
While those conditions control when the loop ends, your loop also needs a variable representing time, which starts at 1 and increments every loop iteration. Each time through the loop, your program may take at least one of four actions (although it may do none of these), each of which should be indicated by a line of output:
- Start serving a new customer from the express lane
- Start serving a new customer from the normal lane
- Finish serving a customer from the express lane
- Finish serving a customer from the normal lane
To start serving a new customer from either lane, three conditions have to be satisfied:
- You're not serving anyone from that lane
- There's at least one customer waiting in that lane
- The current time is greater than or equal to the arrival time of the customer at the front of the queue for that lane
Each customer's service time tells you how many cycles you need to spend serving them. Once that time is up, you can treat the current customer as done and you'll be available to serve a new customer. Note that:
- You'll need to track how long the current customer has left
- A customer with a service time of 1 will start and finish in the same loop iteration
- If you finish serving a customer in one iteration, the next customer starts in the following iteration.
The output my program generates while serving the customers represented infile1.txtis below:
T = 1: Serving customer 1 in normal lane T = 2: Serving customer 2 in express lane T = 4: Done serving customer 2 in express lane T = 5: Serving customer 3 in express lane T = 5: Done serving customer 1 in normal lane T = 6: Serving customer 4 in normal lane T = 6: Done serving customer 3 in express lane T = 7: Serving customer 5 in express lane T = 7: Done serving customer 5 in express lane T = 8: Serving customer 6 in express lane T = 9: Done serving customer 6 in express lane T = 11: Serving customer 8 in express lane T = 12: Done serving customer 4 in normal lane T = 13: Serving customer 7 in normal lane T = 13: Done serving customer 8 in express lane T = 14: Serving customer 10 in express lane T = 15: Done serving customer 10 in express lane T = 16: Done serving customer 7 in normal lane T = 17: Serving customer 9 in normal lane T = 19: Done serving customer 9 in normal lane Done serving all customers
3. Hints
The template files I've provided contain some hints, particularly the main program. Additional hints are here:
Items in queue
You can use the array-based queue template we designed in class (or, if you're feeling unnecessarily ambitious, redesign it), which is provided below. You do need some way of representing the data stored in each queue entry, though. I used theQItemclass that's also provided with the assignment, which stores each customer's number, arrival time, and service time. I'll let you decide what functions are necessary for your solution; feel free to redefine that class or completely ignore it.
File handling hints
We talked briefly about file I/O in Lecture 3, coveringifstreamobjects for input and noting that you can use anifstreamthe same way you usecin.
While there's aneof()function forifstreamobjects, you don't necessarily need it. A statement using the input extraction operator>>returnsfalseif it fails to read anything, which you can use to your advantage. Ifmyfile.txtcontains a list of integers, the code below will read all of those values:
int main() { ifstream in1; // Input file stream int inval; // Input value in1.open("myfile.txt"); while (in1 >> inval) { // do something to process input } in1.close(); return 0; }
Customer service hints
Did you carefully read the description of the customer service process, particularly the short lists that describe:
- The conditions under which you continue to handle customers?
- The actions this loop might take each time you go through?
- The conditions under which you start handling a new customer?
- The conditions under which you finish handling a new customer?
If not, you might want to go back and take some detailed notes on that section. Those points come directly from my solution so, while you're absolutely welcome to solve this problem any way you want, do know that the description's based on a working program.
prog4main.cpp
/******** ADD YOUR OWN HEADER COMMENT *********/
/***************************************************************
* I BELIEVE YOU'LL NEED TO INCLUDE ALL OF THESE HEADER FILES; *
*FEEL FREE TO MODIFY THIS LIST AS NEEDED*
***************************************************************/
#include "Queue.h"
#include "QItem.h"
#include
#include
#include
using namespace std;
int main() {
/****************************************************************
* VARIABLE DECLARATIONS SHOULD INCLUDE (BUT NOT BE LIMITED TO):
*- VARIABLES TO READ FROM THE FILE (INPUT STREAM, STRING FOR NAME,
*ETC.)
*- TWO QUEUES REPRESENTING THE TWO LANES YOU'RE MODELING
*- AT LEAST ONE TEMPORARY QItem VARIABLE TO STORE USER INPUT
*BEFORE ADDING IT TO EACH "LANE"
*(YOU MAY WANT A SECOND TEMP VARIABLE SO YOU CAN USE THE
*TEMP VARIABLES TO REPRESENT THE CUSTOMERS YOU'RE
*SERVING, AND HAVE THE QUEUES JUST HOLD WAITING CUSTOMERS)
*- A VARIABLE TO COUNT TIME
*- SOMETHING TO TRACK (1) WHETHER YOU'RE SERVING A CUSTOMER FROM
*EACH QUEUE AND (2) HOW LONG THAT CUSTOMER HAS LEFT
* ***************************************************************/
/*****************************************************************
* START WITH FILE HANDLING AS DESCRIBED IN THE SPEC
*(THE TEST CASES ARE DESIGNED SO YOU CAN TEST THE FILE HANDLING
*WITHOUT IMPLEMENTING ANYTHING ELSE IN THE REST OF THE PROGRAM!)
* ***************************************************************/
/*****************************************************************
* THEN, ADD THE LOOP THAT TRACKS TIME AND HANDLES CUSTOMER SERVICE
* ***************************************************************/
return 0;
}
Queue.h
#include
using std::cout;
template
class Queue {
public:
Queue(unsigned maxSize = 1024);
~Queue();
bool empty() const;
void enqueue(const T &val);
void dequeue();
T getFront();
private:
T* list;
int front, back;
unsigned cap;
};
// Constructor
template
Queue
{
list = new T[maxSize];
}
// Destructor
template
Queue
{
delete [] list;
}
// True if list is empty
template
bool Queue
return (front == back); // Returns true if front == back
//false otherwise
}
// Add new value to back of queue
template
void Queue
if ((back + 1) % cap == front) // Queue is full
cout
else { // At least one empty spot in queue
list[back] = val;
back = (back + 1) % cap;
}
}
// Remove element at front of Queue
template
void Queue
if (!empty()) // Can't remove from empty queue
front = (front + 1) % cap;
}
// Retrieve value of element at top of Queue
template
T Queue
if (!empty())
return list[front];
// Empty queue--return garbage data
else {
cout
return list[cap - 1];
}
}
Qitem.h
#ifndef QITEM_H
#define QITEM_H
class QItem {
public:
/**********************************************************************
* MODIFY THIS CLASS AS YOU SEE FIT--MY SOLUTION USED THE DATA MEMBERS
*SHOWN BELOW AND HAD FUNCTIONS THAT ALLOWED ME TO MODIFY AND READ
*THEM AS NEEDED, BUT YOU MAY COME UP WITH SOMETHING DIFFERENT
* ********************************************************************/
private:
unsigned cNum; // Customer number
unsigned arrTime; // Arrival time
unsigned svcTime; // Time required to service customer
};
#endif
Qitem.cpp
/*************************************************
* FILE TO HOLD DEFINITIONS FOR QItem FUNCTIONS
* ***********************************************/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
