生活随笔
收集整理的這篇文章主要介紹了
数据结构 实验五(银行叫号系统)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
運用隊列實現銀行叫號系統。
銀行叫號系統源代碼
#include
<iostream
>
using namespace std
;struct Customers
{int m_id
=1;
};class Bank {
public:Bank(int queueCapacity
);bool
QueueEmpty();bool
QueueFull();int
QueueLength();bool
EnQueue(Customers customer
);bool
DeQueue(Customers
& customer
);void QueueTraverse();Customers
* m_pQueue
;int m_QueueLength
;int m_QueueCapacity
;int m_Head
;int m_Tail
;
};Bank
::Bank(int queueCapacity
) {m_QueueCapacity
= queueCapacity
;m_Head
= 0;m_Tail
= 0;m_QueueLength
= 0;m_pQueue
= new Customers[m_QueueCapacity
];
}bool Bank
::QueueEmpty() {return m_QueueLength
== 0 ? true : false;
}bool Bank
::QueueFull() {return m_QueueLength
== m_QueueCapacity
? true : false;
}int Bank
::QueueLength() {return m_QueueLength
;
}bool Bank
::EnQueue(Customers customer
) {if (QueueFull())return false;else {m_pQueue
[m_Tail
] = customer
;m_Tail
++;m_Tail
= m_Tail
% m_QueueCapacity
;m_QueueLength
++;return true;}
}bool Bank
::DeQueue(Customers
& customer
) {if (QueueEmpty())return false;else {customer
= m_pQueue
[m_Head
];m_Head
++;m_Head
= m_Head
% m_QueueCapacity
;m_QueueLength
--;return true;}
}void Bank
::QueueTraverse() {for (int i
= m_Head
; i
< m_QueueLength
; i
++) {cout
<< m_pQueue
[i
% m_QueueCapacity
].m_id
<< endl
;}
}int
main() {Bank
bank(10);Customers customer
;int count
= 1;cout
<< "歡迎使用銀行叫號系統" << endl
;cout
<< "1.有新客戶到達,取號" << endl
;cout
<< "2.有窗口空閑,叫號" << endl
;cout
<< "3.退出系統" << endl
;int number
;while (true) {cin
>> number
;if (number
== 1) {if (bank
.EnQueue(customer
)) {cout
<< "您的id是" << count
<< ",前面還有" << bank
.QueueLength() - 1 << "人在排隊!" << endl
;count
++;}else {cout
<< "取號失敗!" << endl
;}}if (number
== 2) {if (bank
.DeQueue(customer
)) {count
--;cout
<< "請第" << bank
.m_Head
<< "號客戶到窗口辦理!" << endl
;}else {cout
<< "隊列中已沒有客戶!" << endl
;}}if (number
== 3)exit(0);}return 0;
}
總結
以上是生活随笔為你收集整理的数据结构 实验五(银行叫号系统)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。