我想实现以下功能:
返回时钟一天中经过的时间(以秒为单位)。
以秒为单位返回时钟的剩余时间。
确定并输出两个时钟的时间间隔。以hr:min:sec的形式输出时间。
我到目前为止的代码是:
时钟类型
class clockType
{
public:
void setTime(int hours, int minutes, int seconds);
//Function to set the time.
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
// sec = seconds;
// The function checks whether the
// values of hours, minutes, and seconds
// are valid. If a value is invalid, the
// default value 0 is assigned.
void getTime(int& hours, int& minutes, int& seconds) const;
//Function to return the time.
//Postcondition: hours = hr; minutes = min;
// seconds = sec;
void printTime() const;
//Function to print the time.
//Postcondition: The time is printed in the form
// hh:mm:ss.
void incrementSeconds();
//Function to increment the time by one second.
//Postcondition: The time is incremented by one second.
// If the before-increment time is
// 23:59:59, the time is reset to 00:00:00.
void incrementMinutes();
//Function to increment the time by one minute.
//Postcondition: The time is incremented by one minute.
// If the before-increment time is
// 23:59:53, the time is reset to 00:00:53.
void incrementHours();
//Function to increment the time by one hour.
//Postcondition: The time is incremented by one hour.
// If the before-increment time is
// 23:45:53, the time is reset to 00:45:53.
bool equalTime(const clockType& otherClock) const;
//Function to compare the two times.
//Postcondition: Returns true if this time is equal to
// otherClock; otherwise, returns false.
clockType(int hours, int minutes, int seconds);
//Constructor with parameters
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
// sec = seconds;
// The constructor checks whether the
// values of hours, minutes, and seconds
// are valid. If a value is invalid, the
// default value 0 is assigned.
clockType();
//Default constructor
//The time is set to 00:00:00.
//Postcondition: hr = 0; min = 0; sec = 0;
private:
int hr; //variable to store the hours
int min; //variable to store the minutes
int sec; //variable to store the seconds
};
/Implementation File for the class clockType
clockTypeImp.cpp
#include <iostream>
#include "clockType.h"
using namespace std;
void clockType::setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}
void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
hours = hr;
minutes = min;
seconds = sec;
}
void clockType::incrementHours()
{
hr++;
if(hr > 23)
hr = 0;
}
void clockType::incrementMinutes()
{
min++;
if (min > 59)
{
min = 0;
incrementHours();
}
}
void clockType::incrementSeconds()
{
sec++;
if (sec > 59)
{
sec = 0;
incrementMinutes();
}
}
void clockType::printTime() const
{
if (hr < 10)
cout << "0";
cout << hr << ":";
if (min < 10)
cout << "0";
cout << min << ":";
if (sec < 10)
cout << "0";
cout << sec;
}
bool clockType::equalTime(const clockType& otherClock) const
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}
clockType::clockType(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}
clockType::clockType() //default constructor
{
hr = 0;
min = 0;
sec = 0;
}
** main.cpp **
#include <iostream>
#include "clockType.h"
using namespace std;
int main()
{
clockType myClock;
clockType yourClock;
int hours;
int minutes;
int seconds;
myClock.setTime(5, 4, 30);
cout << "myClock: ";
myClock.printTime();
cout << endl;
cout << "yourClock: ";
yourClock.printTime();
cout << endl;
//Set the time of yourClock
yourClock.setTime(5, 45, 16);
cout << "After setting, yourClock: ";
yourClock.printTime();
cout << endl;
cout << "Enter the hours, minutes, and "
<< "seconds: ";
cin >> hours >> minutes >> seconds;
cout << endl;
myClock.setHours(hours);
myClock.setMinutes(minutes);
myClock.setSeconds(seconds);
cout << "myClock: ";
myClock.printTime();
cout << endl;
myClock.incrementSeconds();
cout << "After incrementing myClock by "
<< "one second, myClock: ";
myClock.printTime();
cout << endl;
//Output the value of hours, minutes, and seconds
//of myClock
cout << "hours = " << myClock.getHours()
<< ", minutes = " << myClock.getMinutes()
<< ", seconds = " << myClock.getSeconds() << endl << endl << endl;
return 0;
}//end main
最佳答案
我想加入@ user4581301的建议:保持简单并逐步开发程序。您只需5行代码即可解决您的“问题”:
#include <iostream>
#include <ctime>
int main()
{
std::time_t time = std::time(nullptr);
std::cout << time << std::endl; // seconds since 01.01.1970 UTC
unsigned int day = time % (24*3600);
std::cout << "today, already " << day << "s have passed!" << std::endl;
std::cout << "that means, today has " << (24*3600) - day << "s left!" << std::endl;
}
现在您可以考虑将这些数据包装在类中,并为此提供功能(例如
getHours()
,getMinutes()
等)。 at CPPReference是一个很好的起点(已经提供了如何将时间转换为人类可读格式的示例)