C++#include <io.h>
#include <tchar.h>
#include <conio.h>
#include <windows.h>
#include <sys/stat.h>
#include <bits/stdc++.h>
#define clearLine cout << "********************************\n"
using namespace std;
int countData = 1;
int TimeLimit = 1000;
int MemoryLimit = 512;
int OutputLimit = 6666;
string k = "";
string prefix = "";
string fileCopy = "type ";
string fileCpoy = "type ";
struct mytime { string cmd; };
void About(void) ;
void Judge(void) ;
void Settings(void) ;
void commandString(int i) ;
void getNowDireFind(void) ;
DWORD WINAPI f1(LPVOID lp) ;
size_t getFileSize1(const char *fileName) ;
bool Compare(const char* file1, const char* file2) ;
main(void) {
getNowDireFind();
while (1) {
cout << "欢迎使用 MikuJudge,请选择服务:" << endl;
cout << "[A] 评测题目" << endl;
cout << "[B] 系统设置" << endl;
cout << "[C] 系统说明" << endl;
cout << "[Q] 退出" << endl;
char c = getch();
system("cls");
if (c != 'a' && c != 'b' && c != 'c' && c != 'q') {
cout << "\e[2m无效请求。3 秒后返回主界面。\e[0m" << endl;
Sleep(3000), system("cls");
continue;
}
if (c == 'a') {
Judge();
cout << "\e[2m请按任意键继续......\e[0m" << endl;
getch();
system("cls");
}
else if (c == 'b') {
Settings();
system("cls");
}
else if (c == 'c') {
About();
cout << "\e[2m请按任意键继续......\e[0m" << endl;
getch();
system("cls");
}
else if (c == 'q') return 0;
}
return 0;
}
void About(void) {
cout << "MikuJudge 是一款轻量级的 OI 比赛评测工具。" << endl;
cout << "本版本号为 \033[0;31mV1.1.5\033[0;29m,发布于 \033[0;31m2024 年 5 月 16 日\033[0;29m。" << endl;
cout << "开发者为 ManyMoleCules,如果喜欢这个项目请关注一下吧!" << endl;
}
void Judge(void) {
cout << "请输入数据组数:";
cin >> countData;
Sleep(1000), system("cls");
if (!countData) {
cout << "\033[0;33m数据组数不能为 0。\033[0;29m" << endl;
return;
}
cout << "如果您的数据文件有前缀,请按 Y,否则按 N。接下来评测机将会评测您的代码。" << endl;
char p = getch();
if (p == 'y') {
cout << "请输入数据前缀:";
cin >> prefix;
}
Sleep(1000), system("cls");
string igig = k + "\\settings\\TimeLimit.txt";
freopen(igig.c_str(), "r", stdin);
cin >> TimeLimit;
fclose(stdin);
igig = k + "\\settings\\MemoryLimit.txt";
freopen(igig.c_str(), "r", stdin);
cin >> MemoryLimit;
fclose(stdin);
igig = k + "\\settings\\OutputLimit.txt";
freopen(igig.c_str(), "r", stdin);
cin >> OutputLimit;
fclose(stdin);
int cntAC = 0;
for (int i = 1; i <= countData; i++) {
commandString(i);
cout << "\e[2m", system("g++ -o cpp.exe cpp.cpp"), cout << "\e[0m";
HANDLE hThread1;
mytime T = { "cpp.exe" };
hThread1 = CreateThread(NULL, 0, f1, &T, 0, NULL);
int signal = WaitForSingleObject(hThread1, (DWORD) TimeLimit);
if (signal != WAIT_OBJECT_0) {
cout << "#" << i << ": \033[0;33mTime Limit Exceeded\033[0;29m" << endl;
TerminateProcess(hThread1, -1), CloseHandle(hThread1);
continue;
}
if (getFileSize1("cpp.out") >= OutputLimit) {
cout << "#" << i << ": \033[0;35mOutput Limit Exceeded\033[0;29m" << endl;
TerminateProcess(hThread1, -1), CloseHandle(hThread1);
continue;
}
cout << "#" << i << ": ";
if (Compare("std.out", "cpp.out")) cout << "\033[0;32mAccepted\033[0;29m" << endl, cntAC++;
else cout << "\033[0;31mUnaccepted\033[0;29m" << endl;
CloseHandle(hThread1);
}
cout << "\e[2m", system("g++ -o cpp.exe cpp.cpp"), cout << "\e[0m";
clearLine;
cout << "本题得分:" << cntAC * 100 / countData << endl << "状态:";
if (cntAC == countData) cout << "\033[0;32mAccepted\033[0;29m" << endl;
else if (cntAC && cntAC != countData) cout << "\033[0;34mPartial Accepted\033[0;29m" << endl;
else cout << "\033[0;31mUnaccepted\033[0;29m" << endl;
clearLine;
if (cntAC == countData) cout << "恭喜你通过了此题!" << endl;
}
void Settings(void) {
while (1) {
cout << "请选择设置项:" << endl;
cout << "[A] 时空限制" << endl;
cout << "[B] 内存限制\033[0;31m(暂不可用)\033[0;29m" << endl;
cout << "[C] 输出大小限制" << endl;
cout << "[Q] 退出" << endl;
char c = getch();
system("cls");
if (c != 'a' && c != 'b' && c != 'c' && c != 'q') {
cout << "\e[2m无效请求。3 秒后返回主界面。\e[0m" << endl;
Sleep(3000), system("cls");
continue;
}
if (c == 'a') {
system("cls");
cout << "请输入新的时空限制(单位 ms,1~5000ms):";
int tl;
cin >> tl, TimeLimit = tl;
string fileName = k + "\\settings\\TimeLimit.txt";
ofstream file(fileName.c_str());
file << "", file << tl, file.close();
cout << "\e[2m保存成功,请按任意键继续......\e[0m" << endl;
getch();
system("cls");
}
else if (c == 'b') {
system("cls");
cout << "请输入新的内存限制(单位 MB,1~1024MB):";
int ml;
cin >> ml, MemoryLimit = ml;
string fileName = k + "\\settings\\MemoryLimit.txt";
ofstream file(fileName.c_str());
file << "", file << ml, file.close();
cout << "\e[2m保存成功,请按任意键继续......\e[0m" << endl;
getch();
system("cls");
}
else if (c == 'c') {
system("cls");
cout << "请输入新的输出大小限制(单位 B,1~1073741824B):";
int ol;
cin >> ol, OutputLimit = ol;
string fileName = k + "\\settings\\OutputLimit.txt";
ofstream file(fileName.c_str());
file << "", file << ol, file.close();
cout << "\e[2m保存成功,请按任意键继续......\e[0m" << endl;
getch();
system("cls");
}
else if (c == 'q') {
system("cls");
return;
}
}
}
void commandString(int i) {
fileCopy += k, fileCopy += "\\data\\", fileCopy += prefix, fileCopy += to_string(i), fileCopy += ".out > std.out";
const char *p1 = fileCopy.c_str();
system(p1);
fileCpoy += k, fileCpoy += "\\data\\", fileCpoy += prefix, fileCpoy += to_string(i), fileCpoy += ".in > std.in";
const char *p2 = fileCpoy.c_str();
system(p2);
fileCopy = "type ", fileCpoy = "type ";
}
size_t getFileSize1(const char *fileName) {
if (fileName == NULL) return -1;
struct stat statbuf;
stat(fileName, &statbuf);
size_t fileSize = statbuf.st_size;
return fileSize;
}
DWORD WINAPI f1(LPVOID lp) {
mytime* t = (mytime*) lp;
system((t->cmd).c_str());
return 0;
}
void getNowDireFind(void) {
char buf[1010];
getcwd(buf, sizeof buf), k = buf;
}
bool Compare(const char* file1, const char* file2) {
FILE *f1, *f2;
int size1, size2;
unsigned char buffer1[1024], buffer2[1024];
f1 = fopen(file1, "rb+"), f2 = fopen(file2, "rb+");
bool isTheSame = false;
if (!f1 || !f2) goto end;
while (1) {
size1 = fread(buffer1, 1, 1024, f1), size2 = fread(buffer2, 1, 1024, f2);
if (size1 != size2) goto end;
if (size1 < 1) break;
for (int i = 0; i < size1; i++)
if (buffer1[i] != buffer2[i]) goto end;
}
isTheSame = true;
end: // these need to fix
if (size1 + 1 == size2){
for (int i = 0; i < size1; i++)
if (buffer1[i] != buffer2[i]) {
if (f1) fclose(f1);
if (f2) fclose(f2);
return false;
}
if (buffer2[size2 - 1] == '\r' || buffer2[size2 - 1] == '\n') return TRUE;
else return false;
}
else if (size2 + 1 == size1) {
for (int i = 0; i < size2; i ++)
if (buffer1[i] != buffer2[i]) {
if (f1) fclose(f1);
if (f2) fclose(f2);
return false;
}
if (buffer1[size1 - 1] == '\r' || buffer1[size1 - 1] == '\n') return TRUE;
else return false;
}
if (f1) fclose(f1);
if (f2) fclose(f2);
return isTheSame;
}