Python.爱好者 发表于 2019-8-2 18:52:59

OJ评测

用C++做一个OJ,可以判断格式错误、正确、答案错误、时间超限,选手程序已经编译为user.exe文件。代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>
bool cmp(char answer[],char output[]) {
        int apos=strlen(answer)-1,opos=strlen(output)-1;
        while((answer=='\n'||answer==' ')&&apos!=0) apos--;
        while((output=='\n'||output==' ')&&opos!=0) opos--;
        while(apos!=0&&opos!=0) {
                while(answer==' '&&apos!=0) apos--;
                while(output==' '&&opos!=0) opos--;
                while(apos!=0&&opos!=0) {
                        if(answer=='\n'&&output=='\n') {apos--,opos--;break;}
                        if(!(answer!='\n'||output!='\n')) return 0;
                        if(answer!=output) return 0;
                        apos--,opos--;
                }
        }
        return !(apos^opos);
}
bool cmp2(char answer[],char output[]) {
        int apos=strlen(answer)-1,opos=strlen(output)-1;
        while(apos!=0&&opos!=0) {
                while((answer=='\n'||answer==' '||answer=='\t')&&apos!=0) apos--;
                while((output=='\n'||output==' '||output=='\t')&&opos!=0) opos--;
                if(answer!=output) return 0;
                apos--,opos--;
        }
        if(apos!=0) if(answer=='\n'||answer==' '||answer=='\t') apos--;
        if(opos!=0) if(output=='\n'||output==' '||output=='\t') opos--;
        while((answer=='\n'||answer==' '||answer=='\t')&&apos!=0) apos--;
        while((output=='\n'||output==' '||output=='\t')&&opos!=0) opos--;
        return !(apos^opos);
}
int digit(int num) {
        if(num==0) return 0;
        if(num<0) return digit(-num)+1;
        int cnt=0;
        while(num) num/=10,cnt++;
        return cnt;
}
DWORD WINAPI func(LPVOID lpParamter) {
        system("user<problem.in>problem.out");
}
int x=rand()%100,y=rand()%100;
int main() {
        srand(time(0));
        fclose(fopen("problem.out","w"));
        FILE* input=fopen("problem.in","w");
        fprintf(input,"%d %d",x,y);
        fclose(input);
        HANDLE ut=CreateThread(0,0,func,0,0,0);
        Sleep(1000);
        FILE* out=fopen("problem.out","r");
        fseek(out,0,2);
        size_t size=ftell(out);
        fseek(out,0,0);
        char *output=(char*)malloc(size);
        char *answer=(char*)malloc(digit(x)+digit(y)+2);
        fread(output,1,size,out);
        sprintf(answer,"%d",x+y);
        if(size==0) printf("TLE");
        else if(cmp(answer,output)) printf("AC");
        else if(cmp2(answer,output)) printf("PE");
        else printf("WA");
        TerminateThread(ut,0);
}
选手程序:#include<stdio.h>
int a,b;
int main() {
        scanf("%d%d",&a,&b);
        printf("%d",a+b);
        while(1);
}
我的程序在输出TLE后就无法关闭了,为什么?

Python.爱好者 发表于 2019-8-3 12:13:58

有人吗?
页: [1]
查看完整版本: OJ评测