第十二章编程练习(5)
2016-02-22 21:08
357 查看
QUEUE.h
QUEUE.cpp
main.cpp
测试了3次 分别为100小时,200小时和300小时
![](https://img-blog.csdn.net/20160222211252323?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![](https://img-blog.csdn.net/20160222211258498?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
#pragma once #ifndef QUEUE_H_ #define QUEUE_H_ class Customer { private: int processtime;//顾客所需的服务时间 public: Customer() { processtime = 0; }; void set(); int ptime()const { return processtime; }//只有使用成员函数才能访问类私有数据?友元:我也可以!我和成员函数基情满满的 }; typedef Customer Item; class Queue { private: struct Node { Item item; struct Node * next; };//表链?链表 ! enum { Q_SIZE = 10 }; Node * front;//指向链表头 Node * rear;//指向上一个表 int items;//队列人数 const int qsize;//队列长度 Queue(const Queue &q) :qsize(0) {};//使用初始化列表的方式初始化私有常量 Queue & operator=(const Queue & q) { return *this; };//用了new 深度复制构造函数和赋值运算符少不了的啦 public: Queue(int qs = Q_SIZE); ~Queue();//用了记得还 析构函数 你的忠实朋友! bool isempty()const;//空? bool isfull()const;//满! int queuecount()const;//返回表的数量 bool enqueue(const Item & item);//创建一个表 bool dequeue(Item &item);//删除一个表 }; #endif
QUEUE.cpp
#include "queue.h" #include <iostream> void Customer::set() { processtime = std::rand() % 3+1;//顾客的处理时间设置为1~3分钟 } Queue::Queue(int qs) :qsize(qs) { front = rear = nullptr; items = 0; } Queue::~Queue() { Node *temp; while (front != nullptr) { temp = front; front = front->next; delete temp; } } bool Queue::isempty() const { return items == 0; } bool Queue::isfull() const { return items == qsize; } int Queue::queuecount() const { return items; } bool Queue::enqueue(const Item & item) { if (isfull()) return false; Node * add = new Node; add->item = item; add->next = nullptr; items++; if (front == nullptr) front = add; else rear->next = add; rear = add; return true; } bool Queue::dequeue(Item & item) { if (front == nullptr) return false; item = front->item; items--; Node * temp = front; front = front->next; delete temp; if (items == 0) rear = nullptr; return true; }
main.cpp
#include "queue.h" #include <iostream> #include <cstdlib> #include <ctime> const int MIN_PER_HR = 60; int main() { using namespace std; srand(time(0)); cout << "Enter maximum size of queue: "; int qs; if (!(cin >> qs))//设置队列长度 exit(EXIT_FAILURE); Queue line(qs);//初始化队列 cout << "Please enter the number of simulation hours: "; int ctime; if (!(cin >> ctime))//设置测试时长 exit(EXIT_FAILURE); long hours = ctime*MIN_PER_HR; Item temp; temp.set(); line.enqueue(temp); double cycle = temp.ptime();//初始化第一位顾客 unsigned long people = 1; for (int i = 0; i < hours; i++) { if (!(line.isfull()))//当前一位顾客的服务时间还剩1分钟时就可以入队了 { if (cycle == 1) { temp.set(); line.enqueue(temp); cycle = temp.ptime() + 1;//顾客从入队到离队的时间,别忘了加上等待的时间 //cout << temp.ptime() << endl; ## 看看这随机数坑爹不?ORZ people +=line.queuecount(); } } while (!(line.isempty())) line.dequeue(temp); cycle--; } cout << people << endl;//在设定时间内有多少顾客获得服务 cout << "Has " << people/ctime << " people!" << endl;//平均每小时的顾客数 system("pause"); return 0; }
测试了3次 分别为100小时,200小时和300小时
相关文章推荐
- Python执行系统命令的方法
- Java c++通过des加解密
- Eclipse配置外部Maven
- STL 源代码剖析 算法 stl_algo.h -- merge sort
- JAVA_面向对象_笔记_01
- JAVA_面向对象_笔记_01
- 更改django管理员密码
- OpenJudge百炼习题解答(C++)--题3858:和数
- 【ASP.NET】Cookie机制
- thinkphp_3.1.2介绍和安装
- 程序员面试题精选100题(15)-含有指针成员的类的拷贝[C/C++/C#]
- OpenJudge百炼习题解答(C++)--题4072:判断多个点是否在同一直线
- Spring定时任务的几种实现
- OpenJudge百炼习题解答(C++)--题4074:积水量
- php编译安装
- java的动态代理机制详解
- OpenJudge百炼习题解答(C++)--题4045:与3和5无关的数
- 程序员面试题精选100题(08)-求1+2+...+n[C/C++/C#]
- perl面向对象的概念
- eclipse ant jacoco