一个小程序,每隔1分钟执行1个程序,用秒控制,老是出错

Thirty 2010-05-30 10:42:48
#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秒只打印一次,结果打印了很多次,大家帮我分析下是怎么造成的?我觉得程序控制逻辑没错啊
...全文
379 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
gjlzjb 2010-05-30
  • 打赏
  • 举报
回复
楼主的意思是在每分钟的23秒处执行函数吧,也就是说当23到来的时候执行一次,23-24中间就不再执行,那只要用好表示"ip"即可,如下。

#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;
}
ArtiFly2000 2010-05-30
  • 打赏
  • 举报
回复
最简单就是,执行一次,sleep一分钟,执行下一次
复杂一点的就要定时器

[Quote=引用 9 楼 jackyid 的回复:]
这个程序只是我刚才突然想起用这个方法试试的,如果我要定时执行某函数,一般是怎么处理的呢?
[/Quote]
向立天 2010-05-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 jackyid 的回复:]
这个程序只是我刚才突然想起用这个方法试试的,如果我要定时执行某函数,一般是怎么处理的呢?
[/Quote]
windows下有时钟消息
或者通过多线程处理里
Thirty 2010-05-30
  • 打赏
  • 举报
回复
7L 代码结构比我很棒,不错
Thirty 2010-05-30
  • 打赏
  • 举报
回复
这个程序只是我刚才突然想起用这个方法试试的,如果我要定时执行某函数,一般是怎么处理的呢?
Thirty 2010-05-30
  • 打赏
  • 举报
回复
6L
兄弟比我心细,的确是我粗心了
向立天 2010-05-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jackyid 的回复:]
引用 2 楼 boover 的回复:

一秒钟之内while能执行千万遍,所以打印很多!


C/C++ code
if ( ip==1 &amp;&amp; temp==SEC )
{
temp++;
ip=0;
}

if ( ip==0 &amp;&amp; temp==SEC)
{
Pri……
我感觉我结构还是没啥问题的
[/Quote]
逻辑要是没问题结果就不会有问题
现在你没有得到想要的结果只能说你的逻辑是有问题的
我想你应该是这个意思吧
#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;
}
Boover 2010-05-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jackyid 的回复:]
我感觉我结构还是没啥问题的
[/Quote]

咱们假设现在temp刚好等于23(第一遍),此时ip=0,则第二个if可以执行,打印一遍,ip=1;
循环继续(用时肯定要小于1秒,所以此时temp=tblock->tm_sec仍为23),第一个if执行,temp++,变为24,ip=0
循环继续(用时肯定要小于1秒,所以此时temp=tblock->tm_sec仍为23,虽然刚才temp++了但是一句temp=tblock->tm_sec仍使其为23),所以第二个if仍能执行;
继续如上循环,直到temp=tblock->tm_sec=24,是以打印许多。
diablox0147 2010-05-30
  • 打赏
  • 举报
回复
用WinApi的GetLocalTime然后检查里面的wSecond成员~
Thirty 2010-05-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 boover 的回复:]

一秒钟之内while能执行千万遍,所以打印很多!


C/C++ code
if ( ip==1 && temp==SEC )
{
temp++;
ip=0;
}

if ( ip==0 && temp==SEC)
{
Pri……
[/Quote] 我感觉我结构还是没啥问题的
Thirty 2010-05-30
  • 打赏
  • 举报
回复
其实我也在想是cpu太快的问题
Boover 2010-05-30
  • 打赏
  • 举报
回复
一秒钟之内while能执行千万遍,所以打印很多!


     if ( ip==1 && temp==SEC )
{
temp++;
ip=0;
}

if ( ip==0 && temp==SEC)
{
Print();
ip=1;
}


ip=0和ip=1可以在temp==SEC(即一秒之内)时轮换好多次,是以Print()可以多次执行
fox000002 2010-05-30
  • 打赏
  • 举报
回复
这个只能怪 CPU 太快,一秒内 lz 的代码已经执行不知多少次了

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧