⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulationimp.cpp

📁 C++编成数据结构与程序设计方法 D.S.Malk编著
💻 CPP
字号:


#include <iostream>
#include <string>
#include <cstdlib>

#include "Simulation.h"
#include "queueAsArray.h"

using namespace std;


//*************** customerType ************

void customerType::setCustomerInfo(int customerN, int arrvTime, 
                                   int wTime, int tTime)
{
    customerNumber = customerN;
    arrivalTime = arrvTime;
    waitingTime = wTime;
    transactionTime = tTime;
}

customerType::customerType(int customerN, int arrvTime, 
                           int wTime, int tTime)
{
    setCustomerInfo(customerN, arrvTime, wTime, tTime);
}


int customerType::getWaitingTime() const
{
    return waitingTime;
}

void customerType::incrementWaitingTime()
{
    waitingTime++;
}

void customerType::setWaitingTime(int time)
{
    cout << "See Programming Exercise 17 at the end of this chapter." << endl;
}

int customerType::getArrivalTime() const
{
    cout << "See Programming Exercise 17 at the end of this chapter." << endl;
    return 0;
}

int customerType::getTransactionTime() const
{
    cout << "See Programming Exercise 17 at the end of this chapter." << endl;
    return 0;
}

int customerType::getCustomerNumber() const
{
    cout << "See Programming Exercise 17 at the end of this chapter." << endl;
    return 0;
}
//**************** serverType **********

serverType::serverType()
{
    status = "free";
    transactionTime = 0;
}

bool serverType::isFree() const
{
    return (status == "free");
}

void serverType::setBusy()
{
    status = "busy";
}

void serverType::setFree()
{
    status = "free";
}

void serverType::setTransactionTime(int t)
{
    transactionTime = t;
}

void serverType::setTransactionTime()
{
    int time;

    time = currentCustomer.getTransactionTime();

    transactionTime = time;
}

void serverType::decreaseTransactionTime()
{
    transactionTime--;
}

int serverType::getRemainingTransactionTime() const
{
    cout << "See Programming Exercise 17 at the end of this chapter." << endl;
    return 0;
}

void serverType::setCurrentCustomer(customerType cCustomer)
{
    cout << "See Programming Exercise 17 at the end of this chapter." << endl;
}

int serverType::getCurrentCustomerNumber() const
{
    cout << "See Programming Exercise 17 at the end of this chapter." << endl;
    return 0;
}

int serverType::getCurrentCustomerArrivalTime() const
{
    cout << "See Programming Exercise 17 at the end of this chapter." << endl;
    return 0;
}

int serverType::getCurrentCustomerWaitingTime() const
{
    cout << "See Programming Exercise 17 at the end of this chapter." << endl;
    return 0;
}

int serverType::getCurrentCustomerTransactionTime() const
{
    cout << "See Programming Exercise 17 at the end of this chapter." << endl;
    return 0;
}


//************** serverListType ***********

serverListType::serverListType(int num)
{
    numOfServers = num;
    servers = new serverType[num];
}

serverListType::~serverListType()
{
    delete [] servers;
}

int serverListType::getFreeServerID() const
{
    int serverID = -1;

    int i;

    for (i = 0; i < numOfServers; i++)
        if (servers[i].isFree())
        {
            serverID = i;
            break;
        }

    return serverID;
}

int serverListType::getNumberOfBusyServers() const
{
    int busyServers = 0;

    int i;

    for (i = 0; i < numOfServers; i++)
        if (!servers[i].isFree())
            busyServers++;

    return busyServers;
}

void serverListType::setServerBusy(int serverID, 
                                   customerType cCustomer, 
                                   int tTime)
{
    servers[serverID].setBusy();
    servers[serverID].setTransactionTime(tTime);
    servers[serverID].setCurrentCustomer(cCustomer);
}

void serverListType::setServerBusy(int serverID, 
                                   customerType cCustomer)
{
    int time;

    time = cCustomer.getTransactionTime();

    servers[serverID].setBusy();
    servers[serverID].setTransactionTime(time);
    servers[serverID].setCurrentCustomer(cCustomer);
}

void serverListType::updateServers(ostream& outFile)
{
    int i;

    for (i = 0; i < numOfServers; i++)
        if (!servers[i].isFree())
        {
            servers[i].decreaseTransactionTime();

            if (servers[i].getRemainingTransactionTime() == 0)
            {
                outFile << "From server number  " << (i + 1) 
                        << " customer number "
                        << servers[i].getCurrentCustomerNumber()
                        << "\n     departed at time unit "
                        << servers[i].
                              getCurrentCustomerArrivalTime()
                           + servers[i].
                              getCurrentCustomerWaitingTime() 
                           + servers[i].
                              getCurrentCustomerTransactionTime()
                        << endl;
                servers[i].setFree();
            }
        }
}

//*************** waitQueue ************


waitingCustomerQueueType::waitingCustomerQueueType(int size)
                          :queueType<customerType>(size)
{
}

void waitingCustomerQueueType::updateWaitingQueue()
{
    customerType cust;

    cust.setWaitingTime(-1);  
    int wTime = 0;
	
	addQueue(cust);

    while (wTime != -1)
    {
        cust = front();
        deleteQueue();

        wTime = cust.getWaitingTime();
        if (wTime == -1)
            break;
        cust.incrementWaitingTime();
        addQueue(cust);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -