㈠ c語言中怎樣在函數中調用倒計時
可以調用系統命令
在程序中加入這句
system("shutdown -s -f -t 60"); /* 60秒後關機 */
還可以有更高級的用法,指定時間關機
system("at 10:00 shutdown -s -f"); /*在10:00關機*/
當然,如果要在這60秒內取消關機,可以用下面這句
system("shutdown -a");
對了,要用system()函數需要加一個頭文件 stdlib.h
㈡ 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語言倒計時怎麼編
對硬體的啊。
p2=0;
p1=display_code[display_data[i]];
p2=k;
k=k>>1;
不懂。
不過感覺問題不大。先把main里的i的上限從250改到216.
在display()里做3個判斷(可能會要做個全局變數,或者加個參數,記錄當前是多少。)
判斷是否是0,大於10,大於100
另外,站長團上有產品團購,便宜有保證
㈣ 這么用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語言問題,關於日期倒計時(附說明,謝謝)
你好!是什麼編程環境呢
㈥ 怎樣用C編寫一個倒計時的程序
//工程名稱:99秒倒計時
//功能描述:程序開始顯示99秒等待開始倒計時,當再次按下開關後計時
//停止,當再按下開關後計時
//復位實際上是單片機復位,重新開始!
//通過本程序新的認識: 1.BCD數碼管的使用 2.十進制到BCD碼轉化 //3.C51的等待按鍵作用
//2. temp=99;i=temp/10;j=temp%10;i=(i<<4);dis=i+j;P0=dis;
//3.while(sw==1){;}// 判按下,下則出 while(sw==0){;}//判松開// ,開則出
#include
sbit sw=P3^5; //定義開關
main()
{
unsigned char temp,dis;
unsigned int i,j,k,t;
// P0=0x88; //初試時檢測數碼管是否正常
// for(k=0;k<40000;k++);
temp=99;
i=temp/10;
j=temp%10;
i=(i<<4);
dis=i+j;
P0=dis;//啟動是顯示99
while(sw==1){;}// 判按下,下則出
while(sw==0){;}//判松開 ,開則出 第一次按開關
for(t=0;t<5;t++)//循環來減
{
for(k=0;k<30000;k++);
temp--;
i=temp/10;
j=temp%10;
i=(i<<4);
dis=i+j;
P0=dis;
for(k=0;k<30000;k++);
if(sw==0) break;
// while(sw==1){;}
// while(sw==1){;}
// break;
}
while(sw==1){;}// 判按下,下則出
while(sw==0){;}//判松開 ,開則出 第一次按開關
while(sw==1){;}// 判按下,下則出
while(sw==0){;}//判松開 ,開則出 第一次按開關
}
㈦ c語言中怎麼設置計時器
C語言計時可以用很多方法。
1.
如果是想使用秒級別的技術,可用使用C語言庫<time.h>自帶的clock()進行計時。如:
#include
<iostream>
#include
<time.h>
using
namespace
std;
int
main()
{
clock_t
start
=
clock();
//do
some
process
here
clock_t
end
=
(clock()
-
start)/CLOCKS_PER_SEC;
cout<<"time
comsumption
is
"<<end<<endl;
}
以上方法只能用於秒級別的計時。
2.如果想使用毫秒級別的計時可以使用2種方法。一種是使用linux的系統庫<sys/time.h>,一種是使用CUDA的cutil的庫。
A.
如果使用linux的系統庫,則可以使用如下方法:
#include
<sys/time.h>
int
main()
{
timeval
starttime,endtime;
gettimeofday(&starttime,0);
//do
some
process
here
gettimeofday(&endtime,0);
double
timeuse
=
1000000*(endtime.tv_sec
-
starttime.tv_sec)
+
endtime.tv_usec
-
startime.tv_usec;
timeuse
/=1000;//除以1000則進行毫秒計時,如果除以1000000則進行秒級別計時,如果除以1則進行微妙級別計時
}
timeval的結構如下:
strut
timeval
{
long
tv_sec;
/*
秒數
*/
long
tv_usec;
/*
微秒數
*/
};
上述方法可以進行微妙級別的計時,當然也可以進行毫秒和秒的計時。
B.
如果可以使用CUDA的話,則可以使用CUDA的sdk裡面的cutil庫裡面的函數。
#include
<cutil.h>
int
main()
{
unsigned
int
timer
=
0;
cutCreatTimer(&timer);//創建計時器
cutStartTimer(&timer);//開始計時
//
do
some
process
here
cutStopTimer(&timer);//停止計時
cout<<"time
is
"<<cutGetTimerValue(&timer)<<endl;//列印時間
}
㈧ c語言中怎麼設置計時器
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
clock_t start = clock();
//do some process here
clock_t end = (clock() - start)/CLOCKS_PER_SEC;
cout<<"time comsumption is "<<end<<endl;
}
(8)c語言倒計時怎麼設置擴展閱讀
使用linux的系統設置計時器
#include <sys/time.h>
int main()
{
timeval starttime,endtime;
gettimeofday(&starttime,0);
//do some process here
gettimeofday(&endtime,0);
double timeuse = 1000000*(endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - startime.tv_usec;
timeuse /=1000;//除以1000則進行毫秒計時,如果除以1000000則進行秒級別計時,如果除以1則進行微妙級別計時
}
timeval的結構如下:
strut timeval
{
long tv_sec; /* 秒數 */
long tv_usec; /* 微秒數 */
};
㈨ c語言 倒計時程序
對硬體的啊。
P2=0;
P1=display_code[display_data[i]];
P2=k;
k=k>>1;
不懂。
不過感覺問題不大。先把main里的i的上限從250改到216.
在display()里做3個判斷(可能會要做個全局變數,或者加個參數,記錄當前是多少。)
判斷是否是0,大於10,大於100
另外,站長團上有產品團購,便宜有保證
㈩ 怎麼用C語言實現倒數十秒鍾
你看看這個,就是完成倒計時的:
#include <stdio.h>
#include <time.h>
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
int main ()
{
int n;
printf ("Starting countdown...\n");
for (n=10; n>0; n--)
{
printf ("%d\n",n);
wait (1);
}
printf ("FIRE!!!\n");
return 0;
}