⑴ c語言倒計時怎麼編
定位要倒計時的坐標!然後再需要倒計時的地方添加gotoxy(所定位的坐標),當然這只是十以下的倒計時,超過了十則會清除不了十位數上的數字,要解決這個辦法就是輸出%2d
⑵ C語言程序運行計時
可以使用time.h裡面的clock函數
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
long i = 10000000L;
clock_t start, finish;
double ration; /* 測量一個事件持續的時間*/
printf( "Time to do %ld empty loops is ", i) ;
start = clock();
while(i--);
finish = clock();
ration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%f seconds\n", ration );
system("pause");
return 0;
}
⑶ 怎麼用c語言編寫一個計時器!!!
調用win的api函數ExitWindowsEx();
#include <stdio.h>
#include <time.h>
main()
{
clock_t start,end;
int n;
printf("How many seconds do you want to count? ");
scanf("%d",&n);
getchar();
clrscr();
start=end=clock();
while((n-(int)(end-start)/19)>=0&!kbhit())
{
printf("the time is: %d",n-(int)(end-start)/19);
sleep(1);
end=clock();
clrscr();
}
ExitWindowsEx();
}
循環結束就可以了。
網上幫你找了下。
頭文件: 在Winuser.h中定義;需要包含 Windows.h.
靜態庫: User32.lib.
【以上轉貼】
【以下原創】
#include "ctime" //不要直接編,可能過不了,為C++,只是告知你大概方法
using namespace std;
//我寫的一個類,調用API函數:
// gameTime類,用於時間控制
class gameTime
{
public:
gameTime();
~gameTime();
public:
DWORD tNow; //當前時間
DWORD tPre; //開始時間
private:
bool key; //運行控制
public:
bool getKey() { return key; }
void setKey( bool temp ) { key = temp; }
DWORD getTimeDelay(){ return (tNow - tPre);}
};
/**-----------------------------------------------------------------------------
* gameTime類實現段
*------------------------------------------------------------------------------
*/
gameTime::gameTime():tNow(0),tPre(0),key(false)
{
}
gameTime::~gameTime()
{
}
//原理是開始計時時:
tPre = GetTickCount();
///....執行。
gameStartTime.tNow = GetTickCount();
if(gameStartTime.getTimeDelay()>= 72000)
............
//在72S內做什麼什麼。。。
這個是控制時間間隔的。
⑷ 秒錶計時程序要求(需要用C語言實現)
這里的分段計時,我使用空格鍵實現的,F2比較麻煩。程序開始,輸入回車開始計時,中途輸入空格可以開始新的計時,最後輸入回車完成計時。
文件存在程序目錄下的timeout.txt
真麻煩,下次這種求助才給10分,絕對不做。。。
//////////////////////////
我的代碼就是在VS2010下寫的。。。怎麼會無法編譯。。。你要建一個空工程,然後加入C++源文件。
/////////////////////////////
請新建一個空工程,不要新建Win32Console那個工程!
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<stdlib.h>
structtm//定義時間結構體,包括時分秒和10毫秒
{
inthours,minutes,seconds;
inthscd;
}time,tmp,total;//time用以計時顯示,tmp用以存儲上一階段時間,total記總時間
intcnt;
FILE*fout;
//每次調用update函數,相當於時間過了10ms
voipdate(structtm*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
}
voiddisplay(structtm*t)
{
//此處輸出計時結果, 為回車不換行,既一直在同一行更新時間
printf("%d:",(*t).hours);
printf("%d:",(*t).minutes);
printf("%d:",(*t).seconds);
printf("%d ",(*t).hscd);
//printf("Now,press'e'keytostoptheclock...");
}
voidtime_init()//初始化時間
{
time.hours=time.minutes=time.seconds=time.hscd=0;
}
voidget_total()//計算總時間
{
total.hscd=cnt%100;
cnt/=100;
total.seconds=cnt%60;
cnt/=60;
total.minutes=cnt%60;
cnt/=60;
total.hours=cnt;
}
intmain()
{
charm;
time_init();
cnt=0;
fout=fopen("timeout.txt","w");
printf("Now,pressEnterkeytobegintheclock... ");
while(1)
{
m=getch();
if(m!=' ')//讀入一個輸入,如果是回車,那麼跳出次循環
printf("InputError! ");
else
break;
}
printf("Whilecounting,! ");
while(1)
{
if(kbhit())//此處檢查是否有鍵盤輸入
{
m=getch();
if(m==' ')//如果等於回車,那麼計時結束,跳出循環
break;
elseif(m=='')//如果等於空格,顯示此次計時,初始化計時器
{
tmp=time;//記錄上一段計時器結果
fprintf(fout,"%d:%d:%d:%d ",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);//寫入文件
time_init();
printf(" ");
}
else
{
printf("InputError! ");
}
}
update(&time);//更新計時器
display(&time);//顯示計時器時間
}
tmp=time;//輸出最後一次即使結果,寫入文件
fprintf(fout,"%d:%d:%d:%d ",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);
get_total();//計算總的時間,顯示,並寫入文件
printf(" totaltime:%d:%d:%d:%d ",total.hours,total.minutes,total.seconds,total.hscd);
fprintf(fout,"totaltime:%d:%d:%d:%d ",total.hours,total.minutes,total.seconds,total.hscd);
fclose(fout);
getch();
}
⑸ 在C語言中如何實現精確計時
time()
頭文件:time.h
函數原型:time_t time(time_t * timer)
功能:返回以格林尼治時間(GMT)為標准,從1970年1月1日00:00:00到現在的此時此刻所經過的秒數。
2.clock()
頭文件:time.h
函數原型:clock_t clock(void);
功能:該函數返回值是硬體滴答數,要換算成秒,需要除以CLK_TCK或者 CLK_TCKCLOCKS_PER_SEC。比如,在VC++6.0下,這兩個量的值都是1000。
3. timeGetTime()
頭文件:Mmsystem.h 引用庫: Winmm.lib
函數原型:DWORD timeGetTime(VOID);
功能:返回系統時間,以毫秒為單位。系統時間是從系統啟動到調用函數時所經過的毫秒數。注意,這個值是32位的,會在0到2^32之間循環,約49.71天。
⑹ C語言寫出計時器
boolsetTime1(inth,intm,ints)//倒計時
{
if((h<0)||(m<0||m>=60)||(s<0||s>=60))
{
printf("Timeseterror! ");
returnfalse;
}
while(h>0||m>0||s>0)
{
printf("%02d:%02d:%02d",h,m,s);
--s;
if(s<0)
{
s=59;
--m;
if(m<0)
{
m=59;
--h;
}
}
Sleep(1000);
system("cls");
}
returntrue;
}
boolsetTime2(inth,intm,ints)//正計時
{
inthour=0,min=0,sec=0;
if((h<0)||(m<0||m>=60)||(s<0||s>=60))
{
printf("Timeseterror! ");
returnfalse;
}
while(h!=hour||m!=min||s!=sec)
{
printf("%02d:%02d:%02d",hour,min,sec);
++sec;
if(sec==60)
{
sec=0;
++min;
if(min==60)
{
min=0;
++hour;
}
}
Sleep(1000);
system("cls");
}
returntrue;
}