大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←
问题:
解答:
main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "queue.h"
using namespace std;
const int MIN_PER_HR = 60;
const int MIN_SIM_HOURS = 150;
bool newcustomer(double x);
int main()
{
srand(time(0));
cout << "研究案例:Heather银行自动柜员机\n";
cout << "请输入队列的最大人数: ";
int qs;
cin >> qs;
Queue line(qs);
/*cout << "请输入模拟小时数:";
int hours;
cin >> hours;*/
cout << "模拟时间要大于等于100." << endl;
int hours = MIN_SIM_HOURS;//这里就固定为150;
long cyclelimit = MIN_PER_HR * hours;
//cout << "请输入平均每个小时的顾客数量:";
//double perhour;
//cin >> perhour;
//double min_per_cust;//每个客服到达的平均分钟数.
//min_per_cust = MIN_PER_HR / perhour;//到达一个客户的平均时间
double perhour = 0;
Item temp;
double average_wait = 0;
while (average_wait<1)
{
double min_per_cust;
long turnaways = 0;
long customers = 0;
long served = 0;
long sum_line = 0;
int wait_time = 0;
long line_wait = 0;
perhour++;
if (!line.isempty())line.dequeue(temp);//下次进入前清空队列
for (int cycle = 0; cycle < cyclelimit; cycle++)
{
min_per_cust = MIN_PER_HR / perhour;
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();
served++;
}
if (wait_time > 0)wait_time--;
sum_line += line.queuecount();
}
average_wait = (double)line_wait / served;
if (average_wait < 1)
{
if (customers > 0)
{
cout << "接受顾客数为:" << customers << endl;
cout << "服务顾客数为:" << served << endl;
cout << "turnaways:" << turnaways << endl;
cout << "平均每分钟的队列长度: ";
cout.precision(2);
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << (double)sum_line / cyclelimit << endl;
cout << "每个顾客平均排队等待时间: " << (double)line_wait / served << " 分钟\n";
}
else
cout << "No customers!\n";
cout << "The average " << perhour << " of arrival per hour,and average wait time is" << average_wait << endl<<endl;
}
}
cout << "Done!" <<endl;
return 0;
}
bool newcustomer(double x)
{
return (rand() * x / RAND_MAX < 1);
}
queue.h
#pragma once
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;
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; }
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);
};
queue.cpp
#include "queue.h"
#include <cstdlib>
Queue::Queue(int qs):qsize(qs)
{
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 == qsize;
}
int Queue::queuecount()const
{
return items;
}
bool Queue::enqueue(const Item& item)
{
if (isfull())return false;
Node* node = new Node;
node->item = item;
node->next = NULL;
items++;
if (front == NULL)
{
front = node;
}
else
{
rear->next = node;
}
rear = node;
return true;
}
bool Queue::dequeue(Item& item)
{
if (isempty())return false;
item = front->item;
items--;
Node* temp = front;
front = front->next;
delete temp;
if (items == 0)
{
rear == NULL;
}
return true;
}
void Customer::set(long when)
{
processtime = rand() % 3 + 1;
arrive = when;
}
运行结果:
考查点:
- 数据结构队列
不懂的地方问我吧…
2024年9月8日15:48:39