头文件
1 #pragma once 2 #ifndef QUEUE_H_ 3 #define QUEUE_H_ 4 class Customer { 5 private: 6 long arrive; 7 int processtime; 8 public: 9 Customer() { arrive = processtime = 0; } 10 void set(long when); 11 long when() const { return arrive; } 12 int ptime() const { return processtime; } 13 }; 14 typedef Customer Item; 15 class Queue 16 { 17 private: 18 struct Node 19 { 20 Item item; 21 struct Node* next; 22 }; 23 enum { Q_SIZE = 10 }; 24 Node* front; 25 Node* rear; 26 int items; 27 const int qsize; 28 Queue(const Queue& q):qsize(0){} 29 Queue& operator=(const Queue& q) { return *this; } 30 public: 31 Queue(int qs = Q_SIZE); 32 ~Queue(); 33 bool isempty()const; 34 bool isfull()const; 35 int queuecount()const; 36 bool enqueue(const Item& item); 37 bool dequeue(Item& item); 38 }; 39 #endif
类方法实现文件
1 #include "pch.h" 2 #include "queue.h" 3 #include <cstdlib> 4 Queue::Queue(int qs) :qsize(qs) { 5 front = rear = nullptr; 6 items = 0; 7 } 8 Queue::~Queue() { 9 Node* temp; 10 while (front!=nullptr) 11 { 12 temp = front; 13 front = front->next; 14 delete temp; 15 } 16 } 17 bool Queue::isempty()const { 18 return items == 0; 19 } 20 bool Queue::isfull()const { 21 return items == qsize; 22 } 23 int Queue::queuecount()const { 24 return items; 25 } 26 bool Queue::enqueue(const Item& item) { 27 if (isfull()) 28 return false; 29 Node* add = new Node; 30 add->item = item; 31 add->next = nullptr; 32 items++; 33 if (front == nullptr) 34 front = add; 35 else 36 rear->next = add; 37 rear = add; 38 return true; 39 } 40 bool Queue::dequeue(Item& item) { 41 if (front == nullptr) 42 return false; 43 item = front->item; 44 items--; 45 Node* temp; 46 temp = front; 47 front = front->next; 48 delete temp; 49 if (items == 0) 50 rear = nullptr; 51 return true; 52 } 53 void Customer::set(long when) { 54 processtime = std::rand() % 3 + 1; 55 arrive = when; 56 }
主函数调用类
1 #include "pch.h" 2 #include <iostream> 3 #include <string> 4 #include <cstring> 5 #include <cstdlib> 6 #include <ctime> 7 #include "queue.h" 8 using namespace std; 9 const int MIN_PER_HR = 60; 10 bool newcustomer(double x); 11 int main(void) { 12 srand(time(0)); 13 cout << "Case Study: Bank of Heather Automatic Teller "; 14 cout << "Enter maximum size of queue: "; 15 int qs; 16 cin >> qs; 17 Queue line(qs); 18 cout << "Enter the number of simulation hours: "; 19 int hours; 20 cin >> hours; 21 long cyclelimit = MIN_PER_HR * hours; 22 cout << "Enter the average number of customers per hour: "; 23 double perhour; 24 cin >> perhour; 25 double min_per_cust; 26 min_per_cust = MIN_PER_HR / perhour; 27 Item temp; 28 long turnaways = 0; 29 long customers = 0; 30 long served = 0; 31 long sum_line = 0; 32 int wait_time = 0; 33 long line_wait = 0; 34 for (int cycle = 0; cycle < cyclelimit; cycle++) { 35 if (newcustomer(min_per_cust)) { 36 if (line.isfull()) 37 turnaways++; 38 else { 39 customers++; 40 temp.set(cycle); 41 line.enqueue(temp); 42 } 43 } 44 if (wait_time <= 0 && !line.isempty()) { 45 line.dequeue(temp); 46 wait_time = temp.ptime(); 47 line_wait += cycle - temp.when(); 48 served++; 49 } 50 if (wait_time > 0) 51 wait_time--; 52 sum_line += line.queuecount(); 53 } 54 if (customers > 0) { 55 cout << "customer accepted: " << customers << endl; 56 cout << " customers served: " << served << endl; 57 cout << " turnaways: " << turnaways << endl; 58 cout << "average queue size: "; 59 cout.precision(2); 60 cout.setf(ios_base::fixed, ios_base::floatfield); 61 cout << (double)sum_line / cyclelimit << endl; 62 cout << " average wait time: " << (double)line_wait / served << " minutes "; 63 } 64 else 65 cout << "No customers! "; 66 cout << "Done! "; 67 return 0; 68 } 69 bool newcustomer(double x) { 70 return rand()*x / RAND_MAX < 1; 71 }
IDE:VS2017