c语言版数据结构-用队列模拟一个银行排号系统的源代码
答案:2 悬赏:80 手机版
解决时间 2021-11-16 19:51
- 提问者网友:最爱你的唇
- 2021-11-15 20:27
c语言版数据结构-用队列模拟一个银行排号系统的源代码
最佳答案
- 五星知识达人网友:低音帝王
- 2021-11-15 22:05
写个main方法使用循环的方式调用BankSimSystem中的方法即可模拟整个排队和叫号过程。
class BankSimSystem {
private:
QueueManager mQManager;
public:
BankQueueSystem() {
mQManager = new QueueManager();
}
void getOneNum() {
int nRand = random(5);
int nNum;
if nRand <= 2) {
nNum = mQManager.getOneVIPNum();
printf("One people enqueue : N%d" nNum);
} else {
nNum = mQManager.getOneNormalNum();
printf("One people enqueue : V%d" nNum);
}
}
void CallOneNum() {
char *sNum = mQManager.CallOneNum();
if (nNum == -1) {
printf("None in queue");
}
printf("Call the people : %s", sNum);
}
}
class QueueManager {
private:
Queue mNormal;
Queue mVIP;
public:
QueueManager() {
mNormal = new Queue();
mVIP = new Queue();
}
char getOneNormalNum() {
int mNormal.enQueue();
}
int getOneVIPNum() {
return mVip.enQueue();
}
char* CallOneNum() {
char sNum[10];
if (!mVIP.isempty()) {
sprintf(sNum, "V%d",mVIP.outQueue())
return sNum;
}
if (!mNormal.isempty()) {
sprintf(sNum, "N%d",mNormal.outQueue())
return sNum;
}
return null;
}
};
class Queue {
private:
int mNum;
struct Index{
int num;
struct index *next;
} *mHead, *mTail;
public:
Queue() {
mNum = 0;
mHead = null;
mTail = null;
}
int enQueue() {
struct Index *tmpIndex = (struct Index *)malloc(sizeof(struct Index));
tmpIndex->num = mNum;
tmpIndex->next = null;
mNum++;
if (mTail == null) {
mHead = tmpIndex;
} else {
mTail->next = tmpIndex;
}
mTail = tmpIndex;
}
int outQueue() {
struct Index *tmpIndex;
int nReturn;
if (mHead == null) {
return -1;
}
tmpIndex = mHead;
mHead = mHead->next;
if (mHead == null) {
mTail == null;
}
nReturn = tmpIndex->num;
free(tmpIndex);
return nReturn;
}
}
class BankSimSystem {
private:
QueueManager mQManager;
public:
BankQueueSystem() {
mQManager = new QueueManager();
}
void getOneNum() {
int nRand = random(5);
int nNum;
if nRand <= 2) {
nNum = mQManager.getOneVIPNum();
printf("One people enqueue : N%d" nNum);
} else {
nNum = mQManager.getOneNormalNum();
printf("One people enqueue : V%d" nNum);
}
}
void CallOneNum() {
char *sNum = mQManager.CallOneNum();
if (nNum == -1) {
printf("None in queue");
}
printf("Call the people : %s", sNum);
}
}
class QueueManager {
private:
Queue mNormal;
Queue mVIP;
public:
QueueManager() {
mNormal = new Queue();
mVIP = new Queue();
}
char getOneNormalNum() {
int mNormal.enQueue();
}
int getOneVIPNum() {
return mVip.enQueue();
}
char* CallOneNum() {
char sNum[10];
if (!mVIP.isempty()) {
sprintf(sNum, "V%d",mVIP.outQueue())
return sNum;
}
if (!mNormal.isempty()) {
sprintf(sNum, "N%d",mNormal.outQueue())
return sNum;
}
return null;
}
};
class Queue {
private:
int mNum;
struct Index{
int num;
struct index *next;
} *mHead, *mTail;
public:
Queue() {
mNum = 0;
mHead = null;
mTail = null;
}
int enQueue() {
struct Index *tmpIndex = (struct Index *)malloc(sizeof(struct Index));
tmpIndex->num = mNum;
tmpIndex->next = null;
mNum++;
if (mTail == null) {
mHead = tmpIndex;
} else {
mTail->next = tmpIndex;
}
mTail = tmpIndex;
}
int outQueue() {
struct Index *tmpIndex;
int nReturn;
if (mHead == null) {
return -1;
}
tmpIndex = mHead;
mHead = mHead->next;
if (mHead == null) {
mTail == null;
}
nReturn = tmpIndex->num;
free(tmpIndex);
return nReturn;
}
}
全部回答
- 1楼网友:神也偏爱
- 2021-11-15 23:30
这个问题太范范了吧……
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯