im的任务是让用户输入开始和停止时间,并计算和显示差异。我们需要一个头文件MyTime,其中包含带有小时,分钟和秒的结构。我的主要功能提示用户输入时间,它直接进入MyTime结构。然后,将调用DefineElapsedTime函数,并将两个指针传递给包含用户输入时间的两个结构,然后将其返回的指针存储在“指向MyTime的指针”变量中。那就是我遇到麻烦的地方。对此也给出了提示:“在DefineElapsedTime中声明一个静态MyTime结构,在其中存储经过的时间,然后返回其地址。返回指针或对自动对象的引用总是错误的。”
非常感谢您的帮助;我到目前为止所拥有的是下面。现在,我的调用DecisionElapsedTime函数的TimeDiff变量正在返回垃圾。我不确定我是否正确设置了结构。我是C ++的新手,因此深表感谢。谢谢。
主要功能:
#include <iostream>
#include "C1A7E1_MyTime.h"
using namespace std;
MyTime* DetermineElapsedTime(const MyTime* StartTime, const MyTime* StopTime);
int main()
{
MyTime StartTime, StopTime;
MyTime *TimeDiff;
for (int i = 1; i <= 3; i++);
{
cout << "Please enter a Start and Stop Time that is space seperated on "
"the same line in the following colon-delimited format "
"(HH:MM:SS): ";
// Declare Colon delimiters
char Colon1, Colon2, Colon3, Colon4;
// Get users input
cin >> StartTime.hours >> Colon1 >> StartTime.minutes >> Colon2
>> StartTime.seconds >> StopTime.hours >> Colon3
>> StopTime.minutes >> Colon4 >> StopTime.seconds;
TimeDiff = DetermineElapsedTime(&StartTime, &StopTime);
cout << "\nThe time elapsed from " << StartTime.hours << Colon1
<< StartTime.minutes << Colon2 << StartTime.seconds << " to "
<< StopTime.hours << Colon3 << StopTime.minutes
<< Colon4 << StopTime.seconds << " is " << TimeDiff;
}
return 0;
}
DetermineElapsedTime函数:
#include "C1A7E1_MyTime.h"
#define HR_SECONDS 3600;
#define MIN_SECONDS 60;
using namespace std;
MyTime *DetermineElapsedTime(const MyTime *StartTime, const MyTime *StopTime)
{
// Declare static MyTime structure to store elapsed time in
MyTime static ElapsedTime;
// Pull out Start Time in seconds
long int Start_Hr = StartTime->hours * HR_SECONDS;
int Start_Min = StartTime->minutes * MIN_SECONDS;
int Start_Sec = StartTime->seconds;
// Total the number of seconds in start time
int TotalStart = Start_Hr + Start_Min + Start_Sec;
// Pull out Stop Time in seconds
long int Stop_Hr = StopTime->hours * HR_SECONDS;
int Stop_Min = StopTime->minutes * MIN_SECONDS;
int Stop_Sec = StopTime->seconds;
// Total the number of seconds in stop time
int TotalStop = Stop_Hr + Stop_Min + Stop_Sec;
// Find the difference in start/stop time
long int TimeDiff = (TotalStop - TotalStart);
// If stop time is less than start time (new day) then make it positive
if (TimeDiff <= 0)
{
TimeDiff = -TimeDiff;
}
// Find time difference in hours
int ElapsedHr = TimeDiff / HR_SECONDS;
ElapsedTime.hours = ElapsedHr;
// Find time difference in minutes
int FirstSub = ElapsedHr * HR_SECONDS;
int remainingSeconds = (TimeDiff) - FirstSub;
int ElapsedMin = remainingSeconds / MIN_SECONDS;
ElapsedTime.minutes = ElapsedMin;
// Find time difference in seconds
int SecondSub = ElapsedMin * MIN_SECONDS;
remainingSeconds = remainingSeconds - SecondSub;
int ElapsedSec = remainingSeconds;
ElapsedTime.seconds = ElapsedSec;
//Return address of elapsed time
return(&ElapsedTime);
}
MyTime结构/头文件
#ifndef C1A7E1_MYTIME_H // Header Guard
#define C1A7E1_MYTIME_H
//Structure Definition and declaration of structur variables
struct MyTime
{
int hours, minutes, seconds;
};
#endif