C++ Car Simulation

一曲冷凌霜 提交于 2019-12-13 04:18:27

问题


C++ Data Structures using Queue, My code output is not correct, I am not sure what to change.

C++ using the stl Queue library, I am having trouble getting the correct output from my program. The wait time is not being displayed correctly and the start time of the wash is not being displayed correctly. This is the code I have so far:

#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;
}

The correct code output

My code output


回答1:


The following code solves your task conform to the photographed description with two exceptions:

  1. The average wait time isn't converted into minutes and seconds
  2. The total car wash use time isn't reported

The rest works as specified:

#include <iostream>
#include <cassert>
#include <fstream>
#include <queue>
#include <cstdlib>

constexpr auto SIMULATION_END_TIME = 80;
constexpr auto OPENING_END_TIME = 60;

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/static_cast<double>(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 = 0;
    averager cal;
    ifstream infile;
    ofstream arrivalrec;
    arrivalrec.open("arrival_time.txt");
    arrivalrec << "Start of Simulation" << endl;
    arrivalrec << "Car\t\tArrival\tCar Wash\tDeparture\tWait\tTotal" << endl;
    arrivalrec << "Number\tTime\tStart Time\tTime\t\tTime\tTime" << endl;
    arrivalrec << "------------------------------------------------------------" << endl;

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



    infile.open("input.txt");
    infile >> temp;
    carNum = 1;

    for (runTime=1;runTime<=SIMULATION_END_TIME;runTime++){

        if (runTime == temp) {
            waitQueue.push(temp);
            infile >> temp;
        }
        if((runTime <= OPENING_END_TIME)&&(!carwashing.is_busy())&&(!waitQueue.empty())) {
            carArrival=waitQueue.front();
            waitQueue.pop();
            startTime = runTime;
            waitTime=startTime-carArrival;
            totalWaitTime += waitTime;
            TotalTime = washTime + waitTime;
            if (maxWaitTime < waitTime)
                maxWaitTime = waitTime;
            cal.plus_next_number(startTime-carArrival);
            carwashing.startWashing();
        }
        else
        {
            waitTime++;
        }
        if (carwashing.is_busy())
            carwashing.one_second();

        if ((!carwashing.is_busy())&&(startTime >= DeptTime)) {
            DeptTime = startTime + washTime;
            totalServiceTime += washTime;
            arrivalrec << carNum << "\t\t" << carArrival << "\t\t" << startTime
                << "\t\t\t" << DeptTime << "\t\t\t" <<
                waitTime << "\t\t" << TotalTime << endl;
            carNum++;
        }
    }
    int car_denied = 0;
    while (!waitQueue.empty())
    {
        arrivalrec << carNum << "\t\t" << waitQueue.front() << "\tCar arrived after closing time and was not served." << endl;
        waitQueue.pop();
        car_denied++;
        carNum++;
    }
    arrivalrec << "End of Simulation" << endl << endl;
    arrivalrec << "Statistics:" << endl;
    arrivalrec << "\tTotal wait time: " << totalWaitTime << " minutes" << endl;
    arrivalrec << "\tMaximum customer waiting time for a car wash is "
                << maxWaitTime << " minutes" << endl;
    arrivalrec << "\tPercentage of time car wash operates is "
                << ((totalServiceTime / static_cast<double>(OPENING_END_TIME)) * 100.0)
                << " %" << endl;
    arrivalrec<<"\tCars washed were: "<<carNum - car_denied - 1<<endl;
    arrivalrec<<"\tThe average waiting time is: "<<cal.average_time()<<endl; // TODO: Convert time to minutes and seconds
    arrivalrec<<"\tThe number of denied cars is:"<<car_denied<<endl;
    arrivalrec<<endl;
    return 0;
}

Output of arrival_time.txt file is:

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

Please check the code with your one and accept this answer if it fits to your issue. Thanks.




回答2:


untitled.cc:7:1: error: unknown type name 'constexpr' constexpr auto SIMULATION_END_TIME = 80; ^ untitled.cc:7:11: error: expected unqualified-id constexpr auto SIMULATION_END_TIME = 80; ^ untitled.cc:8:1: error: unknown type name 'constexpr' constexpr auto OPENING_END_TIME = 60; ^ untitled.cc:8:11: error: expected unqualified-id constexpr auto OPENING_END_TIME = 60; ^ untitled.cc:99:29: error: use of undeclared identifier 'SIMULATION_END_TIME' for (runTime=1;runTime<=SIMULATION_END_TIME;runTime++){ ^ untitled.cc:105:24: error: use of undeclared identifier 'OPENING_END_TIME' if((runTime <= OPENING_END_TIME)&&(!carwashing.is_busy())&&(!waitQueue.empty())) { ^ untitled.cc:147:61: error: use of undeclared identifier 'OPENING_END_TIME' << ((totalServiceTime / static_cast(OPENING_END_TIME)) * 100.0) ^ 7 errors generated.



来源:https://stackoverflow.com/questions/58332835/c-car-simulation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!