Home
Problem
Status
Contest
Workbook
User
Group
Forum
Register
Login
{"managingGroups":{},"author":"std_algorithm","updateTime":1555890224000,"title":"HZOI2017VotingSystem","dislikeCnt":0,"content":"[A better version with description for HZNOI](http://note.hznoi.com/blog/post/asttinx64/HZOI2017VotingSystem20190323Standalone)\n`Client_multiple.cpp`\n```cpp\n#define VERBASE(PLATFORM) (\"[multiple client - compiled for \" PLATFORM \"]2.4.1.0000(stable,config_builtin)\")\n#include\u003cbits/stdc++.h\u003e\n#if !defined(PLATFORM_LINUX)\u0026\u0026!defined(PLATFORM_WIN)\n#\tifdef __linux\n#\t\tdefine PLATFORM_LINUX\n#\tendif\n#\tif defined(_WIN32)||defined(_WIN64)\n#\t\tdefine PLATFORM_WIN\n#\tendif\n#endif\n#if defined(PLATFORM_LINUX)\u003d\u003ddefined(PLATFORM_WIN)\n#\terror \"Cannot detect platform by macro. \"\n#endif\n#ifdef PLATFORM_LINUX\n#\tdefine VERSION (VERBASE(\"linux\"))\n#\tinclude\u003cunistd.h\u003e\n#\tinclude\u003csys/utsname.h\u003e\n#\tinclude\u003carpa/inet.h\u003e\n#endif\n#ifdef PLATFORM_WIN\n#\tdefine VERSION (VERBASE(\"windows\"))\n#\tinclude\u003cwindows.h\u003e\n#endif\n#define SERVER_PORT (24976)\n#define SERVER_IP (\"172.18.200.100\")\n#ifdef PLATFORM_LINUX\n#\tdefine SOCKET int\n#\tdefine SOCKET_ERROR (-1)\n#\tdefine SYSINFO_READ_ERR (1)\n#endif\n#ifdef PLATFORM_WIN\n#\tdefine DLL_ERR (1)\n#endif\n#define SOCK_ERR (2)\n#define EMPTY_DATA (3)\n#define LENGTH_ERR (4)\n#define SEND_CONNECTION_ERR (5)\n#define SEND_TIMEOUT_ERR (6)\n#define RECV_CONNECTION_ERR (7)\n#define RECV_TIMEOUT_ERR (8)\n#define INTERACTION_SUCCESS (9)\n#ifdef PLATFORM_WIN\n#\tdefine WINSOCK_MAJOR_VER (2)\n#\tdefine WINSOCK_MINOR_VER (2)\n#endif\n#define ORIGINAL_TEXT (1)\ninline void Init(){\n#ifdef PLATFORM_LINUX\n\tutsname info;\n\tif(uname(\u0026info)){\n\t\tstd::cerr\u003c\u003c\"Cannot read information about system. \"\u003c\u003cstd::endl;\n\t\texit(SYSINFO_READ_ERR);\n\t}\n\tstd::cout\u003c\u003c\"Powered by \"\u003c\u003cinfo.sysname\u003c\u003c\" \"\u003c\u003cinfo.release\u003c\u003c\" \"\u003c\u003cinfo.machine\u003c\u003c\" (Berkeley Socket). \"\u003c\u003cstd::endl;\n#endif\n#ifdef PLATFORM_WIN\n\tWSADATA info;\n\tif(WSAStartup(MAKEWORD(WINSOCK_MAJOR_VER,WINSOCK_MINOR_VER),\u0026info)){\n\t\tstd::cerr\u003c\u003c\"Dynamic Link Library linking failed. \"\u003c\u003cstd::endl;\n\t\texit(DLL_ERR);\n\t}\n\tstd::cout\u003c\u003c\"Powered by \"\u003c\u003cinfo.szDescription\u003c\u003c\" (Berkeley Socket) [ Version \"\u003c\u003cWINSOCK_MAJOR_VER\u003c\u003c\".\"\u003c\u003cWINSOCK_MINOR_VER\u003c\u003c\" ]. \"\u003c\u003cstd::endl;\n#endif\n}\nSOCKET ClientSocket;\nsockaddr_in TargetServer;\ninline void SocketCreate(){\n\tClientSocket\u003dsocket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);\n\tif(ClientSocket\u003d\u003d(SOCKET)SOCKET_ERROR){\n\t\tstd::cerr\u003c\u003c\"Socket creation failed. \"\u003c\u003cstd::endl;\n\t\texit(SOCK_ERR);\n\t}\n#ifdef PLATFORM_LINUX\n\ttimeval SendTimeout;\n\tSendTimeout.tv_sec\u003d2;\n\tSendTimeout.tv_usec\u003d0;\n\ttimeval RecvTimeout;\n\tRecvTimeout.tv_sec\u003d2;\n\tRecvTimeout.tv_usec\u003d0;\n#endif\n#ifdef PLATFORM_WIN\n\tint SendTimeout\u003d2000;\n\tint RecvTimeout\u003d2000;\n#endif\n\tsetsockopt(ClientSocket,SOL_SOCKET,SO_SNDTIMEO,(char*)\u0026SendTimeout,sizeof(SendTimeout));\n\tsetsockopt(ClientSocket,SOL_SOCKET,SO_RCVTIMEO,(char*)\u0026RecvTimeout,sizeof(RecvTimeout));\n\tmemset(\u0026TargetServer,0,sizeof(sockaddr_in));\n\tTargetServer.sin_family\u003dAF_INET;\n\tTargetServer.sin_port\u003dhtons(SERVER_PORT);\n\tTargetServer.sin_addr.s_addr\u003dinet_addr(SERVER_IP);\n\tstd::cout\u003c\u003c\"Server address: \"\u003c\u003cSERVER_IP\u003c\u003cstd::endl;\n}\ninline int RequestPost(std::string data,std::string\u0026ResponseData){\n\tstatic const int RecvBufSize\u003d2000;\n\tstatic char RecvBuf[RecvBufSize];\n\tResponseData.clear();\n\tif(data.size()\u003e1111){\n\t\tstd::cerr\u003c\u003c\"Request too long. \"\u003c\u003cstd::endl;\n\t\treturn LENGTH_ERR;\n\t}\n\tint sendtoStatus\u003dsendto(ClientSocket,data.c_str(),data.size(),0,(sockaddr*)\u0026TargetServer,sizeof(sockaddr));\n\tif(!sendtoStatus){\n\t\tstd::cerr\u003c\u003c\"Connection reset or refused. \"\u003c\u003cstd::endl;\n\t\treturn SEND_CONNECTION_ERR;\n\t}\n\tif(!~sendtoStatus){\n\t\tstd::cerr\u003c\u003c\"Connection Timeout. \"\u003c\u003cstd::endl;\n\t\treturn SEND_TIMEOUT_ERR;\n\t}\n\tsockaddr_in RecvAdd\u003d{0};\n\twhile(1){\n\t\tmemset(RecvBuf,0,sizeof(RecvBuf));\n#ifdef PLATFORM_LINUX\n\t\tunsigned\n#endif\n\t\t\tint extlen\u003dsizeof(sockaddr_in);\n\t\tint recvfromStatus\u003drecvfrom(ClientSocket,RecvBuf,RecvBufSize,0,(sockaddr*)\u0026RecvAdd,\u0026extlen);\n\t\tif(!recvfromStatus){\n\t\t\tstd::cerr\u003c\u003c\"Connection reset or refused. \"\u003c\u003cstd::endl;\n\t\t\treturn RECV_CONNECTION_ERR;\n\t\t}\n\t\tif(!~recvfromStatus){\n\t\t\tstd::cerr\u003c\u003c\"Connection Timeout. \"\u003c\u003cstd::endl;\n\t\t\treturn RECV_TIMEOUT_ERR;\n\t\t}\n\t\tif((std::string)inet_ntoa(RecvAdd.sin_addr)\u003d\u003d(std::string)SERVER_IP)\n\t\t\tbreak;\n\t}\n\tResponseData\u003dRecvBuf;\n\tif(ResponseData.empty())\n\t\treturn EMPTY_DATA;\n\treturn INTERACTION_SUCCESS;\n}\ninline void TerminateSession(){\n#ifdef PLATFORM_LINUX\n\tshutdown(ClientSocket,SHUT_RDWR);\n\tclose(ClientSocket);\n#endif\n#ifdef PLATFORM_WIN\n\tshutdown(ClientSocket,SD_BOTH);\n\tclosesocket(ClientSocket);\n\tWSACleanup();\n#endif\n\tstd::cin.clear();\n\tstd::cin.sync();\n\tstd::cout\u003c\u003c\"Press any key to continue... \"\u003c\u003cstd::endl;\n\tstd::cin.get();\n\texit(0);\n}\ninline void Trim(std::string\u0026s){\n\twhile(!s.empty()\u0026\u0026isspace(s.back()))\n\t\ts.pop_back();\n\tfor(unsigned i\u003d0;i\u003cs.size();i++)\n\t\tif(!isspace(s[i])){\n\t\t\ts\u003ds.substr(i,s.size());\n\t\t\tbreak;\n\t\t}\n}\ninline void Readline(std::string\u0026target,bool original\u003d0){\n\tstd::cin.clear();\n\tstd::cin.sync();\n\tstd::getline(std::cin,target);\n\tif(!original){\n\t\tTrim(target);\n\t\tstd::transform(target.begin(),target.end(),target.begin(),tolower);\n\t\tif(target\u003d\u003d\"quit\"||target\u003d\u003d\"exit\")\n\t\t\tTerminateSession();\n\t}\n}\n#define ERRPL(INFO) {std::cerr\u003c\u003c(INFO)\u003c\u003cstd::endl;}\n#ifdef PLATFORM_LINUX\n#\tdefine DELAY {sleep(1);}\n#endif\n#ifdef PLATFORM_WIN\n#\tdefine DELAY {Sleep(1000);}\n#endif\nint main(){\n\tstd::cout\u003c\u003c\"HZOI2017 Voting System (version \"\u003c\u003cVERSION\u003c\u003c\"). Copyright 2019 @std_algorithm. \"\u003c\u003cstd::endl;\n\tDELAY;\n\tInit();\n\tDELAY;\n\tSocketCreate();\n\tDELAY;\n\tstd::cout\u003c\u003c\"Initialization finished. \"\u003c\u003cstd::endl;\n\tstd::string SessionKey;\n\twhile(1){\n\t\tstd::string ResponseData,cmd;\n\t\tif(RequestPost(\"Public\",SessionKey)!\u003dINTERACTION_SUCCESS){\n\t\t\tERRPL(\"Press [Enter] to try again or enter command \\\"quit\\\" or \\\"exit\\\". \");\n\t\t\tReadline(cmd);\n\t\t\tcontinue;\n\t\t}\n\t\twhile(RequestPost(\"Private \"+SessionKey+\" grab\",ResponseData)!\u003dINTERACTION_SUCCESS){\n\t\t\tERRPL(\"Press [Enter] to try again or enter command \\\"quit\\\" or \\\"exit\\\". \");\n\t\t\tReadline(cmd);\n\t\t\tcontinue;\n\t\t}\n\t\tif(ResponseData\u003d\u003d\"Expired\"){\n\t\t\tERRPL(\"This voting session has EXPIRED. \");\n\t\t\tSessionKey.clear();\n\t\t\tcontinue;\n\t\t}\n\t\tif(ResponseData!\u003d\" Inactive\"){\n\t\t\tstd::cout\u003c\u003c\"Voting for \"\u003c\u003cResponseData\u003c\u003cstd::endl;\n\t\t\twhile(1){\n\t\t\t\tstd::cout\u003c\u003c\"Please enter voting target ( or command \\\"quit\\\" or \\\"exit\\\" ): \"\u003c\u003cstd::flush;\n\t\t\t\tReadline(cmd);\n\t\t\t\tif(RequestPost(\"Private \"+SessionKey+\" target \"+cmd,ResponseData)!\u003dINTERACTION_SUCCESS){\n\t\t\t\t\tERRPL(\"Error occurred. Please try again. \");\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif(ResponseData\u003d\u003d\"No such target\"){\n\t\t\t\t\tERRPL(\"No such target. Please try again. \");\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif(ResponseData\u003d\u003d\"Expired\"){\n\t\t\t\t\tERRPL(\"This voting session has EXPIRED. \");\n\t\t\t\t\tSessionKey.clear();\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif(ResponseData\u003d\u003d\"Success\"){\n\t\t\t\t\tstd::cout\u003c\u003c\"Successfully voted. \"\u003c\u003cstd::endl;\n\t\t\t\t\twhile(1){\n\t\t\t\t\t\tstd::cout\u003c\u003c\"Please leave a comment for the reason and press [Enter] to submit or just press [Enter] to skip. \"\u003c\u003cstd::endl;\n\t\t\t\t\t\tReadline(cmd,ORIGINAL_TEXT);\n\t\t\t\t\t\tif(cmd.empty())\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\twhile(RequestPost(\"Private \"+SessionKey+\" comment \"+cmd,ResponseData)!\u003dINTERACTION_SUCCESS){\n\t\t\t\t\t\t\tERRPL(\"Press [Enter] to try again or enter command \\\"rewrite\\\" to rewrite the comment, or \\\"quit\\\" or \\\"exit\\\". \");\n\t\t\t\t\t\t\tReadline(cmd);\n\t\t\t\t\t\t\tif(cmd\u003d\u003d\"rewrite\"){\n\t\t\t\t\t\t\t\tcmd.clear();\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(ResponseData\u003d\u003d\"Expired\"){\n\t\t\t\t\t\t\tERRPL(\"This voting session has EXPIRED. \");\n\t\t\t\t\t\t\tSessionKey.clear();\n\t\t\t\t\t\t\tcmd\u003d\"exp\";\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(!cmd.empty()){\n\t\t\t\t\t\t\tstd::cout\u003c\u003c\"Successfully commented. Thanks! \"\u003c\u003cstd::endl;\n\t\t\t\t\t\t\tcmd.clear();\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(cmd.empty())\n\t\t\t\t\t\tcmd\u003d\"Finished.\";\n\t\t\t\t\tif(cmd\u003d\u003d\"Finished.\"||cmd\u003d\u003d\"exp\")\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tERRPL(\"Unrecognized response. Perhaps internal error occurred. \");\n\t\t\t\tTerminateSession();\n\t\t\t}\n\t\t\tif(cmd\u003d\u003d\"Finished.\")\n\t\t\t\tbreak;\n\t\t\tcontinue;\n\t\t}\n\t\tERRPL(\"No active voting sessions. Press [Enter] to try again or enter command \\\"quit\\\" or \\\"exit\\\". \");\n\t\tReadline(cmd);\n\t}\n\tTerminateSession();\n\treturn 0;\n}\n\n```\n\n`Server_windows.cpp`\n```cpp\n#define VERBASE() (\"[server for windows]2.0.3.0000(stable,config_builtin)\")\n#include\u003cbits/stdc++.h\u003e\n#if !defined(PLATFORM_LINUX)\u0026\u0026!defined(PLATFORM_WIN)\n#\tifdef __linux\n#\t\tdefine PLATFORM_LINUX\n#\tendif\n#\tif defined(_WIN32)||defined(_WIN64)\n#\t\tdefine PLATFORM_WIN\n#\tendif\n#endif\n#if defined(PLATFORM_LINUX)\u003d\u003ddefined(PLATFORM_WIN)\n#\terror \"Cannot detect platform by macro. \"\n#endif\n#ifdef PLATFORM_LINUX\n#\terror \"Platform not supported yet. \"\n#endif\n#ifdef PLATFORM_WIN\n#\tdefine VERSION (VERBASE())\n#endif\n#include\u003cwindows.h\u003e\n#include\u003cconio.h\u003e\n#define SERVER_PORT (24976)\n#define SERVER_IP (\"172.18.200.100\")\nconst std::string iplist[]\u003d{\n\t\"172.18.200.1\",\n\t\"172.18.200.2\",\n\t\"172.18.200.3\",\n\t\"172.18.200.4\",\n\t\"172.18.200.5\",\n\t\"172.18.200.6\",\n\t\"172.18.200.7\",\n\t\"172.18.200.8\",\n\t\"172.18.200.9\",\n\t\"172.18.200.10\",\n\t\"172.18.200.11\",\n\t\"172.18.200.12\",\n\t\"172.18.200.13\",\n\t\"172.18.200.14\",\n\t\"172.18.200.15\",\n\t\"172.18.200.16\"\n};\nconst std::string validtar[]\u003d{\n\t\"chr\",\n\t\"ckq\",\n\t\"csm\",\n\t\"gyz\",\n\t\"jjm\",\n\t\"jzh\",\n\t\"lbr\",\n\t\"lc\",\n\t\"nzy\",\n\t\"wty\",\n\t\"ww\",\n\t\"yqy\",\n\t\"yxy\",\n\t\"zcl\",\n\t\"zsq\",\n\t\"zyn\"\n};\n#define DLL_ERR (1)\n#define SOCK_ERR (2)\n#define MULTI_ERR (3)\n#define WINSOCK_MAJOR_VER (2)\n#define WINSOCK_MINOR_VER (2)\ninline void Init(){\n\tWSADATA info;\n\tif(WSAStartup(MAKEWORD(WINSOCK_MAJOR_VER,WINSOCK_MINOR_VER),\u0026info)){\n\t\tstd::cerr\u003c\u003c\"Dynamic Link Library linking failed. \"\u003c\u003cstd::endl;\n\t\texit(DLL_ERR);\n\t}\n\tstd::cout\u003c\u003c\"Powered by \"\u003c\u003cinfo.szDescription\u003c\u003c\" (Berkeley Socket) [ Version \"\u003c\u003cWINSOCK_MAJOR_VER\u003c\u003c\".\"\u003c\u003cWINSOCK_MINOR_VER\u003c\u003c\" ]. \"\u003c\u003cstd::endl;\n}\nSOCKET ServerSocket;\nsockaddr_in LocalServer;\ninline void SocketCreate(){\n\tServerSocket\u003dsocket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);\n\tif(ServerSocket\u003d\u003d(SOCKET)SOCKET_ERROR){\n\t\tstd::cerr\u003c\u003c\"Socket creation failed. \"\u003c\u003cstd::endl;\n\t\texit(SOCK_ERR);\n\t}\n\tint SendTimeout\u003d2222;\n\tint RecvTimeout\u003d2222;\n\tsetsockopt(ServerSocket,SOL_SOCKET,SO_SNDTIMEO,(char*)\u0026SendTimeout,sizeof(int));\n\tsetsockopt(ServerSocket,SOL_SOCKET,SO_RCVTIMEO,(char*)\u0026RecvTimeout,sizeof(int));\n\tmemset(\u0026LocalServer,0,sizeof(sockaddr_in));\n\tLocalServer.sin_family\u003dAF_INET;\n\tLocalServer.sin_port\u003dhtons(SERVER_PORT);\n\tLocalServer.sin_addr.S_un.S_addr\u003dinet_addr(SERVER_IP);\n\tbind(ServerSocket,(sockaddr*)\u0026LocalServer,sizeof(LocalServer));\n\tstd::cout\u003c\u003c\"Server address: \"\u003c\u003cSERVER_IP\u003c\u003cstd::endl;\n}\nstd::map\u003cstd::string,std::pair\u003cstd::string,std::string\u003e \u003esub;\nHANDLE OutputHandle,MTX;\n#define LOCK {WaitForSingleObject(MTX,INFINITE);}\n#define UNLOCK {ReleaseMutex(MTX);}\ninline void drawStatus(){\n\tCOORD LeftTopPos\u003d{0,0};\n\tsystem(\"color F0\");\n\tSetConsoleCursorPosition(OutputHandle,LeftTopPos);\n\tint backw\u003dBACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|BACKGROUND_INTENSITY;\n\tfor(auto iterate:iplist){\n\t\tif(sub[iterate].first.empty()){\n\t\t\tSetConsoleTextAttribute(OutputHandle,backw|FOREGROUND_RED);\n\t\t\tstd::cout\u003c\u003c\"[Client] \"\u003c\u003citerate\u003c\u003c\" [Waiting...] \";\n\t\t}else{\n\t\t\tSetConsoleTextAttribute(OutputHandle,backw|FOREGROUND_GREEN);\n\t\t\tstd::cout\u003c\u003c\"[Client] \"\u003c\u003citerate\u003c\u003c\" [Submitted!]\";\n\t\t\tSetConsoleTextAttribute(OutputHandle,backw|FOREGROUND_BLUE);\n\t\t\tif(sub[iterate].second.empty())\n\t\t\t\tstd::cout\u003c\u003c\" \";\n\t\t\telse\n\t\t\t\tstd::cout\u003c\u003c\"[Commented] \";\n\t\t}\n\t\tstd::cout\u003c\u003c\"\\n\";\n\t}\n\tstd::cout\u003c\u003cstd::flush;\n\tSetConsoleTextAttribute(OutputHandle,backw);\n}\nstd::map\u003cstd::string,std::string\u003ekeymap;\nstd::map\u003cstd::string,int\u003evot;\nconst char*dict\u003d\"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890\";\nstd::string title;\nULONG WINAPI ListenRequest(LPVOID param){\n\tconst int RecvBufSize\u003d1048576;\n\tstatic char RecvBuf[RecvBufSize];\n\tstd::mt19937 rd(time(0));\n\twhile(1){\n\t\tsockaddr_in RecvAdd\u003d{0};\n\t\tmemset(RecvBuf,0,sizeof(RecvBuf));\n\t\tint extlen\u003dsizeof(sockaddr_in);\n\t\trecvfrom(ServerSocket,RecvBuf,RecvBufSize,0,(sockaddr*)\u0026RecvAdd,\u0026extlen);\n\t\tstd::string ipstring\u003dinet_ntoa(RecvAdd.sin_addr);\n\t\tstd::string data\u003dRecvBuf;\n\t\tif(data.empty())\n\t\t\tcontinue;\n\t\tbool matched\u003d0;\n\t\tfor(auto chk:iplist)\n\t\t\tif(chk\u003d\u003dipstring)\n\t\t\t\tmatched\u003d1;\n\t\tif(!matched)\n\t\t\tcontinue;\n\t\tstd::string resdata;\n\t\tLOCK;\n\t\tif(data.substr(0,6)\u003d\u003d\"Public\"){\n\t\t\tstd::string keygen;\n\t\t\tfor(unsigned i\u003d0;i\u003c16;i++)\n\t\t\t\tkeygen.push_back(dict[rd()%62]);\n\t\t\tkeymap[ipstring]\u003dkeygen;\n\t\t\tresdata\u003dkeygen;\n\t\t}else if(data.size()\u003e8\u0026\u0026data.substr(8,16)!\u003dkeymap[ipstring]){\n\t\t\tresdata\u003d\"Expired\";\n\t\t}else if(data.size()\u003e25\u0026\u0026data.substr(25,4)\u003d\u003d\"grab\"){\n\t\t\tresdata\u003dtitle;\n\t\t}else if(data.size()\u003e25\u0026\u0026data.substr(25,6)\u003d\u003d\"target\"){\n\t\t\tif(data.size()\u003c\u003d32){\n\t\t\t\tUNLOCK;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tdata\u003ddata.substr(32,data.size());\n\t\t\tif(!sub[ipstring].first.empty()){\n\t\t\t\t--vot[sub[ipstring].first];\n\t\t\t\tsub[ipstring].first.clear();\n\t\t\t\tsub[ipstring].second.clear();\n\t\t\t}\n\t\t\tif(!vot.count(data))\n\t\t\t\tresdata\u003d\"No such target\";\n\t\t\telse{\n\t\t\t\t++vot[sub[ipstring].first\u003ddata];\n\t\t\t\tresdata\u003d\"Success\";\n\t\t\t}\n\t\t\tdrawStatus();\n\t\t}else if(data.size()\u003e25\u0026\u0026data.substr(25,7)\u003d\u003d\"comment\"){\n\t\t\tif(data.size()\u003c\u003d33){\n\t\t\t\tUNLOCK;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tdata\u003ddata.substr(33,data.size());\n\t\t\tsub[ipstring].second\u003ddata;\n\t\t\tresdata\u003d\"Success\";\n\t\t\tdrawStatus();\n\t\t}\n\t\tsendto(ServerSocket,resdata.c_str(),resdata.size(),0,(sockaddr*)\u0026RecvAdd,sizeof(sockaddr));\n\t\tUNLOCK;\n\t}\n\treturn 0;\n}\ninline void UIInit(){\n\tOutputHandle\u003dGetStdHandle(STD_OUTPUT_HANDLE);\n\ttitle\u003d\" Inactive\";\n}\ninline void Trim(std::string\u0026s){\n\twhile(!s.empty()\u0026\u0026isspace(s.back()))\n\t\ts.pop_back();\n\tfor(unsigned i\u003d0;i\u003cs.size();i++)\n\t\tif(!isspace(s[i])){\n\t\t\ts\u003ds.substr(i,s.size());\n\t\t\tbreak;\n\t\t}\n}\n#define ESC_ASCII (27)\ninline void UIProcess(){\n\tsystem(\"cls\");\n\tsystem(\"color F0\");\n\tCreateThread(NULL,0,\u0026ListenRequest,NULL,0,NULL);\n\twhile(1){\n\t\tstd::string target;\n\t\twhile(1){\n\t\t\tstd::cout\u003c\u003c\"Please enter a title for the coming voting session ( or command \\\"quit\\\" or \\\"exit\\\" ): \"\u003c\u003cstd::flush;\n\t\t\tstd::cin.clear();\n\t\t\tstd::cin.sync();\n\t\t\ttarget\u003d\"\";\n\t\t\tstd::getline(std::cin,target);\n\t\t\tTrim(target);\n\t\t\tstd::transform(target.begin(),target.end(),target.begin(),tolower);\n\t\t\tif(target.size()\u003c80)\n\t\t\t\tbreak;\n\t\t\tstd::cerr\u003c\u003c\"Title too long. \"\u003c\u003cstd::endl;\n\t\t}\n\t\tif(target\u003d\u003d\"quit\"||target\u003d\u003d\"exit\")\n\t\t\tbreak;\n\t\tLOCK;\n\t\ttitle\u003dtarget;\n\t\tkeymap.clear();\n\t\tsub.clear();\n\t\tfor(auto x:validtar)\n\t\t\tvot[x]\u003d0;\n\t\tsystem(\"cls\");\n\t\tdrawStatus();\n\t\tstd::cout\u003c\u003c\"Press [Escape] to terminate current voting session. \"\u003c\u003cstd::endl;\n\t\tlong long BeginClock\u003dclock();\n\t\tUNLOCK;\n\t\twhile(getch()!\u003dESC_ASCII)\n\t\t\t;\n\t\tLOCK;\n\t\tlong long EndClock\u003dclock();\n\t\tsystem(\"cls\");\n\t\tkeymap.clear();\n\t\tstd::cout\u003c\u003c\"Voting for \"\u003c\u003ctitle\u003c\u003c\" used \"\u003c\u003c(double)(EndClock-BeginClock)/CLOCKS_PER_SEC\u003c\u003c\"s. \"\u003c\u003cstd::endl;\n\t\tstd::ofstream fout(title+\".txt\");\n\t\tfout\u003c\u003c\"Result of \"\u003c\u003ctitle\u003c\u003cstd::endl;\n\t\tstd::vector\u003cstd::pair\u003cint,std::string\u003e \u003eorder;\n\t\tstd::map\u003cstd::string,std::set\u003cstd::string\u003e \u003ecom;\n\t\tfor(auto stat:sub)\n\t\t\tcom[stat.second.first].insert(stat.second.second);\n\t\tfor(auto tar:vot)\n\t\t\torder.push_back(std::make_pair(tar.second,tar.first));\n\t\tstd::sort(order.begin(),order.end(),std::greater\u003cstd::pair\u003cint,std::string\u003e \u003e());\n\t\tfor(unsigned i\u003d0;i\u003corder.size();i++)\n\t\t\tif(order[i].first){\n\t\t\t\tfout\u003c\u003corder[i].second\u003c\u003c\" got \"\u003c\u003corder[i].first\u003c\u003c\" votes. \"\u003c\u003cstd::endl;\n\t\t\t\tfor(auto stat:com[order[i].second])\n\t\t\t\t\tfout\u003c\u003c\"\\t\"\u003c\u003cstat\u003c\u003cstd::endl;\n\t\t\t}\n\t\tfout.close();\n\t\tstd::cout\u003c\u003c\"Result has been printed into \"\u003c\u003ctitle\u003c\u003c\".txt automatically according to default configuration. \"\u003c\u003cstd::endl;\n\t\ttitle\u003d\" Inactive\";\n\t\tUNLOCK;\n\t}\n}\n#define DELAY {Sleep(1000);}\nint main(){\n\tstd::cout\u003c\u003c\"HZOI2017 Voting System (version \"\u003c\u003cVERSION\u003c\u003c\"). Copyright 2019 @std_algorithm. \"\u003c\u003cstd::endl;\n\tMTX\u003dCreateMutex(NULL,FALSE,\"VoteServer\");\n\tif(GetLastError()\u003d\u003dERROR_ALREADY_EXISTS){\n\t\tstd::cerr\u003c\u003c\"A server is already running. \"\u003c\u003cstd::endl;\n\t\texit(MULTI_ERR);\n\t}\n\tInit();\n\tDELAY;\n\tSocketCreate();\n\tDELAY;\n\tUIInit();\n\tDELAY;\n\tstd::cout\u003c\u003c\"Initialization finished. \"\u003c\u003cstd::endl;\n\tUIProcess();\n\tshutdown(ServerSocket,SD_BOTH);\n\tclosesocket(ServerSocket);\n\tWSACleanup();\n\tstd::cin.clear();\n\tstd::cin.sync();\n\tstd::cout\u003c\u003c\"Press any key to continue... \"\u003c\u003cstd::endl;\n\tstd::cin.get();\n\treturn 0;\n}\n\n```\n","likeCnt":1,"createTime":1553328776000,"isWorkbook":false,"viewCnt":533,"openness":1,"fav":false,"id":971,"trustable":false}