天天看點

【c++實作】模拟銀行叫号系統

#include<iostream>

using namespace std;

  struct QNode{

char id;//目前的号碼

QNode *next;//指向下一個節點

};

class LoopQueue{

private:

QNode *head;//指向的指針

QNode *rear;//指向尾部的指針

int length;//長度    用于取号的時候

public:

void init();

void EnQueue(char n);//入隊

bool DeQueue(QNode &e);//出隊

void add(QNode *p);

void removenode();

//void tranverse();//周遊

// bool SetEmpty();  //置空

int getLength();

QNode * gethead(){

return head;

}

};

//沒有頭結點

//初始化

void LoopQueue::init(){

head=NULL; rear=NULL;

length=0;

}

//入隊

void LoopQueue::add(QNode *e){

     QNode *p=e;//讓指針p指向e;等待接入節點

    if(length==0){//空隊列

        head=p;

        rear=p;length++;

    }else{

         rear->next=p;

        rear=rear->next;//rear 移動到新接入的節點上

        length++;

    }

}

void LoopQueue::EnQueue(char n){//改一改自己的風格,傳個節點吧!!!娜娜棒棒的

QNode *p=new QNode;

p->id=n;//傳入的值作為  節點p的id值   封裝好資料

add(p);

}

//出隊

bool LoopQueue::DeQueue(QNode &e){//e是要取值的節點

if(length==0){//空

return false;

}

e.id=head->id;//叫号時候的号碼  用e把他傳出來

QNode *p=head;//再用一個指針  指向頭  準備釋放掉這個節點

head=head->next;//移動head這個指針

return true;

}

void LoopQueue::removenode(){

    if(length==0){

            cout<<"沒有了!!!"<<endl;

    }

        QNode *p = head;

        head=p->next;//移動head

        delete p;

        length--;

}

 int  LoopQueue::getLength(){

  return length;

 }

#include "BankHead.h"

int main(){

int a,i=1;

QNode e;

LoopQueue queue;

queue.init();//初始化

cout<<"請選擇:\n1、歡迎光臨!請排隊取号,等候叫号!"<<endl

        <<"2、服務号空閑,開始叫号" <<endl

        <<"3、服務結束!退出系統!"<<endl;

while(cin>>a){

        switch(a){

            case 1:

                queue.EnQueue(i++);

cout<<"歡迎光臨!"<<"綜合業務"<<endl

<<"您的号碼是"<<i-1<<endl

//<<"您的号碼是"<<queue.getLength()<<endl

<<"您前面有"<<queue.getLength()-1<<"人等候"<<endl;break;

            case 2:

                if(queue.DeQueue(e)){

                    cout<<"請"<<e.id<<"号客戶到視窗辦理!"<<endl;

                }else{

                    cout<<"效率不錯!所有客戶問題解決完畢!\n你可以稍稍休息一下\n";

                }break;

            case 3:cout<<"小娜娜,期待您下次光臨哈!"<<endl;break;

        }

}

return 0;

}

繼續閱讀