C++对于ATM机的队列模拟

本人从事在线教育c++十年工作经验现在精心整理了一套从小白到项目实践开发各种学习资料如果你想学想加入我们请关注我在私信回复“编程”就可以领取学习资料!!!想学加裙775356268

通过设置简单的队列模型,应用到ATM机上去模拟一些数据的获取,详细的代码如下:

//description.h

#ifndef _DESCRIPTION_H_

#define _DESCRIPTION_H_

#include

const int MAX_QUEUE = 100;

using namespace std;

class Customer

{

private:

long processtime;

long begin_time;

public:

Customer(){ processtime = 0; begin_time = 0; }

~Customer(){}

void set(long t);

long get_bt(){ return begin_time; }

long get_pt(){ return processtime; }

};

typedef Customer Item;

class Queue

{

private:

struct Node

{

Item item;

Node *next;

};

Node *front, *rear;

int num;

const int Max_num;

Queue(const Queue &q):Max_num(0){}

Queue & operator=(const Queue &q){ return *this; }

public:

Queue(int Size=MAX_QUEUE);

~Queue();

bool isempty() const { return num==0; }

bool isfull()const { return num == Max_num; }

int get_num() const { return num; }

bool pushQ(const Item&);

bool popQ(Item&);

};

#endif //_DESCRIPTION_H_

//define.cpp

/**

*Copyright U+00A9 2018 XXXXX. All Right Reserved.

*Filename:

*Author:XXXXXX

*Date:2018.12.01

*Version:VS 2013

*Description:

**/

#include

//#include

#include"description.h"

using namespace std;

void Customer::set(long t)

{

processtime = rand() % 3 + 1;//随机生成1~3之间的数,代表每个顾客的处理时间

begin_time = t;

}

Queue::Queue(int Size):Max_num(Size)//对于数据成员为引用&或者非静态const的必须利用初始化列表

{

front = rear = nullptr;

num = 0;

}

Queue::~Queue()//将剩余队列中的数据删除,释放内存空间

{

Node *temp;

while (front != nullptr)

{

temp = front;

front = front->next;

delete temp;

}

cout << "The rest object is erased!" << endl;

}

bool Queue::pushQ(const Item &item)

{

if (isfull()) return false;

Node *temp = new Node;

temp->item = item;

temp->next = nullptr;

if (front == nullptr)

front = temp;

else

rear->next = temp;

rear = temp;

++num;

return true;

}

bool Queue::popQ(Item &item)

{

if (isempty()) return false;

Node *temp;

temp = front;

item = front->item;

front = front->next;

delete temp;

--num;

if (num == 0) rear = nullptr;

return true;

}

//main.cpp

/**

*Copyright U+00A9 2018 XXXXXX. All Right Reserved.

*Filename:

*Author:XXXXXXX

*Date:2018.12.01

*Version:VS 2013

*Description:

**/

#include

#include

#include"windows.h"

#include"description.h"

#include

using namespace std;

bool newcustomer(double);

int main()

{

srand(time(0));

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 cyclelimit = 60 * hours;

cout << "Enter the average number of customers per hour: ";

double perhour;

cin >> perhour;

double min_per_cus = 60.0 / perhour;//计算多少分钟来一个办理者

Item temp;

long turnaway = 0;

long customers = 0;

long served = 0;

long sum_line = 0;

int wait_time = 0;

long line_wait = 0;

for (int cycle = 0; cycle < cyclelimit; cycle++)//将总共时间分成若干个1分钟

{

if (newcustomer(min_per_cus))

{

if (line.isfull())

turnaway++;//如果队列满了,此时新顾客将会离去

else

{

customers++;

temp.set(cycle);//将第cycle分钟设置为顾客的初始时间

line.pushQ(temp);

}

}

if (wait_time <= 0 && !line.isempty())//如果队列不为空并且前一个顾客已经办理好wait_time<=0

{

line.popQ(temp);//出队列

wait_time = temp.get_pt();//重新设置新顾客的办理时间

line_wait += cycle - temp.get_bt();//整个队列等待一个人办好业务所经历的时间

++served;//已经办理好的人数加1

}

if (wait_time > 0)

--wait_time;//按每分钟为单位等前一个顾客办理好

sum_line += line.get_num();//所有时刻队列的长度总和

}

if (customers > 0)

{

cout << "customers accepted: " << customers << endl;

cout << "customers served: " << served << endl;

cout << "turnaways: " << turnaway << 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";

}

else

cout << "no customers!";

cout << "Done!";

system("pause");

return 0;

}

bool newcustomer(double x)//通过此函数来模拟每分钟是否来新顾客

{

return rand()*x / RAND_MAX < 1;

}

Note:对于数据成员为引用或者是非静态const类型,都必须对于构造函数采用初始化列表形式去赋值

程序运行结果如下图

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,084评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,623评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,450评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,322评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,370评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,274评论 1 300
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,126评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,980评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,414评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,599评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,773评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,470评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,080评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,713评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,852评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,865评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,689评论 2 354

推荐阅读更多精彩内容