【问题标题】:A stopwatch in C which captures the current timeC中的秒表,用于捕获当前时间
【发布时间】:2020-02-02 01:48:34
【问题描述】:

我正在尝试用 C 语言制作秒表来收集钟摆的时间段。虽然这里没有显示,i 是一个距离计数器,传感器测量它与摆锤之间的距离。所以我的目标是在摆锤和传感器之间的距离小于或等于 5 时启动计时器。在这个距离处,摆锤直接位于传感器上方。然后当钟摆回到传感器上时再次启动计时器。

这样做会给我时间。在我的尝试中,我决定使用<time.h> 库并获取摆锤打开和关闭传感器的当前时间,然后进行减法以获得时间段。

我的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int timeinfo;
int timeinfo1;
int timePeriod;
int main()
{
    int i =0;
    for (i=0;i<20;i++){
        if (i<=5){
            time_t rawtime;
            struct tm * timeinfo;

            time ( &rawtime );
            timeinfo = localtime ( &rawtime );
            printf ( "Current local time and date: %s", asctime (timeinfo) ); //Collecting the current time when i is less than 5

        }
        else{
                time_t rawtime;
                struct tm * timeinfo1;

                time ( &rawtime );
                timeinfo1 = localtime ( &rawtime );
                printf ( "Current local time and date: %s", asctime (timeinfo1) );//Collecting the current time when i is greater than 5

        }
    }
    timePeriod = timeinfo1-timeinfo;
    printf("%s", timePeriod); // Calculating and printing the time period, the initial minus the final time.

}

代码的结果与我的预期不符,如下所示:

Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020 
Current local time and date: Thu Jan 30 13:00:10 2020
(null)
Process returned 6 (0x6)   execution time : 1.604 s
Press any key to continue.

很明显,每次迭代的值没有时间变化,最终的字符串值返回为空。我认为这是因为不可能减去字符串,但我不确定。

【问题讨论】:

  • 好的,我改一下
  • 答案为 0,因为我的结果显示没有时差
  • 我认为time 没有你需要的粒度。
  • 好的,你建议我改用什么?

标签: c timer


【解决方案1】:

首先你将'timeinfo'和'timeinfo1'定义为全局整数变量,然后在代码中将它们重新定义为局部变量,为什么?

其次,您使用的函数 (localtime) 以秒的粒度为您提供结果,因为 for 循环执行得非常快,您 - 很可能 - 永远不会得到大于 0 或 1 的结果。因此我建议使用 clock_gettime函数,以便您可以获得高达纳秒的分辨率。

第三个问题是使用 (%s) 打印整数的方式

我尝试修复您的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct timespec sub_tspec(struct timespec minuend, struct timespec subtrahend)
{
    struct timespec result;

    if (subtrahend.tv_nsec > minuend.tv_nsec) {
        result.tv_sec = minuend.tv_sec - subtrahend.tv_sec - 1;
        result.tv_nsec = (minuend.tv_nsec + 1e9) - subtrahend.tv_nsec;
    } else {
        result.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
        result.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
    }

    return result;
}

int main()
{
    int i;
    struct timespec timePeriod;
    struct timespec tspec1, tspec2;
    time_t rawtime;

    for (i = 0; i < 20; i++) {
        if (i <= 5) {
            clock_gettime(CLOCK_REALTIME, &tspec1);
            printf("Current time: %ld.%ld\n", tspec1.tv_sec, tspec1.tv_nsec); //Collecting the current time when i is less than 5

        } else {
            clock_gettime(CLOCK_REALTIME, &tspec2);
            printf("Current time: %ld.%ld\n", tspec2.tv_sec, tspec2.tv_nsec); //Collecting the current time when i is greater than 5
        }
    }

    timePeriod = sub_tspec(tspec2, tspec1);
    printf("%ld.%ld\n", timePeriod.tv_sec, timePeriod.tv_nsec); // Calculating and printing the time period, the initial minus the final time.

    return 0;
}

您可以在此处找到有关 clock_gettime 的更多详细信息:https://linux.die.net/man/3/clock_gettime

【讨论】:

    猜你喜欢
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多