Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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++_Data Structures_Queue - Fatal编程技术网

C++ C++;汽车模拟

C++ C++;汽车模拟,c++,data-structures,queue,C++,Data Structures,Queue,使用C++数据结构的队列,我的代码输出不正确,我不确定要更改什么 C++使用stl队列库时,我无法从程序中获得正确的输出。等待时间显示不正确,清洗开始时间显示不正确。这是我目前掌握的代码: #include <iostream> #include <assert.h> #include <fstream> #include <queue> #include <stdlib.h> using namespace std; class

使用C++数据结构的队列,我的代码输出不正确,我不确定要更改什么

C++使用stl队列库时,我无法从程序中获得正确的输出。等待时间显示不正确,清洗开始时间显示不正确。这是我目前掌握的代码:

#include <iostream>
#include <assert.h>
#include <fstream>
#include <queue>
#include <stdlib.h>

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() {
        assert(!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 = 60;  // 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;

    Washmachine carwashing(washTime);

    infile.open("input.txt");
    for(int startWash = 0; startWash <= endTime; startWash++) {
        infile >> startWash;
        waitQueue.push(startWash);
        if((!carwashing.is_busy()) && (!waitQueue.empty())) {
            carArrival = waitQueue.front();
            waitQueue.pop();
            cal.plus_next_number(startWash - carArrival);
            carwashing.startWashing();
        }
        carwashing.one_second();
        waitTime = startWash - carArrival;

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

        startTime = startWash + waitTime;
        TotalTime = startWash + 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 << "              " << startWash
                   << "                   " << startTime << "                  "
                   << DeptTime << "              " << waitTime << "                "
                   << TotalTime << endl
                   << endl
                   << endl;
    }
    arrivalrec << "Maximum customer waiting time for a car wash is " << maxWaitTime
               << " minutes" << endl;
    arrivalrec << "Percentage of time car wash operates is  "
               << ((totalServiceTime / endTime) * 100.0) << '%' << endl;
    arrivalrec << "Number of customers remaining at " << endTime << " is "
               << 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: " << car_denied << endl;
    arrivalrec << endl;
    return 0;
}
#包括
#包括

我的代码输出


以下代码解决了您的任务,符合拍照描述,但有两个例外:

  • 平均等待时间不转换为分和秒
  • 未报告洗车总使用时间
  • 其余部分按规定工作:

    #包括
    #包括
    #包括
    #包括
    #包括
    constexpr自动模拟结束时间=80;
    constexpr自动打开\u结束\u时间=60;
    使用名称空间std;
    类平均器{
    私人:
    int-cnt;
    整数和;
    公众:
    平均值(){
    cnt=0;
    总和=0;
    }
    无效加上下一个数字(整数值)
    {
    cnt++;
    总和+=数值;
    }
    双倍平均时间()
    {
    断言(cnt>0);
    返回(总和/静态_-cast(cnt));
    }
    int有多少辆车()
    {
    返回cnt;
    }
    };
    高级洗衣机{
    私人:
    int time_用于清洗;
    int time_左;
    公众:
    洗衣机(国际){
    清洗时间=n;
    剩余时间=0;
    }
    布尔很忙{
    返回(剩余时间>0);
    }
    void startWashing(){
    如果(!正在忙()){
    剩余时间=清洗时间;
    }
    }
    无效一秒(){
    如果(正在忙()){
    --离开的时间;
    }
    }
    };
    int main(){
    排队等候;
    int carArrival=0;
    平均卡尔;
    河流充填;
    流向到达率;
    arrivalrec.open(“arrival_time.txt”);
    
    arrivalrecuntitled.cc:7:1:错误:未知类型名称“constexpr” constexpr自动模拟结束时间=80; ^ untitled.cc:7:11:错误:应为不合格id constexpr自动模拟结束时间=80; ^ untitled.cc:8:1:错误:未知类型名称“constexpr” constexpr自动打开\u结束\u时间=60; ^ untitled.cc:8:11:错误:应为不合格id constexpr自动打开\u结束\u时间=60; ^ untitled.cc:99:29:错误:使用未声明的标识符“SIMULATION\u END\u TIME”
    对于(运行时=1;运行时1)注意:只要有可能,复制粘贴文本,而不是提供文本的图像。例如,您的代码输出可能已被复制粘贴。2)您是否尝试使用调试器单步执行代码,以查看代码的行为何时偏离预期?我使用了调试器,我不确定这是否是因为我的文件的编辑方式我试图读取所有的东西,然后执行所有的功能,但是这只是破坏了代码,作为一个整体,给出了一个完全错误的输出。C:<代码> <代码>,C++ >代码> < /C> >代码:>代码> < />代码>,C++ >代码> < />代码> @ Miasiikh“我已经使用了调试器”您是如何使用调试器的?在每个执行步骤中,您是否逐行检查代码,同时观察所有变量的值,并记下代码何时、准确地执行了您不希望执行的操作?如果没有,则说明您没有使用调试器。相关:。
    Start of Simulation
    Car     Arrival Car Wash    Departure   Wait    Total
    Number  Time    Start Time  Time        Time    Time
    ------------------------------------------------------------
    1       1       1           4           2       3
    2       2       4           7           4       5
    3       4       7           10          5       6
    4       10      10          13          2       3
    5       13      13          16          2       3
    6       15      16          19          3       4
    7       16      19          22          5       6
    8       75  Car arrived after closing time and was not served.
    End of Simulation
    
    Statistics:
        Total wait time: 9 minutes
        Maximum customer waiting time for a car wash is 3 minutes
        Percentage of time car wash operates is 35 %
        Cars washed were: 7
        The average waiting time is: 1.28571
        The number of denied cars is:1