C++ Primer Plus第六版编程练习12.6解答
来源:互联网 发布:汕头淘宝村地址 编辑:程序博客网 时间:2024/05/22 12:05
queue.h
// queue.h -- interface for a queue#ifndef QUEUE_H_#define QUEUE_H_// This queue will contain Customer itemsclass Customer{private: long arrive; // arrival time for customer int processtime; // processing time for customerpublic: Customer() : arrive(0), processtime (0){} void set(long when); long when() const { return arrive; } int ptime() const { return processtime; }};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 definitions 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
// queue.cpp -- Queue and Customer methods#include "queue.h"#include <cstdlib> // (or stdlib.h) for rand()// Queue methodsQueue::Queue(int qs) : qsize(qs){ front = rear = NULL; // or nullptr 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 queuebool Queue::enqueue(const Item & item){ if (isfull()) return false; Node * add = new Node; // create node// on failure, new throws std::bad_alloc exception add->item = item; // set node pointers add->next = NULL; // or nullptr; 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 front item into item variable and remove from queuebool 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;}// customer method// when is the time at which the customer arrives// the arrival time is set to when and the processing// time set to a random value in the range 1 - 3void Customer::set(long when){ processtime = std::rand() % 3 + 1; arrive = when; }
bank.cpp
// bank.cpp -- using the Queue interface// compile with queue.cpp#include <iostream>#include <cstdlib> // for rand() and srand()#include <ctime> // for time()#include "queue.h"const int MIN_PER_HR = 60;bool newcustomer(double x); // is there a new customer?int main(){ using std::cin; using std::cout; using std::endl; using std::ios_base;// setting things up std::srand(std::time(0)); // random initializing of rand() cout << "Case Study: Bank of Heather Automatic Teller\n"; cout << "Enter maximum size of queue: "; int qs; cin >> qs; Queue line1(qs); // line queue holds up to qs people Queue line2(qs); cout << "Enter the number of simulation hours: "; int hours; // hours of simulation cin >> hours; // simulation will run 1 cycle per minute long cyclelimit = MIN_PER_HR * hours; // # of cycles Item temp; // new customer data long turnaways; // turned away by full queue long customers; // joined the queue long served; // served during the simulation long sum_line; // cumulative line length long wait_time; // time until autoteller is free long line_wait; // cumulative time in line long cycle; // running time cout << "Enter the average number of customers per hour: "; double perhour; // average # of arrival per hour double min_per_cust; // average time between arrivals while (cin >> perhour) { turnaways = 0; customers = 0; served = 0; sum_line = 0; wait_time = 0; line_wait = 0; min_per_cust = MIN_PER_HR / perhour; // running the simulation for (cycle = 0; cycle < cyclelimit; cycle++) { if (newcustomer(min_per_cust)) // have newcomer { if (line1.isfull() && line2.isfull()) turnaways++; else // line1 or line2 is not full { customers++; temp.set(cycle); // cycle = time of arrival // if line1 has less customers than line2, add new customer to line1, else add to line2 if(line1.queuecount()<=line2.queuecount()) line1.enqueue(temp); else line2.enqueue(temp); } } // process customers in first queue if (wait_time <= 0 && !line1.isempty()) { line1.dequeue (temp); // attend next customer wait_time = temp.ptime(); // for wait_time minutes line_wait += cycle - temp.when(); served++; } // process customers in second queue if (wait_time <= 0 && !line2.isempty()) { line2.dequeue (temp); // attend next customer wait_time = temp.ptime(); // for wait_time minutes line_wait += cycle - temp.when(); served++; } if (wait_time > 0) wait_time--; sum_line += ( line1.queuecount() + line2.queuecount() ); } // reporting results 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 << (double) sum_line / cyclelimit << endl; cout << " average wait time: " << (double) line_wait / served << " minutes\n"; } else cout << "No customers!\n"; // clear queues while (!line1.isempty()) line1.dequeue(temp); while (!line2.isempty()) line2.dequeue(temp); cout << "\nEnter the average number of customers per hour(q to quit): "; } cout << "Done!\n"; cin.get(); return 0;}// x = average time, in minutes, between customers// return value is true if customer shows up this minutebool newcustomer(double x){ return (std::rand() * x / RAND_MAX < 1);}
0 0
- C++ Primer Plus第六版编程练习12.6解答
- C++ Primer Plus第六版编程练习6.7解答
- C++ Primer Plus第六版编程练习6.9解答
- C++ Primer Plus第六版编程练习7.10解答
- C++ Primer Plus第六版编程练习8.2解答
- C++ Primer Plus第六版编程练习8.6解答
- C++ Primer Plus第六版编程练习9.1解答
- C++ Primer Plus第六版编程练习9.4解答
- C++ Primer Plus第六版编程练习10.3解答
- C++ Primer Plus第六版编程练习10.4解答
- C++ Primer Plus第六版编程练习10.5解答
- C++ Primer Plus第六版编程练习10.6解答
- C++ Primer Plus第六版编程练习10.8解答
- C++ Primer Plus第六版编程练习11.1解答
- C++ Primer Plus第六版编程练习11.2解答
- C++ Primer Plus第六版编程练习11.3解答
- C++ Primer Plus第六版编程练习11.4解答
- C++ Primer Plus第六版编程练习11.5解答
- linux常用命令剖析之cat
- 数据结构 - 队列(二)
- scala 1.0 开始写scala的代码
- MATLAB取整
- 凸限制下的凸优化问题(一)
- C++ Primer Plus第六版编程练习12.6解答
- 非最大抑制(NMS)
- Netty服务器部署
- poj 2001 Shortest Prefixes
- Linux 技巧: Bash 测试和比较函数
- Spark下的TopK实现(Java)
- Windows 驱动开发 - 基础1
- PHP中的魔术方法详解
- Ubuntu14.10中安装eclipse后打开无菜单栏解决办法