Document directory
- About imustoj
- Why O & M using scripts
- Functions implemented by the script in this article
- Script running
- Script implementation
- Script usage
Imustoj O & M-automatically generates question test data in batches
By Ma Dongliang (cream Loki)
One man's war (http://blog.csdn.net/MDL13412)
About imustoj
The OJ we use is modified based on Noj. We have made some customization based on actual needs, and the overall function is rarely modified. Therefore, the script provided in this article is also applicable to schools that use Noj.
Why O & M using scripts
When adding a question, it is very troublesome to test and generate the test data. Each question must go through the preparation of the mark, writing the input data, compiling, linking, and running, after the output result is redirected to the file, the input and output data are stored in the corresponding folder according to the OJ folder structure. If you manually perform the above operations on each question, the task volume is very large and complicated. In this case, you need to use an automated script to help us complete the above work.
Functions implemented by the script in this article
- Given the range of question numbers, the corresponding standards and input data file templates are automatically generated for users to directly change without manual creation;
- Automatically compile and link the program, run the program with input data, and redirect the result to the corresponding directory structure;
- Detailed log records are generated for the mark processes that encounter errors during compilation and linking;
Script running
Script implementation
The first step is to automatically generate a template script for the bidding process and input data:
@echo offset STARTINDEX=1100set ENDINDEX=1120set STEPINDEX=1set SOURCEDIR=D:\infilesif exist %SOURCEDIR% goto direxist >nul 2>nulmkdir %SOURCEDIR% >nul 2>nul:direxistFOR /L %%i IN (%STARTINDEX%,%STEPINDEX%,%ENDINDEX%) DO echo TODO > %SOURCEDIR%\%%i.cpp && echo TODO > %SOURCEDIR%\%%i.inpause
Based on startindex and endindex, the above program generates XXX. cpp and XXX. In templates named after question numbers for editing. Output to the directory specified by the sourcedir variable. Since the C language can be compiled without modifying the. cpp suffix, all the standards are formulated as C ++ source files.
Next we will compile, link, and run the program. There are two scripts: maketest. BAT and makeeach. bat.
The maketest. Bat User Enumeration mark, while makeeach. bat is used to actually process each mark.
The following describes the source code of maketest. BAT:
@echo offset SOURCEDIR=D:\infilesfor /f "delims=" %%I in ('dir /B /ON %SOURCEDIR%') do call makeeach.bat %SOURCEDIR%\%%~nI %%~nIpause
The script enumerates the files in the folder specified by the sourcedir variable, extracts the question number, and passes it to makeeach. BAT for further processing.
Let's check the source code of makeeach. BAT:
@echo offset GPPPATH=E:\IMUSTJudge\AcmJudge\bin\gcc\bin\g++.exeset OUTPUTBIN=D:\outfiles\%2.exeset LOGFILE=D:\maketest.logset OUTPUTDIR=D:\outfilesset SOURCEFILE=%1.cppset INFILE=%1.inset OUTFILE=%OUTPUTDIR%\%2\output\%2.outif exist %OUTPUTDIR% goto direxist >nul 2>nulmkdir %OUTPUTDIR% >nul 2>nul:direxistdel /f /q /s %OUTPUTBIN% >nul 2>nul%GPPPATH% %SOURCEFILE% -o %OUTPUTBIN% >nul 2>nulset err=%errorlevel%if "%err%"=="0" goto successgoto failure:successmkdir %OUTPUTDIR%\%2\output >nul 2>nul%OUTPUTBIN% < %INFILE% > %OUTFILE%set COPYINFILEPATH=%OUTPUTDIR%\%2\inputmkdir %OUTPUTDIR%\%2\input >nul 2>nulcopy /Y %INFILE% /A %COPYINFILEPATH% /A >nul 2>nulgoto end:failureecho compile %SOURCEFILE% failedecho compile %SOURCEFILE% failed >> %LOGFILE%:end
This script is the most important of the three scripts, among which:
- Gpppath specifies the path of the G ++ compiler;
- Outputbinspecifies the final compiled program to be saved. Its name is the "cmd.exe" format;
- Logfile specifies the location and name of the error log;
- Outputdir specifies the storage location of the final generated input and output files. It will automatically create a folder with a clear question mark under this folder, and create the inout and output folders in it, copy the input and output data to the corresponding folder;
- Sourcefile specifies the program name;
- Iinfile specifies the name of the input data file;
- Outputfile specifies the path and name of the output file;
For the involved folders, you must first determine whether they exist, and errors will occur during file copying and compilation.
The code for compiling the program is very simple, as shown below:
%GPPPATH% %SOURCEFILE% -o %OUTPUTBIN% >nul 2>nul
To determine whether the compilation is successful, you need to determine the return value of the command, as shown below:
set err=%errorlevel%if "%err%"=="0" goto successgoto failure
After compilation, run the program, generate the output data, and copy the corresponding file to the specified directory structure, as shown below:
:successmkdir %OUTPUTDIR%\%2\output >nul 2>nul%OUTPUTBIN% < %INFILE% > %OUTFILE%set COPYINFILEPATH=%OUTPUTDIR%\%2\inputmkdir %OUTPUTDIR%\%2\input >nul 2>nulcopy /Y %INFILE% /A %COPYINFILEPATH% /A >nul 2>nulgoto end
If compilation fails, the error log is recorded as follows:
:failureecho compile %SOURCEFILE% failedecho compile %SOURCEFILE% failed >> %LOGFILE%
Script usage
After the parameters are configured, run makesourcefile. BAT to generate the bidding process and input the data template, and then complete the template;
After the above operations are completed, run maketest. bat. The program will automatically complete all related operations. If an error occurs, the error log will be printed and recorded in the file for your convenience;
Finally, copy the generated question folder to the corresponding OJ directory.