解决启动外部程序问题。
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace OuterExe 9 { 10 class Program 11 { 12 static void InputFiles() 13 { 14 // In dir: Outer 15 // Input main.cpp & data.in & data.out 16 } 17 18 static void Main(string[] args) 19 { 20 InputFiles(); 21 try 22 { 23 Process Run = Process.Start("Outer\\main.bat"); 24 if (Run != null) 25 { 26 Run.WaitForExit(); 27 if (Run.HasExited) 28 Console.WriteLine("External program has exited."); 29 } 30 else 31 { 32 Run.Kill(); 33 Console.WriteLine("External program has exited by force."); 34 } 35 } 36 catch(Exception exception) 37 { Console.WriteLine(exception.Message); } 38 39 string ErrorString = System.IO.File.ReadAllText("Outer\\error"); 40 string Answer = System.IO.File.ReadAllText("Outer\\data.out"); 41 string YourAnswer = System.IO.File.ReadAllText("Outer\\test.out"); 42 43 if (ErrorString != "") 44 { 45 Console.WriteLine("Compiling error:\n" + ErrorString); 46 Console.ReadKey(); 47 return; 48 } 49 if (YourAnswer == Answer) 50 Console.WriteLine("Accepted"); 51 else Console.WriteLine("Wrong Answer"); 52 Console.ReadKey(); 53 } 54 } 55 }
在 debug 里面,有个 Outer 文件夹。
用 main.bat 统筹程序的执行。
1 @echo off 2 g++ Outer\main.cpp -o Outer\main 2> Outer\error 3 Outer\main < Outer\data.in > Outer\test.out
要注意的是 g++ 的执行目录不是 Outer,而是 debug,这是 bat 的特点决定的。
时间: 2024-10-19 17:41:06