計時器c語言
⑴ c語言 計時器庫函數
在開始時用time()函數取一次時間,在結束時(輸入與生成相同時)再用time()取一回次時間,之後求出再答次時間之差即可。
*************************************************
#include <stdio.h> //for printf()
#include <stdlib.h> //for system()
#include <time.h> //for time() & time_t
void main()
{
time_t ts,te;
system("pause");
ts=time(NULL);
system("pause");
te=time(NULL);
printf("%ld\n",te-ts);
system("pause");
}
/////////////////////////////////////////////
輸出兩次按鍵之間的時間(秒)
⑵ 用c語言怎麼寫秒計時器
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar tt,num;
uchar shi,ge,temp;
uchar code table[]=
{0x3f,0x06,0x5b,0x4f,<br>0x66,0x6d,0x7d,0x07,<br>0x7f,0x6f};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uchar shi,uchar ge)
{
shi=temp/10;
ge=temp%10; P2=table[shi];
P3=0xfe;
delay(1);
P2=0x00; P2=table[ge];
P3=0xfd;
delay(1);
P2=0x00;
}
void timer0() interrupt 1
{
TH0=-50000/256;
TL0=-50000%256;
tt++;
if(tt>=20)
{
tt=0;
temp++;
if(temp==60)
temp=0;
}
}
void init()
{
TMOD=0x01;
TH0=-50000/256;
TL0=-50000%256;
EA=1;
ET0=1;
TR0=1;
tt=0;
temp=0;
}
void main()
{
init();
while(1)
{
display(shi,ge);
}
} 這個是60秒的,供參考
⑶ C語言程序運行計時
使用time()函數。它在頭文件time.h中
具體使用方法如下:
time_t a,b;//time_t是表示時間的結構體,你可以在time.h中找到它的原型。
a=time(NULL);//表示獲取當前的機器時間。
代碼段
b=time(NULL);//表示獲取當前的機器時間。
a是代碼段執行前的時間,b是代碼段執行後的時間(單位是秒),那麼b-a當然是代碼段的執行時間了。輸出時,以長整型輸出時間。
希望這個解答可以幫到你。
⑷ 請問怎麼用c語言怎麼寫一個最簡單計時器
#include<stdio.h>
#include<windows.h>
intmain()
{
inthour=0,min=0,sec=0;
while(1){
回Sleep(1000);//暫停1s
system("cls");//清屏答
sec++;
if(sec==60){
min++;
sec=0;
}
if(min==60){
hour++;
min=0;
}
if(hour==24){
hour=0;
}
printf("%02d:%02d:%02d ",hour,min,sec);
//%02d輸出長度為2,不足2前面補0
}
return0;
}
運行截圖
⑸ 求c語言計時器
#include<stdio.h>
#include<time.h>
intmain(intargc,char*argv[])
{
charbutton=0;
time_tstart=0,end=0;
while(1)
{
button=getchar();
if('A'==button)
{
if(0==start)
{
start=time(NULL);
printf("開始計時%s ",ctime(&start));
}
else
{
printf("不能重復計時 ");
}
}
elseif('B'==button)
{
if(0==start)
{
printf("還未開始計時 ");
}
else
{
end=time(NULL);
printf("結束計時%s ",ctime(&end));
break;
}
}
}
return0;
}
⑹ 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;
}
(6)計時器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語言編程計時器如何工作
C語言編程計時器,參考思路如下:
每隔30秒(自己定)操作文件file:
1、file文件里只存一個數(初值為0);
2、每隔30秒打開文件,讀出數字;
3、數字加上30秒;
4、再寫進去;
在"Dos.h"中有void sleep(unsigned seconds)函數;
Sample:
#include "Dos.h"
#include "stdio.h"
void main(){
while(1){
sleep(yourTime);
doSomething(...);
}
}
⑻ 用c語言編寫計時器,具體如下
#include
#include
#include time.h>
#include
#include
static int count = 0;
static struct itimerval oldtv;
void set_timer()
{
struct itimerval itv;
itv.it_interval.tv_sec = 1;
itv.it_interval.tv_usec = 0;
itv.it_value.tv_sec = 1;
itv.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &itv, &oldtv);
}
void signal_handler(int m)
{
count ++;
printf("%d\n", count);
}
int main()
{
signal(SIGALRM, signal_handler);
set_timer();
while(count < 10000);
exit(0);
return 1;
}
⑼ 怎麼用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語言計時器的例子
這樣就行了:
#include<stdio.h>
#include<time.h>
void funcA()
{
int i=0,j=0;
for(i=0;i<10000;i++)
for(j=0;j<10000;j++)
{
;
}
}
void main()
{
clock_t start,finish;
double ration;
printf("funcA() START!!\n");
start=clock();
funcA();
finish=clock();
printf("funcA() END!!\n");
ration=(double)(finish-start)/CLOCKS_PER_SEC;
printf("%.0f 毫秒\n",ration*1000);
}