添加queue

This commit is contained in:
朱毅骏 2021-08-23 10:52:45 +08:00
parent 403da0d718
commit 37742dfa76
3 changed files with 215 additions and 0 deletions

81
base/queue/bank.cpp Normal file
View File

@ -0,0 +1,81 @@
//
// Created by zhuyijun on 2021/8/23.
//
#include <iostream>
#include "queue.h"
#include <cstdlib>
#include <ctime>
const int MIN_PER_HR = 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 << "Enter maximum size of queue: ";
int qs;
cin >> qs;
Queue line(qs);
cout << "Enter the number of simulation hours: ";
int hours;
cin >> hours;
long cyclelimt = MIN_PER_HR * hours;
cout << "Enter the average number of customer per hour: ";
double perhour;
cin >> perhour;
double min_per_cust;
min_per_cust = MIN_PER_HR;
Item temp;
long turnaways = 0;
long customers = 0;
long served = 0;
long sum_line = 0;
int wait_time = 0;
long line_wait = 0;
for (int cycle = 0; cycle < cyclelimt; ++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();
served++;
}
if (wait_time > 0) {
wait_time--;
}
sum_line += line.queueCount();
}
if (customers > 0) {
cout << "customers accepted: " << customers << endl;
cout << "customers served: " << served << endl;
cout << " turnaways: " << turnaways << endl;
cout.precision(2);
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << (double) sum_line / cyclelimt << endl;
cout << "average wait_time: " << (double) line_wait / served << endl;
} else {
cout << "No customers!\n";
}
return 0;
}
bool newCustomer(double x) {
return (std::rand() * x / RAND_MAX < 1);
}

68
base/queue/queue.cpp Normal file
View File

@ -0,0 +1,68 @@
//
// Created by zhuyijun on 2021/8/23.
//
#include <cstdlib>
#include "queue.h"
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;
}
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;
}
void Customer::set(long when) {
processTime = std::rand() % 3 + 1;
arrive = when;
}

66
base/queue/queue.h Normal file
View File

@ -0,0 +1,66 @@
//
// Created by zhuyijun on 2021/8/23.
//
#ifndef BASE_QUEUE_H
#define BASE_QUEUE_H
class Customer {
private:
long arrive;
int processTime;
public:
Customer() : arrive(0), processTime(0) {}
void set(long when);
long when() const {
return arrive;
}
int ptime() const {
return processTime;
}
};
typedef Customer Item;
//template<Class T>
class Queue {
private:
struct Node {
Item item;
struct Node *next;
};
enum {
Q_SIZE = 0
};
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);
};
#endif //BASE_QUEUE_H