24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 737  |  回复: 2
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

gghgbsl8768

实习版主

优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!

[求助] 倒计时器

求助倒计时器算法,用C语言实现,谢谢!
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

curton

新虫

Hello Kitty

★ ★
nono2009:编辑内容 2011-05-30 16:24
nono2009:编辑内容 2011-05-30 16:25
nono2009(金币+2): 谢谢应助。今后建议用[code][/code]贴代码,以免与表情字符冲突。 2011-05-30 16:26:10
秒表计时器的代码
CODE:
#include   
#include
#include
#include   
struct tm     //定义时间结构体,包括时分秒和10毫秒
{
int hours,minutes,seconds;
int hscd;
}time,tmp,total;    //time用以计时显示,tmp用以存储上一阶段时间,total记总时间
int cnt;

FILE* fout;
//每次调用update函数,相当于时间过了10ms
void update(struct tm *t)   
{

(*t).hscd++;    //10ms单位时间加1  
cnt++;
if ((*t).hscd==100)   //计时满1s,进位
{  
  (*t).hscd=0;  
  (*t).seconds++;  
}  
if ((*t).seconds==60)   //计时满一分,进位
{  
  (*t).seconds=0;  
  (*t).minutes++;  
}  
if ((*t).minutes==60)        //计时满一小时,进位

{  
  (*t).minutes=0;  
  (*t).hours++;  
}  
if((*t).hours==24) (*t).hours=0;  
//delay();
Sleep(10);  //Sleep是windows提供的函数,作用是暂停程序,单位毫秒,所以此处暂停10ms
}  
void display(struct tm *t)  
{  
//此处输出计时结果,\r为回车不换行,既一直在同一行更新时间
printf("%d:",(*t).hours);  
printf("%d:",(*t).minutes);  
printf("%d:",(*t).seconds);
printf("%d\r",(*t).hscd);  
//printf("Now, press ‘e’ key to stop the clock…");  
}  
void time_init()  //初始化时间
{
time.hours=time.minutes=time.seconds=time.hscd=0;
}
void get_total()   //计算总时间
{
total.hscd = cnt % 100;
cnt /= 100;
total.seconds = cnt % 60;
cnt /= 60;
total.minutes = cnt % 60;
cnt /= 60;
total.hours = cnt;
}
int main()  
{
char m;   
time_init();
cnt = 0;
fout =  fopen("timeout.txt","w");   
printf("按回车键开始计时!\n");
while(1)
{
  m = getch();
  if(m != ‘\r’)     //读入一个输入,如果是回车,那么跳出次循环
   printf("输入错误,仅能输入回车键!\n");
  else
   break;
}
printf("已经开始计时,但是你可以按回车键以分段计时!\n");
while(1)
{
  if(kbhit())    //此处检查是否有键盘输入
  {
   m=getch();  
   if(m == ‘\r’)     //如果等于回车,那么计时结束,跳出循环
    break;
   else if(m == ‘ ‘)  //如果等于空格,显示此次计时,初始化计时器
   {
    tmp = time;      //记录上一段计时器结果
    fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd); //写入文件
    time_init();
    printf("\n");
   }
   else
   {
    printf("输入错误,仅支持输入回车键或者空格键!\n");
   }  
  }
  update(&time);     //更新计时器
  display(&time);    //显示计时器时间
}
tmp = time;       //输出最后一次即使结果,写入文件
fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);
get_total();      //计算总的时间,显示,并写入文件
printf("\n总时间:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd);
fprintf(fout,"统计时间:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd);
fclose(fout);
printf("已经保存到当前目录下的timeout.txt文件中按任意键结束!");
getch();  

}

http://zhidao.baidu.com/question/193652836.html

[ Last edited by nono2009 on 2011-5-30 at 16:25 ]
3楼2011-05-15 18:21:43
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 3 个回答

vis-kaka

管理员

优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!

【答案】应助回帖

gghgbsl8768(金币+1): 谢谢回帖 2011-05-21 12:24:33
不间断的去获取系统时间,然后与你设定的时间进行比较
2楼2011-05-15 12:49:24
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 311(085601)求调剂 +11 liziyeyeye 2026-03-28 12/600 2026-03-31 23:00 by wwytracy
[考研] 267求调剂 +9 uiybh 2026-03-31 9/450 2026-03-31 22:15 by 天倌赐福
[考研] 调剂 +4 好好读书。 2026-03-28 6/300 2026-03-31 20:35 by 好好读书。
[考研] 318求调剂 +3 笃行致远. 2026-03-31 3/150 2026-03-31 20:27 by 求调剂zz
[考研] 0856求调剂 +9 楒桉 2026-03-28 9/450 2026-03-31 19:06 by 暮泽12
[考研] 346求调剂 一志愿070303有机化学 +11 萝卜炖青菜 2026-03-28 12/600 2026-03-31 17:54 by xhai2011
[考研] 254材料与化工求调剂 +3 翰冬林楠 2026-03-30 4/200 2026-03-31 17:53 by yishunmin
[考研] 材料科学与工程调剂 +13 深V宿舍吧 2026-03-30 14/700 2026-03-31 16:28 by hypershenger
[考研] 347求调剂 +11 山顶见α 2026-03-25 11/550 2026-03-31 14:14 by 记事本2026
[考研] 土木304求调剂 +3 顶级擦擦 2026-03-31 3/150 2026-03-31 13:59 by 西京学院招办
[考研] 081200-11408-276学硕求调剂 +4 崔wj 2026-03-31 4/200 2026-03-31 11:56 by jp9609
[考研] 266求调剂 +3 哇呼哼呼哼 2026-03-29 3/150 2026-03-31 10:06 by cal0306
[考研] 本科211总分289,08工学真心求调剂 +3 utopiaE 2026-03-30 3/150 2026-03-30 23:42 by ms629
[考研] 调剂 +4 GK72 2026-03-30 4/200 2026-03-30 20:32 by dick_runner
[考研] 一志愿华中师范化学332分求调剂 +3 Lyy930824@ 2026-03-29 3/150 2026-03-30 20:15 by DHUSHUAI
[考研] 297求调剂 +17 田洪有 2026-03-26 18/900 2026-03-30 18:32 by nothing投稿中
[考研] 材料化工340求调剂 +3 jhx777 2026-03-30 3/150 2026-03-30 17:54 by JourneyLucky
[考研] 348求调剂 +6 小懒虫不懒了 2026-03-28 6/300 2026-03-30 10:29 by Evan_Liu
[考研] 材料与化工304求B区调剂 +4 邱gl 2026-03-26 7/350 2026-03-30 08:39 by 探123
[考研] 305求调剂 +5 哇卢卡库 2026-03-26 5/250 2026-03-27 14:01 by laoshidan
信息提示
请填处理意见