【发布时间】:2019-10-11 00:48:06
【问题描述】:
C++ 数据结构使用队列,我的代码输出不正确,我不确定要改什么。
使用 stl Queue 库的 C++,我无法从我的程序中获取正确的输出。等待时间未正确显示,洗涤开始时间未正确显示。这是我到目前为止的代码:
#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;
}
正确的代码输出
我的代码输出
【问题讨论】:
-
1) 注意:尽可能复制粘贴文本,而不是提供文本图像。例如,您的代码输出可能已被复制粘贴。 2) 您是否尝试过使用调试器单步执行您的代码,以查看代码的行为何时偏离您的预期?
-
我使用了调试器,我不确定是不是因为我的文件被读取的方式。我试图一次阅读所有内容,然后执行这些功能,但这只会破坏整个代码,给出完全错误的输出。
-
C:
<assert.h>, C++<cassert>。 C:<stdlib.h>,C++<cstdlib>。 -
@MiaSheikh "我用过调试器" 如何你用过调试器?您是否逐行逐行执行代码,同时在每个执行步骤中观察所有变量的值,并注意代码何时准确地执行了您未曾预料到的事情?如果你没有:你没有使用调试器。相关:How to debug small programs.
标签: c++ data-structures queue