Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
what the result of average_time have negative
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Robert, post: 3044636"] dear sir: I have read the c++ primer plus 5 book, and do the chapter 12 programming exercises question 5, but the average_wait have negative result. I don't understand why? here is the code, thank you anyone replay me.:) /************************* customer.h *************************/ #ifndef CUSTOMER_H_ #define CUSTOMER_H_ class Customer { private: long arrive; //arrival time for customer int processtime; //process time for customer public: Customer() { arrive = processtime = 0; } void set(long when); long when() const { return arrive; } int ptime() const { return processtime; } }; #endif /************************* customer.cpp *************************/ #include <iostream> #include "customer.h" //when is the time at which the customer arrives //the arrival times is set to when and the processing //time set to a random value in the range 1 - 3 void Customer::set(long when) { processtime = std::rand() % 3 + 1; arrive = when; } /************************* queue.h *************************/ #ifndef QUEUE_H_ #define QUEUE_H_ #include "customer.h" namespace QUEUE { typedef Customer Item; class Queue { private: //class scope definitions //Node is a nested structure definition local to this class struct Node { Item item; struct Node * next;}; enum { Q_SIZE = 10}; //private class members Node * front; //pointer to front of Queue Node * rear; //pointer to rear of Queue int items; //current number of items in Queue const int qsize; //maximum number of items in Queue //preemptive definitiond to prevent public copying Queue(const Queue & q):qsize(0) { } Queue & operator=(const Queue & q) { return *this;} public: Queue(int qs = Q_SIZE); //create queue with a qs limit ~Queue(); bool isempty() const; bool isfull() const; int queuecount() const; bool enqueue(const Item & item); //add item to end bool dequeue(Item & item); //remove item from front }; } #endif /************************* queue.cpp *************************/ #include "queue.h" #include <cstdlib> namespace QUEUE { //Queue methods Queue::Queue(int qs):qsize(qs) { front = rear = NULL; items = 0; } Queue::~Queue() { Node * temp; while (front != NULL) //while queue is not yet empty { temp = front; //save address of front item front = front->next;//reset pointer to next item delete temp; //delete former front } } bool Queue::isempty() const { return items == 0; } bool Queue::isfull() const { return items == qsize; } int Queue::queuecount() const { return items; } //Add item to queue bool Queue::enqueue(const Item & item) { if (isfull()) return false; Node * add = new Node; //create node if (add == NULL) return false; //quit if none available add->item = item; //set node pointers add->next = NULL; items++; if (front == NULL) //if queue is empty, front = add; //place item at front else rear->next = add; //else place at rear rear = add; //have rear point to new node return true; } //Place frony item into item variable and remove fron a queue bool Queue::dequeue(Item &item) { if (front == NULL) return false; item = front->item; //set item to first item in queue items--; Node * temp = front;//save location of first item front = front->next;//reset front to next item delete temp; //delete former first item if ( items == 0) rear = NULL; return true; } } /************************* main.cpp *************************/ #include <iostream> #include <cstdlib> //for rand() and srand() #include <ctime> //for time() #include "queue.h" bool newcustomer(double x); //is there a new customer? void main() { const int MIN_PER_HR = 60; using namespace std; using namespace QUEUE; srand(static_cast<unsigned int>(time(0))); cout << "Case Study: Bank of Healther Automatic Teller\n"; cout << "Enter maximum size of quiue: "; int qs; cin >> qs; Queue line(qs); cout << "Enter the number of simulation hours: "; int hours; cin >> hours; long cyclelimit = MIN_PER_HR * hours; //cout << "Enter the average number of customers per hour: "; double perhour; //cin >> perhour; double min_per_cust; Item temp; long turnaways = 0; long customers = 0; long served = 0; long sum_line = 0; int wait_time = 0; long line_wait = 0; long double average_wait = 0; for (long mpc = 0; mpc < LONG_MAX; mpc++) { average_wait = 0; for (int cycle = 0; cycle < cyclelimit; cycle++) { if (newcustomer(mpc)) { if (line.isfull()) turnaways++; else { customers++; temp.set(cycle); line.enqueue(temp); } } if (wait_time <= 0 && !line.isempty()) { line.dequeue(temp); wait_time = temp.ptime(); line_wait += cycle - temp.when(); served++; } if (wait_time > 0) wait_time--; sum_line += line.queuecount(); } if (served) average_wait = (double)line_wait / served; if (served && average_wait <= 1.00) { perhour = mpc; break; } } if (customers > 0) { cout << "customers accepted: " << customers << endl; cout << " customers served: " << served << endl; cout << " turnaways: " << turnaways << endl; cout << "average queue size: "; cout.precision(2); cout.setf(ios_base::fixed,ios_base::floatfield); cout.setf(ios_base::showpoint); cout << (double) sum_line / cyclelimit << endl; cout << " average wait time: " << (double) line_wait / served << "minutes\n"; cout << "the average number of customers per hour" << perhour << endl; } else cout << "No customers!\n"; } bool newcustomer(double x) { return ( rand() * x / RAND_MAX < 1); } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
what the result of average_time have negative
Top