Current location - Training Enrollment Network - Mathematics courses - How to realize timer in C language? For example, I give you a mathematical formula and ask you to give an answer within ten seconds, otherwise, execute the next one.
How to realize timer in C language? For example, I give you a mathematical formula and ask you to give an answer within ten seconds, otherwise, execute the next one.
# include & ltstdio.h & gt

# include & ltwindows.h & gt

int t = 0;

Dword WinAPI countdown (lpvoid lpparam){// Enter the countdown.

int sec = 10; ? //This is set to 10 second. You can change it to 2 seconds or 3 seconds, and see the effect as soon as possible during the test.

while(t & lt; 10 * sec) {

++ t;

Sleep (100);

}

Printf("\n %d seconds, the correct answer is 2\n ",seconds);

Exit (1);

}

int main(){

int result =- 1;

Handling hThread

DWORD dwThreadID

printf(" 1+ 1 = ");

hThread=::CreateThread(

NULL,//Default security attribute

NULL,//Default stack size

Countdown,//Thread Entry Address

NULL,? //parameters passed to the function

0,//Specifies that the thread runs immediately.

& ampDwThreadID // Returns the ID number of the thread.

); ?

Scanf("%d ",& result);

If(result==2)printf ("correct");

Else printf ("Error, the correct answer is 2");

Printf (",it took you %g seconds \n", t/10.0);

* close handle(hThread);

Returns 0;

}