① 這么用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
另外,站長團上有產品團購,便宜有保證