當前位置:首頁 » 編程語言 » c語言做一個24小時的計時器
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言做一個24小時的計時器

發布時間: 2022-08-05 01:21:43

『壹』 如何在c語言中實現計時

以前做那個停車場管理系統的時候, 也是需要計時,因為要收費.. 好像就這么記得. 每個上機的人,應該有一個結構體,在結構體里設個計時的變數,可以是個只有兩個元素的數組.當然這樣會很不方便了.(因為需要你自己輸入上機時間,和下機時間,並保存在變數里.)
.... ANSIC里有一個time函數,在time.h頭文件里. 這個函數,傳遞一個參數,返回的是系統時間(單位我不清楚),返回的系統時間保存在你傳遞的參數里... 你可以試試這個. 貌似這個可能就有點麻煩了. 因為需要測試程序... 你不可能等個1,2個小時,再看看輸出結果是不是對的...測試的時候,乘個數放大一下應該就可以了..

也就是說,你設一個結構體,裡面有一個記錄時間的數組time[2],數組只含兩個元素, 這兩個元素的值,由time函數來獲得.(這里獲得的是系統時間).

.這個結構體里應該還含有的其他元素,應該要包括,電腦標號ID(每個電腦對應一個號碼),和一個bool型變數status,來標識是該電腦的狀態,已有人上機或者處於空閑狀態.

status為true(有人使用該機器)時,把系統時間付給time[0],
該機器的status變為false(有人下機)後,在把一個系統時間付給time[1].計算時間差和收費額.

.. 那些一個小時,半個小時,等等,不同時間的不同收費標准,一般用if,什麼的來搞定.

『貳』 誰能告訴我51單片機簡單的led數碼管時鍾程序 24小時制的(c語言版的)

#include "reg52.h"
#define uint unsigned int
#define uchar unsigned char
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar shi,fen,miao;
uchar time;
void delay(uint x)
{
uint y;
for(;x>0;x--)
{
for(y=0;y<124;y++);
}
}
void display(uchar shi,uchar fen,uchar miao)
{
P2=0; //位碼
P0=(tab[shi/10]); //段碼
delay(2);
P2=1;
P0=(tab[shi%10]);
delay(2);
P2=2; //位碼
P0=0x40; //段碼
delay(2);
P2=3; //位碼
P0=(tab[fen/10]); //段碼
delay(2);
P2=4;
P0=(tab[fen%10]);
delay(2);
P2=5; //位碼
P0=0x40; //段碼
delay(2);
P2=6; //位碼
P0=(tab[miao/10]); //段碼
delay(2);
P2=7;
P0=(tab[miao%10]);
delay(2);

}
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{

if(time==20)
{
time=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
shi=0;
}

}

}
display(shi,fen,miao);
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
time++;
}

/*還有什麼不明白繼續追加*/

『叄』 誰能幫我寫c語言,是輸入一個24小時制的時間,輸出12小時制的時間!例輸入1605,輸出4:05Pm

#include <stdio.h>
int main(void)
{
int h24;
scanf("%d",&h24);
printf("%d:%02d\t%s\n",h24/100<=12?h24/100:h24/100-12,
h24%100,
h24/100<12?"AM":"PM");
return 0;
}

『肆』 這么用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語言編寫一個計時器!!!

調用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語言中怎麼設置計時器

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 <stdio.h>

int main()
{
int seconds;
for (;scanf("%d",&seconds)==1;)
{
printf("%02d:%02d:%02d\n",seconds / 3600,seconds / 60 % 60,seconds % 60);
}
return 0;
}