学习C++中的数字时钟

436 阅读3分钟

C++中的数字时钟

与模拟钟相比,数字钟是以数字方式(以数字或其他符号)显示时间的。在本教程中,我们将使用C++开发一个数字时钟。

Bjarne Stroustrup于1979年在贝尔实验室工作时开发了C++。由于它混合了高级和低级语言的特点,C++被认为是一种中等语言。

前提条件

要跟上本教程,读者应该具备以下条件。

  • 一些关于C++编程语言的基本知识。
  • 安装了一个IDE。

项目概述

条件和循环是两个重要的C++概念,我们将在建立数字时钟时进行练习。

我们将使用time() 函数来获取本地时间。

为了初始化我们的变量,我们将使用tm 结构,它包含数据和时间特征。

让我们把数字钟程序分解成更小的步骤,使其更容易理解和完成。

必须实现以下操作。

  • 使用time() 方法来确定当前的系统时间。
  • 用小时、分钟和秒的声明初始化tm 结构。
  • 使用while 循环显示数字钟上的当前时间。
  • 根据当前情况和输入的内容,增加小时、分钟和秒的变量。
  • 增加一个延迟,然后从屏幕上删除内容。

获得当前的系统时间

我们使用以下程序来获取当前时间。

  • 使用C++中时间库的time() 方法。它提供了一个以当前时间为值的时间类型的对象。

  • 使用localtime() 方法将时间转换为一个tm 标识符。标识符是一个用来指代一类对象的名称。

  • 声明一个timePtr 类型的指针,以保存由localtime() 函数返回的值。

  • tm 类型允许我们使用诸如tm sec,tm min,tm hour, 等特征来操作时间。

下面的语法被用来检索本地时间。

time_t t = time(NULL);
    tm *timePtr = localtime(&t)

利用结构属性

箭头运算符可用于检索timeptr 的属性。

将时间sec 属性设置为你声明的sec 变量的值。

在声明另一个同名的变量之前,用tm min 属性初始化该变量min

使用tm 小时属性,将小时变量设置为零。然后,声明一个AM/PMtimestr 变量。

下面的代码使用指针将本地时间存储在变量中。if 条件被用来将本地时间改为12小时的时钟格式。

    time_t t = time(NULL);
    tm *timePtr = localtime(&t);

    int seconds = (timePtr->tm_sec);
    int minutes = (timePtr->tm_min);
    int hrs = (timePtr->tm_hour);

显示数字时钟。

while (true)
    {
        system("cls");

        cout << "The digital time is:";

        cout << "      |" << hrs << " : " << minutes << " : " << seconds << " " << endl;
    }

由于我们已经创建了我们的数字钟,下一步就是改变我们数字钟上的时间。

如何增加时间

按照下面的程序来增加我们数字时钟的时间。

  • 在while循环的每一次迭代中增加sec 变量。
  • 一旦sec 的值达到60 ,就把min 变量增加到one 。将sec 复位为1。
  • 用同样的方法,当min 达到60 ,用one 递增小时数,将min 变量重置为0
  • 当小时数达到24时,将其设置为00 。这是因为24小时制的标准时间范围是一到二十四小时。

使用下面的代码来完成时间的递增。

 while (true)
    {
        // This increases the seconds
        sec++;
        if (seconds >= 60)
        {
            seconds = 1;
            minutes++;
        }
        // This increases the minutes
        if (minutes >= 60)
        {
            minutes = 0;
            hrs++;
        }
        // This increases the hours
        if (hrs >= 24)
        {
            hrs = 00;
        }
    }

递增之后,最后一步是增加一个延迟,并同时清除屏幕。

为了实现这一功能,我们将使用以下步骤。

  • 使用system(cls) 来清除view
  • 我们将使用sleep() 函数添加一个1000 ms的延迟。
while (true)
    {
        system("cls");

        cout << "The digital time is:";

        cout << "      |" << hrs << " : " << minutes << " : " << seconds << " " << endl;
        //increment sec min and hours
        sec++;
        if (seconds >= 60)
        {
            seconds = 1;
            minutes++;
        }
        // This increases the minutes
        if (minutes >= 60)
        {
            minutes = 0;
            hrs++;
        }
        // This increases the hours
        if (hrs > 24)
        {
            hrs = 00;
        }

        Sleep(1000);
    }

我们项目的完整演示

下面是数字时钟应用程序的完整代码。

#include <iostream>
#include <ctime>
#include <windows.h>
using namespace std;

int main(){
    time_t t = time(NULL);
    tm *timePtr = localtime(&t); // stores the local time of the computer.

    int seconds = (timePtr->tm_sec);
    int minutes = (timePtr->tm_min);
    int hrs = (timePtr->tm_hour);


while (true){
        system("cls");

        cout << "The digital time is :";
        // This output the message "The digital time is :"

        cout << "      |" << hrs << " : " << minutes << " : " << seconds << " " << endl;
        //increment sec min and hours
        seconds++;
        if (seconds >= 60)
        {
            seconds = 1;
            minutes++;
        }
        // This increases the minutes
        if (minutes >= 60)
        {
            minutes = 0;
            hrs++;
        }
        // This increases the hours
        if (hrs > 24)
        {
            hrs = 00;
        }

        Sleep(1000);
    }

    return 0;
}

输出将是。

The digital clock

请注意,显示的时间将因你的位置而异。

总结

在上面的教程中,我们已经学会了创建一个数字时钟所需的步骤。我们还获得了一些关于使用if 语句和while 循环的知识。