当前位置:首页 » 编程语言 » 计时器的c语言代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

计时器的c语言代码

发布时间: 2022-12-21 16:49:45

㈠ 求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
#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语言编写的计时器源代码怎么编写

几天前写了个大概
你做个参考把
#include<stdio.h>
void
main()
{
float
x,y;
char
fuhao;
pintf("请输入第一个数:\t请输入运算符:\t,请输入第二个数:\n");
scanf("%f,%c,%f",&x,&fuhao,&f);
if(fuhao=='+')printf("%f,%c,%f=%f\n",x,fuhao,y,x+y);
else
if(fuhao=='-')printf("%f,%c,%f=%f\n"x,fuhao,y,x-y);
else
if(fuhao=='*')printf("%f,%c,%f=%f\n"x,fuhao,y,x*y);
else
if(fuhao=='/')
{
if(y==0.0)printf("分母不能为0\n");
else
printf("%f,%c,%f=%f\n"x,fuhao,y,x/y);
}
}

㈣ c语言怎么样编写一个时钟程序

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=0;i<iNum;i++)

{

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=0;iAngle<360;iAngle+=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:2;i<3;i++)

{

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语言计时器的例子

用单片机里的计时器就ok了,弄个中断就行

㈥ c语言倒计时器 的编程代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ;
while (clock() < endwait) {}
}

void main(){
int t,m,s;
printf("input counterdown time in seconds\n");
scanf("%d",&t);
printf("\n===================\n");
while(1)
{
wait ( 1 );
t--;
if (t==0) break;
s = t % 60;
m = t / 60;
printf("\r\t%02d:%02d",m,s);
}
exit(0);
};

㈦ 怎么用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>
#include<stdlib.h>
void
main
()
{
do{
time_t
rawtime;
struct
tm
*
timeinfo;
time
(
&rawtime
);
timeinfo
=
localtime
(
&rawtime
);
printf
(
"%4d-%02d-%02d
%02d:%02d:%02d\n",1900+timeinfo->tm_year,
1+timeinfo->tm_mon,
timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
int
c=0;//c的值为了控制时间差
time_t
t,k;
//获取时间的秒数的申明
time(&t);
do
{time(&k);
c=k-t;
if(c==1)
break;
}while(c!=1);
system("cls");
}while(1);
}

㈨ 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;

}

(9)计时器的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语言中的头文件time.h中定义了库函数clock(),
它返回的是从程序运行开始算起的时间,一时钟周期为单位,
time.h还定义了符号:CLOCKS_PER_SEC,
即一秒钟的时钟周期。这样就简单了,
在头文件中加入#include<time.h>,在程序main()主函数的开头定义long now=0;
并给把clock()赋值给now,即now=clock();记录程序开始时的时间,clock()会继续增加,
但now已经确定为开始那一时刻clock()的值,
在程序结尾,算式clock()-now就是程序执行所需的时间,
但是是以时钟周期为单位的,
如果想得到以秒为单位的时间只要输出(clock()-now)/CLOCKS_PER_SEC就是了,
即在程序结尾添加
printf("%f",(clock()-now)/CLOCKS_PER_SEC);就可以了。