Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;队列:如何循环显示洗车的正确开始时间和等待时间_C++_Loops_Queue - Fatal编程技术网

C++ C++;队列:如何循环显示洗车的正确开始时间和等待时间

C++ C++;队列:如何循环显示洗车的正确开始时间和等待时间,c++,loops,queue,C++,Loops,Queue,对于此洗车模拟,您的程序通过输入文件读取车辆到达时间。洗车总时间为3分钟。洗车时,另一辆车不能进入洗车区,这会增加等待时间。如果一辆车在第3分钟发车,下一辆车如果已经到达,则需要在第4分钟进站 我已经尝试过一次读取文件,然后创建另一个循环,但没有成功。我已经尝试了很多东西,我想我只是遇到了一个如何循环程序的问题 #include <iostream> #include <cassert> #include <fstream> #include <queu

对于此洗车模拟,您的程序通过输入文件读取车辆到达时间。洗车总时间为3分钟。洗车时,另一辆车不能进入洗车区,这会增加等待时间。如果一辆车在第3分钟发车,下一辆车如果已经到达,则需要在第4分钟进站

我已经尝试过一次读取文件,然后创建另一个循环,但没有成功。我已经尝试了很多东西,我想我只是遇到了一个如何循环程序的问题

#include <iostream>
#include <cassert>
#include <fstream>
#include <queue>
#include <cstdlib>
    using namespace std;
class averager {
private:
    int cnt;
    int sum;
public:
    averager(){
        cnt=0;
        sum=0;
    }
    void plus_next_number(int value)
    {
        cnt++;
        sum+=value;
    }
    double average_time()
    {
        assert(cnt>0);
        return (sum/cnt);
    }
    int how_many_cars()
    {
        return cnt;
    }
};
class Washmachine {
private:
    int time_for_wash;
    int time_left;

public:
    Washmachine(int n) {
        time_for_wash = n;
        time_left = 0;

    }

    bool is_busy() {
       return (time_left > 0);
    }

    void startWashing() {
       if(!is_busy()) {
           time_left = time_for_wash;
       }

    }

void one_second(){
        if(is_busy()) {
            --time_left;
        }
    }


};



int main() {
    queue<int> waitQueue;
    int carArrival;
    averager cal;
    ifstream infile;
    ofstream arrivalrec;
    arrivalrec.open("arrival_time.txt");
    arrivalrec << "Car Number  " << "Arrival Time  " << "Car Wash Start Time  " << "Departure Time  "
               << "Wait Time  "
               << "Total Time  " << endl
               << endl;

    int  maxWaitTime;   // maxWaitTime initially 0:00
    int totalWaitTime;           // total time customers wait
    int endTime = 540;      // times for the simulation
    int totalServiceTime;
    int startTime;
    int carNum = 0;         // number of cars washed in study
    int washTime = 3;                       // fixed time for a wash in minutes
    int DeptTime;
    int TotalTime;
    int timeleft=0;
    int waitTime;
    int temp;
    int sw;
    Washmachine carwashing(washTime);



    infile.open("input.txt");

   for (int startTime=0;startTime<=endTime;startTime++){

        infile>>temp;
        waitQueue.push(temp);
        if((!carwashing.is_busy())&&(!waitQueue.empty())) {
           carArrival=waitQueue.front();
            waitQueue.pop();
            waitTime=temp-carArrival;
            cal.plus_next_number(temp-carArrival);
            carwashing.startWashing();
        }
        carwashing.one_second();


        if (maxWaitTime<waitTime)
            maxWaitTime=waitTime;
        // add waiting time for customer to totalWaitTime.
        totalWaitTime+=waitTime;
        totalServiceTime+=washTime;
        startTime=temp+waitTime;
        TotalTime=washTime+waitTime;

        DeptTime=startTime +washTime;
        // increment the number of customers served
        carNum++;
        // set washAvailable to false since equipment back in service


        // output the summary data for the simulation include number of cars
        // washed, average customer waiting time and pct of time wash operates

        arrivalrec << carNum << "              " << temp << "                   " <<startTime
                   << "                  " << DeptTime << "              " <<
                   waitTime << "                " << TotalTime << endl
                   << endl << endl;


    }
    arrivalrec << "Maximum customer waiting time for a car wash is "
                << "14 minutes" << endl;
    arrivalrec << "Percentage of time car wash operates is 57 "
               //<< ((totalServiceTime / endTime) * 100.0)
               << '%' << endl;
    arrivalrec << "Number of customers remaining at " << endTime
               << " is 8"<<endl; //<< waitQueue.size() << endl;
    arrivalrec<<"\nCars washed were: "<<carNum<<endl;
    arrivalrec<<"\nThe average waiting time is: "<<cal.average_time()<<endl;
    int car_denied=0;
    while(!waitQueue.empty())
    {
        waitQueue.pop();
        car_denied++;
    }
    arrivalrec<<"\nThe number of denied cars is: 2 "<<endl;
    arrivalrec<<endl;
    return 0;
}

请尝试以下循环以实现洗车模拟的主要功能。 循环使用模拟运行时,而不是在
startTime
上循环。所有事件,如排队、启动和记录洗车过程以及计算等待时间,都是根据以下条件完成的:

infle.open(“input.txt”);
填充>>温度;
carNum=1;
对于(运行时=1;运行时>温度;
}
如果((!carwashing.is_busy())&&(!waitQueue.empty()){
carArrival=waitQueue.front();
waitQueue.pop();
startTime=运行时;
waitTime=startTime carArrival;
totalWaitTime=等待时间;
TotalTime=清洗时间+等待时间;
加上下一个号码(startTime carArrival);
洗车;
}
其他的
{
waitTime++;
}
if(洗车忙吗())
洗车,一秒钟;
如果((!carwashing.is_busy())&&(startTime>=deptime)){
DeptTime=开始时间+清洗时间;
总服务时间+=清洗时间;

arrivalrec欢迎使用堆栈溢出。请阅读、获取、阅读以及。最后,请学习如何创建一个来向我们展示,重点是最小部分。此外,
sum/cnt
是一个整数表达式,它将给出整数结果。
Car Arrival 0  car start 0 car depart 3 wait time 0 total time 3
            3            4            7           1            4
            10           10           13          0            3
            11           14           17          3            6