.net process 调用G++编译器执行CPP编译,模拟ACM在线评判

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;
       }

    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值