69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <time.h>
#define SEC 23 //在每分钟23秒处执行函数
int num=1;
void Print()
{
printf ( "-----------program run times=%d------------\n",num++ );
}
int main()
{
time_t timer;
struct tm *tblock;
int temp,ip;
ip=0;
while ( 1 )
{
timer = time ( NULL ); //取时间
tblock = localtime ( &timer );
temp=tblock->tm_sec;
if ( ip==1 && temp==SEC )
{
temp++;
ip=0;
}
if ( ip==0 && temp==SEC)
{
Print();
ip=1;
}
}
return 0;
}
我预想应该每个23秒只打印一次,结果打印了很多次,大家帮我分析下是怎么造成的?我觉得程序控制逻辑没错啊
#include <stdio.h>
#include <time.h>
#define SEC 23 //在每分钟23秒处执行函数
int num=1;
void Print()
{
printf ( "-----------program run times=%d------------\n",num++ );
}
int main()
{
time_t timer;
struct tm *tblock;
int temp,ip;
ip=0;
while(1)
{
timer = time ( NULL ); //取时间
tblock = localtime ( &timer );
temp=tblock->tm_sec;
if (temp == SEC)
{
if(ip == 0)
{
Print();
ip=1;
}
}
else ip = 0;
}
return 0;
}
#include <stdio.h>
#include <time.h>
#define SEC 23 //在每分钟23秒处执行函数
int num=1;
void Print()
{
printf ( "-----------program run times=%d------------\n",num++);
}
int main()
{
time_t timer;
struct tm* tblock;
int temp, ip;
ip=0;
while(1)
{
timer = time(NULL); //取时间
tblock = localtime(&timer);
temp = tblock->tm_sec;
if(ip==0 && temp==SEC)
{
Print();
ip = 1;
}
if(ip==1 && temp!=SEC)
ip=0;
}
return 0;
}
if ( ip==1 && temp==SEC )
{
temp++;
ip=0;
}
if ( ip==0 && temp==SEC)
{
Print();
ip=1;
}