当前位置:首页 » 编程语言 » 倒计时5分钟c语言代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

倒计时5分钟c语言代码

发布时间: 2022-05-13 09:52:57

① 这么用c语言做倒计时器

tdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
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();

}
另外,站长团上有产品团购,便宜有保证

② 求个c语言小代码,很简单的倒计时程序


#include<stdio.h>
#include<windows.h>

intmain(void)
{
inta;
a=0;
boolup=true;
system("color0A");

while(true)
{
system("cls");

printf("%d a",a);
Sleep(100);
if(up)
{
a++;
}
if(!up)
{
a--;
}
if(a==0)
{
up=true;
}
if(a==100)
{
up=false;
}

}

return0;
}

③ c语言 倒计时时钟程序

如果你有TC,可以试一试。
VC 的 kbhit() 不能返回 键名。
23:59:59 -- 86400 秒

#include <stdio.h>
#include <windows.h>
#include <conio.h>

void main()
{
long int t;
int flag =0;
t = 86400;

printf("press S start, press E stop\n");
while(1)
{
if (kbhit() == 'S' || kbhit() == 's') flag =1;
if (kbhit() == 'E' || kbhit() == 'e') flag =0;
Sleep(1000);
if (flag == 1) t = t -1;
if (t <= 0) break;
printf("%d ",t);
}

④ c语言倒计时器 的编程代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ;
while (clock() < endwait) {}
}

void main(){
int t,m,s;
printf("input counterdown time in seconds\n");
scanf("%d",&t);
printf("\n===================\n");
while(1)
{
wait ( 1 );
t--;
if (t==0) break;
s = t % 60;
m = t / 60;
printf("\r\t%02d:%02d",m,s);
}
exit(0);
};

⑤ 求个c语言小代码,很简单的分钟倒计时程序

//有很多种方法,我这里用的是其中一种:

#include<stdio.h>
#include<Windows.h>
intmain(void){
intminutes;
printf("请输入分钟数: ");
scanf("minutes");
for(;minutes>=0;--minutes){
for(inti=60;i>=0;--i){
printf("还剩%d分钟%d秒结束 ",minutes,i);//输出剩余时间
Sleep(1000);//延时函数延时一秒
};
};
printf("计时结束 ");
system("PAUSE");
return0;
};
//我没上编译器测试,但是具体架构就是这样

⑥ c语言 制作 倒计时

#include"stdio.h"

#include"Windows.h"

intmain(){

printf("请输入倒计时时间(例如:01:26:30):");

inthour=0,min=0,sec=0;

scanf("%d:%d:%d",&hour,&min,&sec);

if(hour>24||hour<0||min>60||min<0||sec>60||sec<0){

printf("输入有误! ");

return0;

}

printf("倒计时开始! ");

inti,j,k;

for(i=hour;i>=0;i--){

for(j=min;j>=0;j--){

for(k=sec;k>=0;k--){

printf(" %2d:%2d:%2d",i,j,k);

Sleep(1000);

}

sec=59;

}

min=59;

}

exit(0);

}

⑦ C语言高手看过来,2分钟倒计时代码例如2:00 1:59

main()
{inti=2;
intj=0;
while(1)
{
system("CLS");
printf("%d:%d%d ",i%3,(j%60)/10,j%10);
getch();//手动,延时自己加,有误差不大,
//sleep(1000);
j--;
if(j<0){j=59;i--;if(i<0)break;}
}
getch();
}

⑧ 倒计时器代码

程序设计思想:
(1)输入目标时间,高考的年,月,日,时,分,秒
下面例子中简写成直接赋值。
(2)转换成 struct tm
(3)再转换成 time_t
(4) 获当前时间 now = time (NULL);
(5)用difftime 计算时间差,送返 long double 秒
(6)把秒转换成 日,时,分,秒
(7)循环 (下面例子中简写成 打印120次,每隔2秒左右打一次)

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

void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ;
while (clock() < endwait) {}
}

void main(){
time_t rawtime;
struct tm * target_time;
int d,h,m,s;
int k=0;
long double dif,r;
time_t now,t_end; // in sec

/* get current timeinfo and modify it to the user's choice */
time ( &rawtime );
target_time = localtime ( &rawtime );

// time struc and to time_t
target_time->tm_year = 2008 - 1900; //year - 1900
target_time->tm_mon= 7 - 1; // month - 1
target_time->tm_mday = 15 ; // day
target_time->tm_hour = 13 ; // hour
target_time->tm_min = 1 ;
target_time->tm_sec = 1 ;
t_end = mktime (target_time);
// printf("%s ",ctime(&t_end)); //print and check

while (k < 120)
{
now = time (NULL);
// printf("%s ",ctime(&now)); // print and check
dif = difftime (t_end,now); //double , time_t time_t
// printf( "%lf\n",dif);
d = (int) (dif / 86400.0);
r = dif - d * 86400.0;
h = (int) (r / 3600.0);
r = r - h * 3600.0;
m = (int) (r / 60.0);
r = r - m * 60.0;
s = (int) (r);
printf("%d--days %d--hours %d--min %d--sec\n",d,h,m,s);
(void) wait ( 2 ); // every 2 seconds print
k = k + 1;

};
}

⑨ c语言倒计时怎么编

定位要倒计时的坐标!然后再需要倒计时的地方添加gotoxy(所定位的坐标),当然这只是十以下的倒计时,超过了十则会清除不了十位数上的数字,要解决这个办法就是输出%2d

⑩ c语言 倒计时程序

对硬件的啊。

P2=0;
P1=display_code[display_data[i]];
P2=k;
k=k>>1;
不懂。

不过感觉问题不大。先把main里的i的上限从250改到216.
在display()里做3个判断(可能会要做个全局变量,或者加个参数,记录当前是多少。)
判断是否是0,大于10,大于100
另外,站长团上有产品团购,便宜有保证