c++ - 我怎样才能让我的程序以秒为单位返回时钟一天中耗时?

标签 c++

我想实现以下功能:

以秒为单位返回时钟一天中耗时。

以秒为单位返回时钟当天的剩余时间。

确定并输出两个时钟的时间间隔。以小时:分钟:秒的形式输出时间。

到目前为止我的代码是:

时钟类型.h

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 (已经提供了如何将时间转换为人类可读格式的示例)

关于c++ - 我怎样才能让我的程序以秒为单位返回时钟一天中耗时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54413870/

相关文章:

c++ - 在C++中分割偶数字符串

c++ - CUDA 中图像行/列的线程索引

c++ - 使用 C++ 进行简单的字符串解析

c++ - 如何获取 std::string 中的字符数?

c++ - DirectX : Mesh displaying occluded faces

c++ - C++使用删除的函数错误进行条件初始化

c++ auto 不命名类型

c++ - bool 表达式中的整数溢出

C++ 语义类型包装

c++ - "class ClassName;"放在其他类定义的头文件中是什么意思?