话说windows下测出来的十分不稳定。。。还有很多bug,不会改啊,还是好好准备期末考吧
很傻很天真。。。
代码:
#include <windows.h>
#include <ntsecapi.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define SystemProcessesAndThreadsInformation 5
using namespace std;
typedef DWORD (WINAPI *ZWQUERYSYSTEMINFORMATION) (DWORD,PVOID,DWORD,PDWORD);
typedef struct _SYSTEM_PROCESS_INFORMATION
{
DWORD NextEntryDelta;
DWORD ThreadCount;
DWORD Reserved1[6];
FILETIME ftCreateTime;
FILETIME ftUserTime;
FILETIME ftKernelTime;
UNICODE_STRING ProcessName;
DWORD BasePriority;
DWORD ProcessId;
DWORD InheritedFromProcessId;
DWORD HandleCount;
DWORD Reserved2[2];
DWORD VmCounters;
DWORD dCommitCharge;
ULONG dwPageFaults;
DWORD dwWorkingSetPeak;
DWORD dwWorkingSet;
DWORD dwQuotaPeakPagedPoolUsage;
DWORD dwQuotaPagedPoolUsage;
DWORD dwQuotaPeakNonPagedPoolUsage;
DWORD dwQuotaNonPagedPoolUsage;
DWORD dwPageFileUsage;
DWORD dwPageFileUsagePeak;
PVOID ThreadInfos[1];
} SYSTEM_PROCESS_INFORMATION, *PSYSTEM_PROCESS_INFORMATION;
HMODULE hNtDll =GetModuleHandle("ntdll.dll");
ZWQUERYSYSTEMINFORMATION ZwQuerySystemInformation = (ZWQUERYSYSTEMINFORMATION)GetProcAddress(hNtDll,"ZwQuerySystemInformation");
ULONG cbBuffer=0x10000;
LPVOID pBuffer=malloc(cbBuffer);
string workpath,gpppath,gccpath,fpcpath,javapath,command,debuger;
inline DWORD get(DWORD &pid)
{
ZwQuerySystemInformation(SystemProcessesAndThreadsInformation,pBuffer,cbBuffer,NULL);
PSYSTEM_PROCESS_INFORMATION pInfo = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
while(1)
{
if(pInfo->ProcessId==pid)return pInfo->dwWorkingSet;
if(pInfo->NextEntryDelta==0)break;
pInfo=(PSYSTEM_PROCESS_INFORMATION)(((PUCHAR)pInfo)+pInfo->NextEntryDelta);
}
pid=0;
return 0;
}
inline void cls()
{
for(int i=0;i<100;++i)printf("\b");
}
int Compling(int LanuageId)
{
printf("Compling...");
switch(LanuageId)
{
case 0:command=gpppath+"g++ foo.cpp -o foo -fno-asm -s -w -O1 -DONLINE_JUDGE 2>error.txt";break;
case 1:command=gccpath+"gcc foo.c -o foo -fno-asm -s -w -O1 -DONLINE_JUDGE 2>error.txt";break;
case 2:command=fpcpath+"fpc foo.pas -Sg -dONLINE_JUDGE>error.txt"; break;
case 3:command=javapath+"javac foo.java 2> error.txt"; break;
default :printf("Sorry,system nonsupport this lanuage.\n");
}
if(system(command.c_str())==0)return 1;
return 0;
}
int Debug(int LanuageId)
{
cls();
printf("Testing...");
/***************************************************/
fclose(stdin);
fclose(stdout);
STARTUPINFO si={0};
PROCESS_INFORMATION pi={0};
si.cb=sizeof(si);
freopen("test.txt","r",stdin);
si.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
freopen("out.txt","w",stdout);
si.hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError=GetStdHandle(STD_ERROR_HANDLE);
si.dwFlags=STARTF_USESTDHANDLES;
/***************************************************/
if(LanuageId<3)command="foo";
else command=javapath+"java foo";
TCHAR *szCommandLine=new TCHAR[command.size()+1];
szCommandLine[command.size()]=0;
copy(command.begin(),command.end(),szCommandLine);
DWORD StartTime=GetTickCount();
DWORD EndTime;
DWORD TimeLen=2000;
DWORD MemorySize=5000000;
DWORD Maxsize=0;
int ERROR_ID=0;
if(CreateProcess(NULL,szCommandLine,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))
{
while(pi.dwProcessId)
{
Maxsize=max(Maxsize,get(pi.dwProcessId));
if(Maxsize>MemorySize)
{
ERROR_ID=2;
break;
}
if(StartTime+TimeLen<GetTickCount())
{
ERROR_ID=1;
break;
}
}
if(ERROR_ID)CloseHandle(pi.hThread),CloseHandle(pi.hProcess);
EndTime=GetTickCount();
}
else ERROR_ID=3;
fclose(stdin);
fclose(stdout);
freopen("CON", "w", stdout);
cls();
if(ERROR_ID==1)cout<<"Time Limit Exceed"<<endl;
if(ERROR_ID==2)cout<<"Memory Limit Exceed"<<endl;
if(ERROR_ID==3)cout<<"Runtime Error "<<GetLastError()<<endl;
if(!ERROR_ID)
{
command="fc ans.txt out.txt >Debug.txt";
if(system(command.c_str())==0)
{
cout<<"Accept "<<EndTime-StartTime<<" ms "<<(Maxsize/1024)<<" K"<<endl;
}
else printf("Wrong Answer\n");
}
return 0;
}
void setworkpath()
{
workpath="D:\\project\\";
gpppath=workpath+"compiler\\MinGW\\bin\\";
gccpath=workpath+"compiler\\MinGW\\bin\\";
fpcpath=workpath+"compiler\\fpc\\";
javapath=workpath+"compiler\\java\\bin\\";
}
int main()
{
setworkpath();
if(Compling(0))Debug(0);
else printf("Complie Error\n");
free(pBuffer);
command="del foo.exe foo.class error.txt out.txt Debug.txt";
system(command.c_str());
return 0;
}