1、C语言中实现精确计时,参考代码如下:
ANSIC: ftime()
struct timeb {
time_t time;
unsigned short millitm;
short timezone;
short dstflag;
};
2、另外,还有就是更精确定计时,需要CPU的支持,具体情况,可以参考MSDN。
LARGE_INTEGER frequency = {0};
LARGE_INTEGER now = {0};
LARGE_INTEGER end = {0};
QueryPerformanceFrequency(&frequency);
while (QueryPerformanceCounter(&now) && ((nowQuadPart - endQuadPart) < 0))
{
Sleep(0);
}
你用的是图形界面?
#include <dosh>
#include <timeh>
#include <conioh>
#include <stdioh>
#include <stdlibh>
#include <graphicsh>
struct ViewPort
{
int left;
int top;
int right;
int bottom;
};
struct Point
{
int x;
int y;
};
struct MyTime
{
int Hour;
int Min;
int Sec;
};
struct ViewPort AllSrc; /全屏视口/
struct ViewPort MouseBox={0,0,623,479}; /鼠标能到达的最大视口/
struct ViewPort TimeBox={428,25,600,55}; /时间视口/
struct Point Mouse,lastpos; /鼠标重绘时用,记录鼠标的当前位置和上一位置/
struct MyTime timer;
voidbitmap; /保存鼠标上一位置的位图(除去鼠标本身)/
char ARROR1[]={
/以下是 鼠标箭头 的 16点阵 字模,128 byte ,横向,自己绘制/
0xe0,0x00,
0xfe,0x00,
0xff,0xf0,
0x7f,0xfe,
0x7f,0xf0,
0x7f,0x00,
0x7f,0x00,
0x3f,0x80,
0x39,0xc0,
0x38,0xe0,
0x38,0x70,
0x38,0x38,
0x10,0x1c,
0x10,0x0e,
0x10,0x06,
0x00,0x00
};
void drawHanzi(char mat,int matsize,int x,int y,int color) /从字模点阵数组中获得字模,显示汉字/
/依次:字模指针、点阵大小、起始坐标(x,y)、颜色/
{
int i,j,k,n;
n=(matsize-1)/8+1;
for(j=0;j<matsize;j++)
for(i=0;i<n;i++)
for(k=0;k<8;k++)
if(mat[jn+i]&(0x80>>k)) /测试为1的位则显示/
putpixel(x+i8+k,y+j,color);
}
void MouseSetXY(int x,int y) /设置鼠标的位置/
{
_CX=x;
_DX=y;
_AX=0x04;
geninterrupt(0x33);
Mousex=x;
Mousey=y;
}
void MouseGetXY() /获取鼠标的位置/
{
_AX=0x03;
geninterrupt(0x33);
Mousex=_CX;
Mousey=_DX;
}
int LeftPress() /判断鼠标左键是否被按下/
{
_AX=0x03;
geninterrupt(0x33);
return (_BX&1);
}
int RightPress() /判断鼠标右键是否被按下/
{
_AX=0x03;
geninterrupt(0x33);
return (_BX&2);
}
void SetMouseXRange(int min,int max) /设置鼠标横向坐标范围/
{
_AX=0x07;
_CX=min;
_DX=max;
geninterrupt(0x33);
}
void SetMouseYRange(int min,int max) /设置鼠标纵向坐标范围/
{
_AX=0x08;
_CX=min;
_DX=max;
geninterrupt(0x33);
}
int bFirst=1;
void DrawMouse() /在Mouse对应坐标处绘制鼠标/
{
if(bFirst)
{
bFirst=0;
getimage(Mousex,Mousey,Mousex+15,Mousey+15,bitmap);
drawHanzi(ARROR1,16,Mousex,Mousey,YELLOW);
}
else
{
putimage(lastposx,lastposy,bitmap,COPY_PUT);
getimage(Mousex,Mousey,Mousex+15,Mousey+15,bitmap);
drawHanzi(ARROR1,16,Mousex,Mousey,YELLOW);
}
lastposx=Mousex;
lastposy=Mousey;
}
void HideMouse() /隐藏鼠标/
{
putimage(lastposx,lastposy,bitmap,COPY_PUT);
bFirst=1;
}
void SetViewPort(struct ViewPort p) /设置视口/
{
setviewport(pleft,ptop,pright,pbottom,1);
}
void SetMouseRange(struct ViewPort p)
{
SetMouseXRange(pleft,pright);
SetMouseYRange(ptop,pbottom);
}
void TimeAdd(struct MyTime pt)
{
pt->Sec++;
if(pt->Sec>59)
{
pt->Sec=0;
pt->Min++;
if(pt->Min>59)
{
pt->Min=0;
pt->Hour++;
}
}
}
void FormatTime(char st,struct MyTime t)
{
sprintf(st,"%d:%2d:%2d",tHour,tMin,tSec);
}
void DrawTop()
{
char st[24];
long ti,last=time(NULL);
int starty=5;
int startx=200,endx=418;
struct time tm,lasttm;
struct time tm_click,lasttm_click;
gettime(&lasttm);
while(!kbhit())
{
SetViewPort(AllSrc);
setlinestyle(SOLID_LINE,NULL,THICK_WIDTH);
setfillstyle(0,YELLOW);
MouseGetXY();
gettime(&tm);
if( ((tmti_sec-lasttmti_sec)100+tmti_hund-lasttmti_hund)>10)
{
DrawMouse();
lasttm=tm;
}
ti=time(NULL);
if(ti!=last)
{
SetViewPort(TimeBox);
TimeAdd(&timer);
FormatTime(st,timer);
setcolor(RED);
clearviewport();
outtextxy(2,2,st);
last=ti;
}
}
}
main()
{
int driver,mode;
driver=VGA;
mode=VGAHI;
timerHour=0;
timerMin=0;
timerSec=0;
initgraph(&driver,&mode,"E:\\TC3\\BGI");
AllSrcleft=0;
AllSrctop=0;
bitmap=malloc(imagesize(0,0,15,15));
AllSrcright=getmaxx();
AllSrcbottom=getmaxy();
setbkcolor(LIGHTGRAY);
SetMouseRange(MouseBox);
MouseSetXY(100,100);
SetViewPort(AllSrc);
DrawTop();
closegraph();
return 0;
}
#include<stdioh>#include<timeh>
int main(){ time_t start, end;
int i;
start = time(NULL);
end = time(NULL);
printf("please input:");
for(i = 0; i < 3; i++){
while(end - start < i){
end = time(NULL);
}
cystem("cls");
printf("%d", i);
}
return 0;
}
以上就是关于c语言倒计时怎么编全部的内容,包括:c语言倒计时怎么编、求高手啊,用C语言编写一个计时程序、c语言 制作 倒计时等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)