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

Posted

技术标签:

【中文标题】(C++) 为啥我的程序没有输出平均等待时间和超过 1 小时的平均时间?【英文标题】:(C++) Why is my program not outputting average wait time and average time over 1 hour?(C++) 为什么我的程序没有输出平均等待时间和超过 1 小时的平均时间? 【发布时间】:2020-10-09 16:21:59 【问题描述】:

这是一个使用队列模拟教师办公时间排队的程序。它会生成随机数量的学生,让他们每个人在办公室度过的时间都是随机的,并将他们添加到队列中。它还应该计算每个学生平均等待时间,以及办公时间是否超过 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;

【问题讨论】:

欢迎来到 ***。这就是调试器适用的情况。在调试器中运行您的代码,这样您就可以逐行执行代码,观察每个语句的作用,当它的行为不是您所期望的时,您可以停止程序并修复它。调试是任何开发人员都必须学习的基本技能,所以现在正是这样做的好时机。 整数对于平均值不是很有用。 【参考方案1】:

您在设置为time(0) 的时间变量之间多次使用difftime:它们最终假定相同的值,因为它们几乎同时被初始化。 因此,我为它们添加了rand() 术语;如果您愿意,可以更准确地选择这些值。

另外,在while-loop 中,我已将s 学生项目更改为指向queue 结构的指针,因此不会创建这些对象的新实例。

在您的问题下的 cmets 中有一个有用的建议:取平均值时保留 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++) 为啥我的程序没有输出平均等待时间和超过 1 小时的平均时间?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的作业有逻辑错误? [关闭]

为啥我的 C++ 程序没有运行?

为啥在多线程应用程序 C++ 中没有发生同步

为啥 API 在 C++ 中挂钩 ExtTextOut 和 DrawText 只输出垃圾?

为啥循环摘要在 gprof 的调用图输出中没有任何调用者?

为啥我的 weighted_grade 变量不起作用?