当前位置:首页 » 编程语言 » 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;
}