C语言定时器
发布网友
发布时间:2022-04-22 16:49
我来回答
共5个回答
热心网友
时间:2023-10-22 05:31
这个程序用多线程比较好解决.
#include <windows.h>
#include <iostream>
#include <conio.h>
using namespace std;
const int MAX_SECOND = 1000 * 60;
DWORD WINAPI MyThread(PVOID pParam)
{
int nSum = 0;
DWORD dwFirstTime = GetTickCount();
cout<<"做题开始,请在"<<MAX_SECOND / 1000 <<"秒内回答."<<endl;
do
{
cout<<"1 + 2 = ? 请回答:"<<endl;
nSum = getch();
cout<<char(nSum)<<endl;
if(nSum == 3 + '0')
{
cout<<"答案正确"<<endl;
return 0;
}
}while(GetTickCount() - dwFirstTime <= MAX_SECOND);
return 0;
}
int main()
{
HANDLE hThread = CreateThread(NULL, 0, MyThread, 0, NULL, NULL);
WaitForSingleObject(hThread, MAX_SECOND);
CloseHandle(hThread);
return 0;
}
热心网友
时间:2023-10-22 05:31
#include<time.h>
time_t t;
struct tm *timer;
time(&t);//t返回系统当前时间
timer=localtime(&t);//返回tm结构时间,
这样取得系统时间,再跟已定时间比较,相等则触发
结构tm定义如下:
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;};
热心网友
时间:2023-10-22 05:32
我手头没编译器,所以没调试,你试试看先
#include <stdio>
#include <windows.h>
main()
{
int ms;
long start = GetTickCount();
scanf("%d",&ms);
printf("wait for %d second...",ms);
while(GetTickCount() < start + ms * 1000);
//your code here
}
热心网友
时间:2023-10-22 05:32
#include<conio.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>
#define N 10
void fun()
{
printf("OK,now fun() start!!");
getch();
}
void main()
{
clock_t start,end;
int i=0;
start=end=clock();
for(i=0;i<N;i++)
{
Sleep(1000);
end=clock();
}
fun();
}
热心网友
时间:2023-10-22 05:33
#include "stdio.h"
#include "time.h"
main()
{
开始做题;
clock_t start,end;
int n = 60;
start=end=clock();
while((n-(int)(end-start)/19)>=0)
{
sleep(1);
end=clock();
}
提交试卷;
}