小知识:
成员初始化列表:
只有构造函数可以使用这种方法,初始化列表由逗号分隔的初始化列表组成(前面带冒号)它位于参数列表的右括号之后,函数体的左括号之前
初始化语法:Class :: class (int n,int m): men1(n),men2(0),men3(n*m=2){
.......
}
1. 这种格式只能用于构造函数
2.必须用这种格式初始化非静态const 数据成员
3.必须用这种格式初始化引用数据成员
(数据成语员初始化的顺序与他们在类声明中的顺序相同,与初始化器中的排列顺序无关)
接口文件代码:
queue.h--interface for a queue队列接口
#ifndef QUEUE_H
#define QUEUE_H
class Customer//顾客类
{
private :
long arrive;
int processtime;
public:
Customer()
{
arrive=processtime=0;
}
void set(long when);
long when() const
{
return arrive;
}
int ptime() const
{
return processtime;
}
};
typedef Customer Item;//将Customer重名为Item
class Queue//队列类
{
private:
struct Node
{
Item item;
struct Node * next;
};//定义链表节点,使用了嵌套结构和类:struct 结构struct 节点的指针 next
enum {Q_size=10}; //枚举限制队列人数范围
Node * front;//队列前指针
Node * rear;//队列尾指针
int items;
const int qsize;//队列人数--常量
//先定义防止在public里复制
Queue(const Queue &q ):qsize(0) {}; //表示在调用构造函数前初始化qsize为0
//成员初始化列表:初始化列表由逗号分隔的初始化列表组成(前面带冒号)它位于参数列表的右括号之后,函数体的左括号之前
Queue & operator =(const Queue &q)
{
return *this;
};
public:
Queue(int qs=Q_size);
~Queue();
bool isempty()const;
bool isfull()const;
int queuecout() const;
bool enqueue(const Item &item);//尾部添加节点
bool dequeue(Item &item);//头部删除节点
};
#endif
函数实现代码:
#include "queue.h"
#include <cstdlib> //提供随机函数rand()
Queue::Queue(int qs):qsize(qs)//初始化数据,非静态的const 在此行初始化
{
front=rear=NULL;
items=0;
}
Queue::~Queue()//析构函数释放指针内存
{
Node * temp;
while(front !=NULL)
{
temp=front;
front=front->next;
delete temp;
}
}
//判断队列是否为空
bool Queue::isempty()const
{
return items==0;
}
//判断队列是否为满
bool Queue::isfull() const
{
return items==10;
}
//数队列的人数
int Queue::queuecout()const
{
return items;
}
//成员进队
bool Queue::enqueue(const Item &item)
{
if(isfull())//队列满排队失败
{
return false;
}
Node *add=new Node;//创建新的尾部节点
if(add==NULL)
return false;
add->item=item;//add->item 将参数 item复制到节点的数据部分
add->next=NULL;//add-next 设置为空,队列最后一个节点指向下个为NULL节点
items++;
if(front==NULL)
front=add;
else
rear->next=add;//将节点添加到队列尾部
rear=add;//尾部指针指向新节点
return true;
}
//删除队列成员
bool Queue::dequeue(Item &item)
{
if(front==NULL)
return false;
item=front->item;//将首个成员的数据部分通过引用调用函数dequeue(Item &item)
items--;//计数器减1
//将首个成员删除
Node *temp=front;
front =front->next;//用front->next 提供为front
delete temp;
if(items==0)
rear=NULL;
return true;
}
//
void Customer::set(long when)
{
processtime=std::rand()%3+1;//设置处理时间为1-3的随机值
arrive=when;
}
模拟测试代码:
#include "queue.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
const int MIN_PRE_HR=60;//没小时60分钟
bool newcustomer(double x);
int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::ios_base;
std::srand(std::time(0));
cout<<"Case Study: Bank of Heather AUTOMATIC Teller\n";
cout <<"请输入队列最大人数:";
int qs;
cin>>qs;
Queue line(qs);//调用构造函数:Queue::Queue(int qs):qsize(qs)
cout<<"请输入模拟时长(单位小时):";
int hours;
cin>>hours;
long cyclelimit=MIN_PRE_HR*hours;//模拟总时长
cout<<"请输入每小时的顾客人数:";
double perhour;
cin>>perhour;
double min_per_cust=MIN_PRE_HR/perhour;//两个顾客到达的平均时间(60分钟除以每小时的顾客人数)
//新顾客的资料
Item temp;
long turnaways=0;//离开的人数
long customers=0;
long server=0;
long sum_line=0;
int wait_time=0;
long line_wait=0;//队列等待总时长
for (int cycle=0; cycle<cyclelimit; cycle++)
{
if(newcustomer(min_per_cust))
{
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();
server++;
}
if(wait_time>0)
wait_time--;
sum_line+=line.queuecout();
}
if(customers>0)
{
cout<<"customer accpeted: "<<customers<<endl;
cout<<"customer server : "<<server<<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 tiem: "<<(double) line_wait/server<< "minutes\n";
}
else
cout< "NO CUSOTME!\n";
cout<<"DONE \n";
system("pause");
return 0;
}
//x=average time,in minutes,between customers
//return value is true if customer shows uo this minute
bool newcustomer(double x)//判断是否在模拟期间有顾客来
{
return (std::rand()*x/RAND_MAX<1);//因为现实不可能每隔6分钟就一个顾客,所以假设每隔6次就会又一次小于6分钟就来顾客的
}