gpt4 book ai didi

c++ - (C++)为什么我的程序没有输出平均等待时间和超过1小时的平均时间?

转载 作者:行者123 更新时间:2023-12-02 10:26:28 26 4
gpt4 key购买 nike

这是一个使用队列模拟教师上类时间的线路的程序。它会产生随机数的学生,给他们每个在办公室花费的随机时间,然后将他们添加到队列中。还应该计算每个学生等待的平均时间,以及办公时间是否超过1个小时。
它也应该运行100次并取所有平均值。
但是,每当我运行它时,等待时间和超过1小时的时间都显示为0,我无法真正弄清楚原因。我敢肯定,这只是我忽略的一些小事情,但出于任何原因,我都无法解决。
谢谢!
让我知道您是否需要我澄清有关我的代码或问题的任何信息。
这是我的代码:

#include <iostream>
#include <ctime> //used for the time(x) function & difftime.
#include <queue>
#include <cmath>

using namespace std;

class Student{
public:
int timeInOffice, arrivalTime, waitTime;
};

int main(){
int numOfStudents;
queue<Student> officeLine;
int avgWaitFinal = 0, avgTimeInOfficeFinal = 0, avgTimeOverFinal = 0;
//avgWaitFinal is the final average wait time
//avgTimeinOfficeFinal is the final average time spent in office
//avgTimeOverFinal is the average time over 1 hour spent in the office.
for (int i=1; i <= 100; i++){
numOfStudents = rand()%10+1;
//This picks a random number of student between 1 and 10
for (int j=0; j < numOfStudents; j++){
Student st;
st.timeInOffice = rand()%10+1;
//this picks a random time spent in the office between 1 and 10 minutes.
st.arrivalTime = time(0);
officeLine.emplace(st);
}
int avgWait = 0, avgTimeInOffice = 0;
time_t officeArrivalTime = time(0);
while (! officeLine.empty()){
Student s = officeLine.front();
s.waitTime = time(0);
avgWait += (int)std::round(difftime(s.waitTime, s.arrivalTime));
avgTimeInOffice += s.timeInOffice;
officeLine.pop();
}
time_t outTime = time(0);
int totalTime = (int)std::round(difftime(outTime, officeArrivalTime));
//this takes the difference between the time the student left and the time they arrived to calculate the total time they spent in the office.
int timeOver = totalTime > 60 ? (totalTime - 60) : 0;
avgTimeOverFinal += timeOver;
avgWait = avgWait/numOfStudents;
avgWaitFinal += avgWait;
avgTimeInOffice = avgTimeInOffice/numOfStudents;
avgTimeInOfficeFinal += avgTimeInOffice;
cout<<"Run #: "<<i<<endl;
cout<<"Number of Students: "<< numOfStudents<<endl;
cout<<"Average Wait Time: "<<avgWait<<endl;
cout<<"Average Visit Time: "<<avgTimeInOffice<<endl;
cout<<"Time Over 1 Hour: "<<timeOver<<endl;
//These are the stats for each individual run of the program
cout<<endl;
}
cout<<"Overall Average Wait Time: "<< (avgWaitFinal/100)<<endl;
cout<<"Overall Average Time in Office: "<< (avgTimeInOfficeFinal/100)<<endl;
cout<<"Overall Average Time Over 1 Hour: "<< (avgTimeOverFinal/100)<<endl;
//these are the final averages of 100 runs
return 0;
}

最佳答案

您可以在设置为difftime的时间变量之间多次使用time(0):它们最终会采用相同的值,因为它们几乎同时被初始化。
因此,我在其中添加了rand()术语;如果需要,可以更准确地选择这些值。
另外,在while -loop内,我将s学生项目更改为指向queue结构的指针,因此没有创建这些对象的新实例。
在您的问题下的注释中有一个有用的建议:取平均值时保留double值,然后再将其四舍五入为int
这是我的代码修订版:

#include <iostream>
#include <ctime> //used for the time(x) function & difftime.
#include <queue>
#include <cmath>

using namespace std;

class Student{
public:
int timeInOffice, arrivalTime, waitTime;
};

int main(){
int numOfStudents;
queue<Student> officeLine;
int avgWaitFinal = 0, avgTimeInOfficeFinal = 0, avgTimeOverFinal = 0;
//avgWaitFinal is the final average wait time
//avgTimeinOfficeFinal is the final average time spent in office
//avgTimeOverFinal is the average time over 1 hour spent in the office.
for (int i=1; i <= 100; i++){
numOfStudents = rand()%10+1;
//This picks a random number of student between 1 and 10
for (int j=0; j < numOfStudents; j++){
Student st;
st.timeInOffice = rand()%10+1;
//this picks a random time spent in the office between 1 and 10 minutes.
st.arrivalTime = time(0) + rand()%10+1;
officeLine.emplace(st);
}
double avgWait = 0, avgTimeInOffice = 0;
time_t officeArrivalTime = time(0);
while ( !officeLine.empty() ){
Student* s = &officeLine.front();
s->waitTime = time(0);
avgWait += difftime(s->arrivalTime, s->waitTime);
avgTimeInOffice += s->timeInOffice;
officeLine.pop();
}
time_t outTime = time(0) + rand()% 100 + 1;
double totalTime = difftime(outTime, officeArrivalTime);
//this takes the difference between the time the student left and the time they arrived to calculate the total time they spent in the office.
int timeOver = totalTime > 60 ? (totalTime - 60) : 0;
avgTimeOverFinal += timeOver;
avgWait = std::round(avgWait/numOfStudents);
avgWaitFinal += avgWait;
avgTimeInOffice = std::round(avgTimeInOffice/numOfStudents);
avgTimeInOfficeFinal += avgTimeInOffice;
cout<<"Run #: "<<i<<endl;
cout<<"Number of Students: "<< numOfStudents<<endl;
cout<<"Average Wait Time: "<<avgWait<<endl;
cout<<"Average Visit Time: "<<avgTimeInOffice<<endl;
cout<<"Time Over 1 Hour: "<<timeOver<<endl;
//These are the stats for each individual run of the program
cout<<endl;
}
cout<<"Overall Average Wait Time: "<< (avgWaitFinal/100)<<endl;
cout<<"Overall Average Time in Office: "<< (avgTimeInOfficeFinal/100)<<endl;
cout<<"Overall Average Time Over 1 Hour: "<< (avgTimeOverFinal/100)<<endl;
//these are the final averages of 100 runs
return 0;
}

关于c++ - (C++)为什么我的程序没有输出平均等待时间和超过1小时的平均时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64283821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com