
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
unsigned char time1[] = { 10, 8, 31, 9, 26 }
unsigned char time2[] = { 10, 8, 31, 9, 50 }
struct tm t1 = {0}
struct tm t2 = {0}
time_t _t1
time_t _t2
double diff
t1.tm_year = time1[0] + 100
t1.tm_mon = time1[1]
t1.tm_mday = time1[2]
t1.tm_hour = time1[3]
t1.tm_min = time1[4]
t2.tm_year = time2[0] + 100
t2.tm_mon = time2[1]
t2.tm_mday = time2[2]
t2.tm_hour = time2[3]
t2.tm_min = time2[4]
_t1 = _mkgmtime( &t1 )
_t2 = _mkgmtime( &t2 )
diff = difftime(_t2, _t1 )
printf( "相差 %.0f 分钟\n", diff / 60 )
}
扩展资料:time_t time( time_t *t) 与 clock_t clock(void)
头文件: time.h
计算的时间单位分别为: s , ms
time_t 和 clock_t 是函数库time.h 中定义的用来保存时间的数据结构
返回值:滚激
1、time : 返回从公元1970年1月1号的UTC时大纤袜间从0时0分0秒算起到现在所经过的秒数。如果参数 t 非空指针的话,返回的时间会保存在 t 所指向的内存。
2、clock:返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数。 1单元 = 1 ms。
所以我们可以根据具体情况需求,判断采用哪一个函数。
具体用法如下例子:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
time_t c_start, t_start, c_end, t_end
c_start = clock() //!<单位为ms
t_start = time(NULL) //!<单位为s
system("pause")
c_end = clock()
t_end = time(NULL)
//!<difftime(time_t, time_t)返回两个time_t变量间的时间间隔,即时间差
printf("The pause used %f ms by clock()\n",difftime(c_end,c_start))
printf("The pause used %f s by time()\n",difftime(t_end,t_start))
system("pause")
return 0
}
因此,要计算竖侍某一函数块的占用时间时,只需要在执行该函数块之前和执行完该函数块之后调用同一个时间计算函数。再调用函数difftime()计算两者的差,即可得到耗费时间。
c语言时钟程序代码如下:
#include<windows.h>
#include<math.h>
#define ID_TIMER 1//计时器ID
#define TWOPI (2*3.14159)
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM)
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("Clock")
HWND hwnd
MSG msg
WNDCLASS wndclass
wndclass.cbClsExtra=0
wndclass.cbWndExtra=0
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH)
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW)
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION)
wndclass.hInstance=hInstance
wndclass.lpfnWndProc=WndProc
wndclass.lpszClassName=szAppName
wndclass.lpszMenuName=NULL
wndclass.style=CS_HREDRAW|CS_VREDRAW
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires Windows
T"),szAppName,MB_ICONERROR)
return 0
}
hwnd=CreateWindow(szAppName,TEXT("Analog Clock"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL)
ShowWindow(hwnd,iCmdShow)
UpdateWindow(hwnd)
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg)
DispatchMessage(&msg)
}
return msg.wParam
}
void Setsotropic(HDC hdc,int cxClient,int cyClient)
{
SetMapMode(hdc,MM_ISOTROPIC)
SetWindowExtEx(hdc,1000,1000,NULL)
SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL)
SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL)
}
void RotatePoint(POINT pt[],int iNum,int iAngle)
{
int i
POINT ptTemp
for(i=0i<iNumi++)
{
ptTemp.x=(int)(pt[i].x*cos(TWOPI*iAngle/360)+pt[i].y*sin(TWOPI*iAngle/360))
ptTemp.y=(int)(pt[i].y*cos(TWOPI*iAngle/360)+pt[i].x*sin(TWOPI*iAngle/360))
pt[i]=ptTemp
}
}
void DrawClock(HDC hdc)
{
int iAngle
POINT pt[3]
for(iAngle=0iAngle<360iAngle+=6)
{
pt[0].x=0
pt[0].y=900
RotatePoint(pt,1,iAngle)
pt[2].x=pt[2].y=iAngle%5?33:100
pt[0].x-=pt[2].x/2
pt[0].y-=pt[2].y/2
pt[1].x=pt[0].x+pt[2].x
pt[1].y=pt[0].y+pt[2].y
SelectObject(hdc,GetStockObject(BLACK_BRUSH))
Ellipse(hdc,pt[0].x,pt[0].y,pt[1].x,pt[1].y )
}
}
void DrawHands(HDC hdc,SYSTEMTIME *pst,BOOL fChange)
{
static POINT pt[3][5]={0,-150,100,0,0,600,-100,0,0,-150, 0,-200,50,0,0,800,-50,0,0,-200, 0,0,0,0,0,0,0,0,0,800 }
int i,iAngle[3]
POINT ptTemp[3][5]
iAngle[0]=(pst->wHour*30)%360+pst->wMinute/2
iAngle[1]=pst->wMinute*6
iAngle[2]=pst->wSecond*6
memcpy(ptTemp,pt,sizeof(pt))
for(i=fChange?0:2i<3i++)
{
RotatePoint(ptTemp[i],5,iAngle[i])
Polyline(hdc,ptTemp[i],5)
}
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static int cxClient,cyClient
static SYSTEMTIME stPrevious
BOOL fChange
HDC hdc
PAINTSTRUCT ps
SYSTEMTIME st
switch(message)
{
case WM_CREATE:
SetTimer(hwnd,ID_TIMER,1000,NULL)
GetLocalTime(&st)
stPrevious=st
return 0
case WM_SIZE:
cxClient=LOWORD(lParam)
cyClient=HIWORD(lParam)
return 0
case WM_TIMER:
GetLocalTime(&st)
fChange=st.wHour!=stPrevious.wHour||st.wMinute!=stPrevious.wMinute
hdc=GetDC(hwnd)
Setsotropic(hdc,cxClient,cyClient)
SelectObject(hdc,GetStockObject(WHITE_PEN))
DrawHands(hdc,&stPrevious,fChange)
SelectObject(hdc,GetStockObject(BLACK_PEN))
DrawHands(hdc,&st,TRUE)
stPrevious=st
return 0
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps)
Setsotropic(hdc,cxClient,cyClient)
DrawClock(hdc)
DrawHands(hdc,&stPrevious,TRUE)
EndPaint(hwnd,&ps)
return 0
case WM_DESTROY:
KillTimer(hwnd,ID_TIMER)
PostQuitMessage(0)
return 0
}
return DefWindowProc(hwnd,message,wParam,lParam)
}
C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。
C语言是由UNIX的研制者丹尼斯·里奇(Dennis Ritchie)于1970年 由 肯·汤普逊(Ken Thompson)所研制出的B语言的基础上发展和完善起来的。目前,C语言编译器普遍存在于各种不同的 *** 作系统中,例如UNIX、MS-DOS、Microsoft Windows及Linux等。C语言的设计影响了许多后来的编程语言,例如C++、Objective-C、Java、C#等。
秒表计时器的代码#include
#include
#include
#include
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()
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)