c语言,怎么写一个倒计时的选择题(在规定时间回答文体,超出算失败)switch...
发布网友
发布时间:2023-12-23 08:22
我来回答
共1个回答
热心网友
时间:2024-10-22 00:06
倒计时可定用计时器,下面是windows平台的实现,每秒打印秒数
#include<iostream>
#include <iomanip>
#include <windows.h>
#include <winsock.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
DWORD dwVersion = 0; WSADATA wsaData; DWORD dwErr = 0;
dwErr = WSAStartup(MAKEWORD(0x2, 0x2), &wsaData);
if (0 != dwErr)
{printf("winsock init error!\n");return -1;}
SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == s)
{
printf("create a socket error!\n");
WSACleanup();
return -1;
}
// 设置为非阻塞
int nRet = 0;
u_long lRet = 1;
nRet = ioctlsocket(s, FIONBIO, &lRet);
if (SOCKET_ERROR == nRet)
{
printf("set socket error!\n");
closesocket(s);
WSACleanup();
return -1;
}
int time=0;
while( 1 ){
fd_set rfds ;
struct timeval tval;
FD_ZERO( &rfds ) ;
FD_SET(1,&rfds);
tval.tv_sec = 1 ; /*秒*/
tval.tv_usec =0 ; /*微秒, 1秒=10的3次方毫秒=10的6次方微妙*/
int ret =select( 1, &rfds, NULL, NULL, &tval );
if(ret==0 ){
cout<<setfill('0')<<setw(3)<<++time<<" s"<<endl;//此处写,时间到了你要做的事
}
else
{cout<<WSAGetLastError()<<endl;
break;
}
}
WSACleanup();
return 0;
}