C++實現銀行排隊系統

1.上機作業(資本主義的世界)

2.代碼

頭文件 banksystem.h

#ifndef BANKSYSTEM_H_INCLUDED
#define BANKSYSTEM_H_INCLUDED

#include<iostream>
#include<queue>
#include<list>
#include<cstdlib>
#include<ctime>
using namespace std;
const int SERVERTIME = 4;
const int ENTERTIME = 2;
int MainTime = 0;
int customerEnterMatrix[100][3];

//NormalWindow:VIP:Official:=3:1:1
//random produce the order of the Users
//
class User{
protected:
     int id;
     int isWait;
     int arriveTime;
     int serverTime;
     string type;
public :
    virtual void getServered(string,int) = 0;
    void setSeverTime(int time){
       serverTime = time;
    }
};

class NormalUser:public User{
public :
    void getServered(string,int);
    NormalUser(int id,int a){
      this->id = id;
       arriveTime=a;
      type = "NormalUser";
    }
};

class VIPUser:public User{
public :
    void getServered(string ,int);
    VIPUser(int id,int a){
     this->id=id;
     arriveTime=a;
     type="VIPUser";
    }
};

class OfficialUser:public User{
public :
    void getServered(string ,int);
    OfficialUser(int id,int a){
    this->id=id;
    arriveTime=a;
    type="OfficialUser";
    }
};

class BankWindow{
protected:
    bool isBusy;
    int id;
    User *client;
    string type;
public :
    BankWindow():isBusy(0){}
    int getBusy(){return isBusy;}
    void setBusy(int i){isBusy=i;}
    virtual void handleUser(User *user) = 0;
};

class NormalWindow:public BankWindow{
public :
    NormalWindow(int id){
    this->id=id;
    type="NormalWindow";
    }
    void handleUser(User *);
};

class VIPWindow:public BankWindow{
public :
    VIPWindow(int id){
    this->id=id;
    type="VIPWindow";
    }
    void handleUser(User *);
};

class OfficialWindow:public BankWindow{
public :
    OfficialWindow(int id){
    this->id=id;
    type="OfficialWindow";
    }
    void handleUser(User *);
};

class Simulater{
private:
    queue<NormalUser> normalUserQueue;
    queue<VIPUser> vipUserQueue;
    queue<OfficialUser> officialUserQueue;
    list<NormalWindow> normalWindowList;
    list<VIPWindow> vipWindowList;
    list<OfficialWindow> officialWindowList;
    int UserID;
    int BankID;
public:
    Simulater(){UserID=1;BankID=1;}
    void customerEnter();
    void simulateCustomerEnter();
    void simulateCallCustomer();
    void simulate();
};

#endif // BANKSYSTEM_H_INCLUDED


源文件 banksystem.cpp

#include"banksystem.h"

void NormalUser::getServered(string bankType,int bankID){
   cout<<"*************************************"<<endl;
   cout<<"NormalUser:"<<id<<" getServered"<<endl;
   cout<<"Its waitTime is "<<serverTime-arriveTime<<endl;
   cout<<"Its servered Window is "<<bankType<<" "<<bankID<<endl;
}
void VIPUser::getServered(string bankType,int bankID){
   cout<<"*************************************"<<endl;
   cout<<"VIPUser:"<<id<<" getServered"<<endl;
   cout<<"Its waitTime is "<<serverTime-arriveTime<<endl;
   cout<<"Its servered Window is "<<bankType<<" "<<bankID<<endl;
}
void OfficialUser::getServered(string bankType,int bankID){
   cout<<"*************************************"<<endl;
   cout<<"OfficialUser:"<<id<<" getServered"<<endl;
   cout<<"Its waitTime is "<<serverTime-arriveTime<<endl;
   cout<<"Its servered Window is "<<bankType<<" "<<bankID<<endl;
}
void NormalWindow::handleUser(User *user){
    user->getServered(type,id);
}
void VIPWindow::handleUser(User *user){
    user->getServered(type,id);
}
void OfficialWindow::handleUser(User *user){
    user->getServered(type,id);
}
void Simulater::customerEnter(){
      cout<<customerEnterMatrix[MainTime/2][0]<<" NormalUser come in"<<endl;
      cout<<customerEnterMatrix[MainTime/2][1]<<" VIPUser come in"<<endl;
      cout<<customerEnterMatrix[MainTime/2][2]<<" OfficialUser come in"<<endl;
}
void Simulater::simulateCustomerEnter(){
      for (int i=0;i<customerEnterMatrix[MainTime/2][0];i++){
        NormalUser user(UserID++,MainTime);
        cout<<"NormalUser "<<UserID-1<<" is waiting."<<endl;
        normalUserQueue.push(user);
      }
      for (int i=0;i<customerEnterMatrix[MainTime/2][1];i++){
        VIPUser user(UserID++,MainTime);
        cout<<"VIPUser "<<UserID-1<<" is waiting."<<endl;
        vipUserQueue.push(user);
      }
      for (int i=0;i<customerEnterMatrix[MainTime/2][2];i++){
        OfficialUser user(UserID++,MainTime);
        cout<<"OfficialUser "<<UserID-1<<" is waiting."<<endl;
        officialUserQueue.push(user);
      }
}
void Simulater::simulateCallCustomer(){
    list<NormalWindow>::iterator it;
    for (it=normalWindowList.begin();it!=normalWindowList.end();it++){
        if(it->getBusy()==0){
            if(!normalUserQueue.empty()){
            NormalUser user = normalUserQueue.front();
            user.setSeverTime(MainTime);
            it->setBusy(2);
            it->handleUser(&user);
            normalUserQueue.pop();
            }
        }else{
           it->setBusy(0);
        }
    }
    list<VIPWindow>::iterator it2;
    for (it2=vipWindowList.begin();it2!=vipWindowList.end();it2++){
        if(it2->getBusy()==0){
            if(!vipUserQueue.empty()){
            VIPUser user = vipUserQueue.front();
            user.setSeverTime(MainTime);
            it2->setBusy(2);
            it2->handleUser(&user);
            vipUserQueue.pop();
            }else if(!normalUserQueue.empty()){
                NormalUser user = normalUserQueue.front();
                user.setSeverTime(MainTime);
                it2->setBusy(2);
                it2->handleUser(&user);
            }
        }else{
           it2->setBusy(0);
        }
    }
    list<OfficialWindow>::iterator it3;
    for (it3=officialWindowList.begin();it3!=officialWindowList.end();it3++){
        if(it3->getBusy()==0){
            if(!officialUserQueue.empty()){
            OfficialUser user = officialUserQueue.front();
            user.setSeverTime(MainTime);
            it3->setBusy(2);
            it3->handleUser(&user);
            officialUserQueue.pop();
            }else if(!normalUserQueue.empty()){
                NormalUser user = normalUserQueue.front();
                user.setSeverTime(MainTime);
                it3->setBusy(2);
                it3->handleUser(&user);
            }
        }else{
           it3->setBusy(0);
        }
    }
}
void Simulater::simulate(){
    //生成處理窗口
    for (int i=0;i<3;i++){
        NormalWindow window(BankID++);
        normalWindowList.push_back(window);
    }
    VIPWindow w(BankID++);
    OfficialWindow w2(BankID++);
    vipWindowList.push_back(w);
    officialWindowList.push_back(w2);
    //通過主時間線處理程序
    MainTime = 0;
    while(MainTime<20){
        cout<<endl;
        cout<<"At "<<MainTime<<" s."<<endl;
        customerEnter();
        simulateCustomerEnter();
        simulateCallCustomer();
        MainTime+=2;
    }
}
void showInstruction(){
    cout<<"***********銀行叫號系統************"<<endl;
    cout<<"*******所有數據均爲隨機生成********"<<endl;
    cout<<"以下爲模擬生成各個時間段進入銀行的人數"<<endl;
    //生成隨機數列表
    srand(time(NULL));
    cout<<"EnterTime  NormalUser  VIPUser  OfficialUser"<<endl;
    for (int i=0;i<10;i++){
      customerEnterMatrix[i][0] = rand()%5;
      customerEnterMatrix[i][1] = rand()%2;
      customerEnterMatrix[i][2] = rand()%2;
      if(i*2>=10) cout<<"    ";
      else cout<<"     ";
      cout<<i*2<<"          "<<customerEnterMatrix[i][0]<<"         "
      <<customerEnterMatrix[i][1]<<"         "<<customerEnterMatrix[i][2]<<endl;
    }
    cout<<"每個用戶取號時間爲 1 s "<<"每個用戶服務時間爲"<<SERVERTIME<<"s"<<endl;
}
int main()
{
    showInstruction();
    Simulater simulater;
    simulater.simulate();
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章