队列模拟学习 C++

//queue.h  --interface for a queue
//原程序的意思是要建立一个.h文件,但是我采用的线上编译器
//这里把.h文件和主文件放在一起
#ifndef QUEUE_H_
#define QUEUE_H_

#include<iostream>
using namespace std;
//This queue will contain Customer items

class Customer  //注意一般情况下把类的开头第一个字母进行大写用于区分
{
private:    //类的默认模式都是私有
    long arrive;  //arrive time for Customer
    int processtime; //processing time for Customer

public:
    Customer() { arrive = 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
};


#include<cstdlib>

//Queue methods
Queue::Queue(int qs) : qsize(qs)
{
    front = rear = nullptr;//or 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;   //detele former front
    }
}

bool Queue::isempty() const
{
    return items == 0;  //此处的意思是要进行比较 因为我们上面定义isempty()析构函数
    //他是一个布尔函数,只能返回false或者true,也就是返回true
}

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
    //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 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;
}
//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 - 3 
void Customer::set(long when)
{
    processtime = std::rand()%3 + 1;
    arrive =when;
    
}
#endif


具体看代码注释,主要是为了模拟和学习C++当中类和动态内存分配情况下的队列模拟

采用了一系列的相关语句

比如类函数

析构函数

结构

指针等

具体可以看C++Primer Plus  这本书的第384页

//bank.cpp -- using the Queue interface
//compile with queue.cpp
#include<cstdlib>  //for rand() and strand()
#include<iostream>
#include<ctime> //for time()

#define QUEUE_H_
//This queue will contain Customer items

class Customer  //注意一般情况下把类的开头第一个字母进行大写用于区分
{
private:    //类的默认模式都是私有
    long arrive;  //arrive time for Customer
    int processtime; //processing time for Customer

public:
    Customer() { arrive = 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
};

const int MIN_PER_HR = 60;
bool newcustomer(double x);  //is therea a new Customer?
int main()
{
    using std::cin;
    using std::cout;
    using std::endl;
    using std::ios_base;
    //setting thing up
    std::srand(std::time(0));  //random initializing of rand()
    
    cout<<"Case Study: Bank Heather Automatic Teller\n";
    cout<<"Enter maximum size of queue: ";
    int qs;
    cin>>qs;
    Queue line(qs);  //line queue holds up to qs people
    
    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
    
    cout<<"Enter the average number of customer per hour: ";
    double perhour;   //average #of arrival per hour
    cin>>perhour;
    double min_per_cust;  //average  time between arrvals
    min_per_cust = MIN_PER_HR / perhour;
    
    Item temp;   //new customer  data
    long turnaways = 0;   //turned away by full queue
    long customer = 0; //joined the queue
    long served = 0;  //served during the simulation
    long sum_line = 0;  //cumulative line length
    int wait_time = 0;  //time untill autoteller is free
    long line_wait = 0;  //cumulative time in line_wait
    
//running the simulation
    for(int cycle = 0;cycle < cyclelimit; cycle++ )
    {
        if(newcustomer(min_per_cust))//have newcustomer
        {
            if(line.isfull())
                turnaways++;
            else
            {
                customer++;
                temp.set(cycle);  //cycle = time of arrival
                line.enqueue(temp);  //add newcomer to line
                
            }
            
        }
        
        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();
    }
    
//reporting results
    if(customer>0)
    {
        cout<<"customers accepted: "<<customer <<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";
    cout<<"Done!\n";
    return 0;

}
//x=average time,in minutes,between customers
//return value is true if customers shows up this minutes

bool newcustomer(double x)
{
    
    return (std::rand() * x / RAND_MAX <1 );
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值