ACM在线评判,需自动判断程序正确性,需两个步骤
1、调用对应的编译器编译学生提交的源代码
2、执行编译好的程序,输入测试数据,运行判断运行结果是否是预期结果
-------------------------------------待测试用户代码max-----------------------------------------------------
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d",a>b?a:b);
return 0;
}
------------------------------模拟评判代码------------------------------------
private void button1_Click(object sender, EventArgs e)
{
if (Compile("max"))//如果编译max程序成功
{
bool result = Test("max", "4 5", "5"); //对MAX程序测试,送入测试数据4 5,预期结果 5
if (result) MessageBox.Show("ok");
else MessageBox.Show("err");
}
}
///自动编译cpp代码
//filename待编译CPP文件名
bool Compile(string fileName)
{
fileName = "max";
string command = @"D:\DEV-CPP\Bin\g++.exe"; //编译器G++
Process p = new Process();
p.StartInfo.FileName = command; //确定程序名
p.StartInfo.Arguments = @"D:\DEV-CPP\Bin\max.cpp -o D:\DEV-CPP\Bin\max.exe"; //确定程式命令行
p.StartInfo.UseShellExecute = false; //Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向输入
p.StartInfo.RedirectStandardOutput = true; //重定向输出
p.StartInfo.RedirectStandardError = true; //重定向输出错误
p.StartInfo.CreateNoWindow = false; //设置置不显示示窗口
p.Start();
textBox1.Text = p.StandardError.ReadToEnd(); //输出出流取得命令行结果果
p.WaitForExit();
if (textBox1.Text == "") return true;//如果无错误输出,则编译成功
return false;
}
//自动测试代码
//filename:被测试cpp的EXE文件名
//testData测试数据
//result 预期测试结果
bool Test(string fileName,string testData,string result)
{
fileName = "max";
string command = @"D:\DEV-CPP\Bin\max.exe";
Process p = new Process();
p.StartInfo.FileName = command; //确定程序名
p.StartInfo.UseShellExecute = false; //Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向输入
p.StartInfo.RedirectStandardOutput = true; //重定向输出
p.StartInfo.RedirectStandardError = true; //重定向输出错误
p.StartInfo.CreateNoWindow = false; //设置置不显示示窗口
p.Start();
p.StandardInput.WriteLine(testData);
string realResult = p.StandardOutput.ReadToEnd(); //取得实际结果realResult
//输出出流取得命令行结果果
p.WaitForExit();
if (result == realResult) return true;
return false;
}
}