用C语言编写的一种秒表,用于捕捉当前时间

用C语言编写的一种秒表,用于捕捉当前时间,c,timer,C,Timer,我想用C语言制作一个秒表来记录钟摆的时间周期。虽然此处未显示,i是一个距离计数器,但传感器测量它与摆锤之间的距离。所以我的目标是当摆锤和传感器之间的距离小于或等于5时启动计时器。在该距离处,摆锤直接位于传感器上方。然后,当钟摆回到传感器上时,再次启动计时器 这样做会给我一个时间段。在我的尝试中,我决定使用库获取摆锤打开和关闭传感器时的当前时间,然后用减法获得时间段 我的代码如下: #include <stdio.h> #include <stdlib.h> #includ

我想用C语言制作一个秒表来记录钟摆的时间周期。虽然此处未显示,
i
是一个距离计数器,但传感器测量它与摆锤之间的距离。所以我的目标是当摆锤和传感器之间的距离小于或等于5时启动计时器。在该距离处,摆锤直接位于传感器上方。然后,当钟摆回到传感器上时,再次启动计时器

这样做会给我一个时间段。在我的尝试中,我决定使用
库获取摆锤打开和关闭传感器时的当前时间,然后用减法获得时间段

我的代码如下:

#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.

}

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

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

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

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

我试图修复您的代码,如下所示:

#包括
#包括
#包括
struct timespec sub_tspec(struct timespec minuted,struct timespec subtrahend)
{
结构timespec结果;
如果(subtrahand.tv\u nsec>minuted.tv\u nsec){
result.tv_sec=minuted.tv_sec-subtrahand.tv_sec-1;
result.tv\u nsec=(minuend.tv\u nsec+1e9)-subtrahand.tv\u nsec;
}否则{
result.tv_sec=minuted.tv_sec-subtrahand.tv_sec;
result.tv\u nsec=minuted.tv\u nsec-subtrahand.tv\u nsec;
}
返回结果;
}
int main()
{
int i;
struct timespec timePeriod;
结构timespec tspec1、tspec2;
时间与时间;
对于(i=0;i<20;i++){

如果(我没问题,我会更改答案为0的答案,因为我的结果显示没有时间差。我认为
时间
没有您需要的粒度。好的,您建议我改用什么?
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.