小弟最近做一个考试系统。
老师要求如下:
输入C语言代码,然后提交,判断结果是否正确。
我当时的第一反应就是调用Turbo C,但是不知道怎么调用。小弟C语言学的比较菜,而且学C语言都是用的Embarcadero RAD Studio编译,没用过Turbo C。
请高手赐教,并且给出相应代码。谢了。
老师要求如下:
输入C语言代码,然后提交,判断结果是否正确。
我当时的第一反应就是调用Turbo C,但是不知道怎么调用。小弟C语言学的比较菜,而且学C语言都是用的Embarcadero RAD Studio编译,没用过Turbo C。
请高手赐教,并且给出相应代码。谢了。
1,写一个自动编译的b.bat(TC木有,需要你自己查)
2,保存C语言代码为 xxx.c
3,System.Diagnostics.Process.Start( "b.bat", "xxx.c");
4,执行编译后的exe并获得退出时的返回值
第4步的风险性比较高,不建议用此方法.
通过Process.StandardOutput 属性 可以获取TC的编译信息;
异步的方式
注册OutputDataReceived事件
调用Process.BeginOutputReadLine 方法
//同步
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
// Define the namespaces used by this sample.
using System;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;namespace ProcessAsyncStreamSamples
{
class SortOutputRedirection
{
// Define static variables shared by class methods.
private static StringBuilder sortOutput = null;
private static int numOutputLines = 0; public static void SortInputListText()
{
// Initialize the process and its StartInfo properties.
// The sort command is a console application that
// reads and sorts text input. Process sortProcess;
sortProcess = new Process();
sortProcess.StartInfo.FileName = "Sort.exe"; // Set UseShellExecute to false for redirection.
sortProcess.StartInfo.UseShellExecute = false; // Redirect the standard output of the sort command.
// This stream is read asynchronously using an event handler.
sortProcess.StartInfo.RedirectStandardOutput = true;
sortOutput = new StringBuilder(""); // Set our event handler to asynchronously read the sort output.
sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler); // Redirect standard input as well. This stream
// is used synchronously.
sortProcess.StartInfo.RedirectStandardInput = true; // Start the process.
sortProcess.Start(); // Use a stream writer to synchronously write the sort input.
StreamWriter sortStreamWriter = sortProcess.StandardInput; // Start the asynchronous read of the sort output stream.
sortProcess.BeginOutputReadLine(); // Prompt the user for input text lines. Write each
// line to the redirected input stream of the sort command.
Console.WriteLine("Ready to sort up to 50 lines of text"); String inputText;
int numInputLines = 0;
do
{
Console.WriteLine("Enter a text line (or press the Enter key to stop):"); inputText = Console.ReadLine();
if (!String.IsNullOrEmpty(inputText))
{
numInputLines ++;
sortStreamWriter.WriteLine(inputText);
}
}
while (!String.IsNullOrEmpty(inputText) && (numInputLines < 50));
Console.WriteLine("<end of input stream>");
Console.WriteLine(); // End the input stream to the sort command.
sortStreamWriter.Close(); // Wait for the sort process to write the sorted text lines.
sortProcess.WaitForExit(); if (numOutputLines > 0)
{
// Write the formatted and sorted output to the console.
Console.WriteLine(" Sort results = {0} sorted text line(s) ",
numOutputLines);
Console.WriteLine("----------");
Console.WriteLine(sortOutput);
}
else
{
Console.WriteLine(" No input lines were sorted.");
} sortProcess.Close();
} private static void SortOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
numOutputLines++; // Add the text to the collected output.
sortOutput.Append(Environment.NewLine +
"[" + numOutputLines.ToString() + "] - " + outLine.Data);
}
}
}
}具体可以参考一下MSDN 查Process类