Ejemplo n.º 1
0
        //compilation
        public static CorrectTaskResult compileprogram(HWPathHelper filepath, string file)
        {
            string strOutput;
            string compilecommand;
            string exefilename     = Path.GetFileNameWithoutExtension(file);
            string exefilePath     = filepath.HWExeFilePath;
            string modifiedProgram = modifyProgram(file);
            //build a process
            Process cmd1 = new Process();

            cmd1.StartInfo.WorkingDirectory       = Path.GetDirectoryName(file);
            cmd1.StartInfo.FileName               = "cmd.exe";
            cmd1.StartInfo.UseShellExecute        = false; //是否使用操作系统shell啟動
            cmd1.StartInfo.RedirectStandardInput  = true;  //接受来自調用程序的输入信息
            cmd1.StartInfo.RedirectStandardOutput = true;  //由調用程序獲取输出信息
            cmd1.StartInfo.RedirectStandardError  = true;  //重定向標準錯誤输出
            cmd1.StartInfo.CreateNoWindow         = true;  //不顯示程序窗口
            compilecommand = @"g++ -g " + modifiedProgram + @" -o " + exefilePath + @"\" + exefilename + @" 2>" + filepath.HWCompErrorFilePath + @"\" + exefilename;
            try
            {
                cmd1.Start();//啟動程序
                cmd1.StandardInput.WriteLine(compilecommand);
                cmd1.StandardInput.WriteLine("exit");
                cmd1.WaitForExit();//等待編譯執行完退出進程
                cmd1.Close();
            }
            catch (Exception e)
            {
                strOutput = e.Message;
            }

            return(IsCompilationSuccess(filepath.HWCompErrorFilePath, exefilename));
        }
Ejemplo n.º 2
0
        //comparison
        public static void comparefile(HWPathHelper HWfilepath, string filename, string solutionfile)
        {
            string outputfile;
            string comparecommand;
            string erroutput;

            outputfile = HWfilepath.HWOutputFilePath + @"\" + filename + @".txt";
            Process cmd1 = new Process();

            cmd1.StartInfo.WorkingDirectory       = HWfilepath.HWOutputFilePath;
            cmd1.StartInfo.FileName               = "cmd.exe";
            cmd1.StartInfo.UseShellExecute        = false; //是否使用操作系统shell啟動
            cmd1.StartInfo.RedirectStandardInput  = true;  //接受来自調用程序的输入信息
            cmd1.StartInfo.RedirectStandardOutput = true;  //由調用程序獲取输出信息
            cmd1.StartInfo.RedirectStandardError  = true;  //重定向標準錯誤输出
            cmd1.StartInfo.CreateNoWindow         = true;  //不顯示程序窗口
            comparecommand = @"FC /W " + solutionfile + @" " + outputfile + @"> " + HWfilepath.HWresultFilePath + @"\" + filename;
            try
            {
                cmd1.Start();//啟動程序
                cmd1.StandardInput.WriteLine(comparecommand);
                cmd1.StandardInput.WriteLine("exit");
                cmd1.WaitForExit();//等待比較執行完退出進程
                cmd1.Close();
            }
            catch (Exception e)
            {
                erroutput = e.Message;
            }
        }
Ejemplo n.º 3
0
        public CorrectTaskReturn StartCorrect(HWPathHelper HWFilePath, string assignmentPath, Program_Answer answer)
        {
            AnswerPathHelper  CorrectAnswer = new AnswerPathHelper(answer.cAnswer_Input);
            string            filename;
            CorrectTaskReturn FinalResult;
            CorrectTaskResult compilationResult;
            CorrectTaskResult executionResult;

            filename             = Path.GetFileNameWithoutExtension(assignmentPath);
            FinalResult          = new CorrectTaskReturn();
            compilationResult    = new CorrectTaskResult();
            executionResult      = new CorrectTaskResult();
            compilationResult    = CorrectTaskHelper.compileprogram(HWFilePath, assignmentPath);
            FinalResult.resultID = filename;

            if (compilationResult.Success)
            {
                FinalResult.IsCompilationSuccess = true;
                DirectoryInfo direct = new DirectoryInfo(CorrectAnswer.AnswerInputFilePath);
                int           count  = 0;
                foreach (var file in direct.GetFiles(answer.cQID + "-*.txt"))
                {
                    count++;
                    string testnum = file.Name.Replace(".txt", "").Substring(file.Name.Replace(".txt", "").IndexOf("-"));
                    executionResult = CorrectTaskHelper.executeprogram(HWFilePath, filename, CorrectAnswer.AnswerInputFilePath + @"\" + file.Name);
                    if (executionResult.Success)
                    {
                        CorrectTaskHelper.adjustoutput(HWFilePath, filename + testnum);
                        CorrectTaskHelper.comparefile(HWFilePath, filename + testnum, CorrectAnswer.AnswerFilePath + @"\" + file.Name);
                        FinalResult.IsExecutionSuccess.Add(true);
                        FinalResult.IsComparsionSuccess.Add(CorrectTaskHelper.PassOrNot(HWFilePath, filename + testnum));
                        FinalResult.addcomparsion(CorrectTaskHelper.PassOrNot(HWFilePath, filename + testnum));
                    }
                    else
                    {
                        FinalResult.ExecutionErrorMessage += "測試" + testnum + @"執行錯誤:" + executionResult.errorMessage + "\r\n";
                    }
                }
                FinalResult.testnum = count;
            }
            else
            {
                FinalResult.ComplationErrorMessage = compilationResult.errorMessage;
            }

            return(FinalResult);
        }
Ejemplo n.º 4
0
        //determine comparison
        public static bool PassOrNot(HWPathHelper HWfilepath, string filename)
        {
            string       ComparisonErrorfile = HWfilepath.HWresultFilePath + @"\" + filename;
            StreamReader errorFile           = new StreamReader(ComparisonErrorfile, System.Text.Encoding.Default);
            string       errorString         = null;

            errorString = errorFile.ReadToEnd();
            errorFile.Close();
            var test = errorString.IndexOf("FC: 找不到相異處");

            if (test != -1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 5
0
        //get the execution result
        public static CorrectTaskResult IsExecutionSuccess(HWPathHelper HWfilepath, string filename)
        {
            CorrectTaskResult result             = new CorrectTaskResult(false);
            string            ExecutionErrorfile = HWfilepath.HWExeErrorFilePath + @"\" + filename;
            StreamReader      errorFile          = new StreamReader(ExecutionErrorfile, System.Text.Encoding.UTF8);
            string            errorString        = null;

            errorString = errorFile.ReadToEnd();
            errorFile.Close();
            if (errorString == "")
            {
                result.Success = true;
                return(result);
            }
            else
            {
                result.errorMessage = errorString;
                return(result);
            }
        }
Ejemplo n.º 6
0
        //adjust the output
        public static void adjustoutput(HWPathHelper HWfilepath, string filename)
        {
            string outputfile;

            string[] adjustedOutput;
            string   outputString;

            outputfile = HWfilepath.HWOutputFilePath + @"\" + filename + @".txt";
            StreamReader outputFile = new StreamReader(outputfile, System.Text.Encoding.Default);

            outputString = outputFile.ReadToEnd();
            outputFile.Close();
            adjustedOutput = outputString.Split(':');
            StreamWriter output = new StreamWriter(outputfile, false, System.Text.Encoding.Default);

            foreach (string line in adjustedOutput)
            {
                output.WriteLine(line);
            }
            output.Close();
        }
Ejemplo n.º 7
0
        //execution
        public static CorrectTaskResult executeprogram(HWPathHelper filepath, string filename, string inputfile)
        {
            string errOutput = null;
            string inputcommand;
            string outputResult;
            string error;
            string inputfilepath  = inputfile;
            string inputfileName  = Path.GetFileNameWithoutExtension(inputfile);
            string testnum        = inputfileName.Substring(inputfileName.IndexOf("-"));
            string outputfilepath = filepath.HWOutputFilePath + @"\" + filename + testnum + @".txt";
            string errorfilepath  = filepath.HWExeErrorFilePath + @"\" + filename + testnum;

            //build a reader, and the encoding choose ASCII
            StreamReader inputText = new StreamReader(inputfilepath, System.Text.Encoding.ASCII);
            Process      proc      = new Process();

            proc.StartInfo.WorkingDirectory       = filepath.HWExeFilePath;
            proc.StartInfo.FileName               = filepath.HWExeFilePath + @"\" + filename + @".exe";
            proc.StartInfo.UseShellExecute        = false; //是否使用操作系统shell啟動
            proc.StartInfo.RedirectStandardInput  = true;  //接受来自調用程序的输入信息
            proc.StartInfo.RedirectStandardOutput = true;  //由調用程序獲取输出信息
            proc.StartInfo.RedirectStandardError  = true;  //重定向標準錯誤输出
            proc.StartInfo.CreateNoWindow         = true;  //不顯示程序窗口

            try
            {
                proc.Start();//啟動程序
                StreamWriter commandStreamWriter = proc.StandardInput;
                //proc.BeginOutputReadLine();
                do
                {
                    inputcommand = inputText.ReadLine();
                    if (!String.IsNullOrEmpty(inputcommand))
                    {
                        commandStreamWriter.WriteLine(inputcommand);
                    }
                }while (!String.IsNullOrEmpty(inputcommand));
                commandStreamWriter.Close();
                inputText.Close();
                proc.WaitForExit(WaitExecutionTime);//等待程序執行完退出進程
                if (proc.HasExited == false)
                {
                    if (proc.Responding)
                    {
                        proc.Kill();
                        errOutput += "Time Limit Exceed. \r\n";
                        error      = proc.StandardError.ReadToEnd();
                        errOutput += error;
                    }
                    else
                    {
                        proc.Kill();
                        errOutput += " Runtime Error.\r\n";
                        error      = proc.StandardError.ReadToEnd();
                        errOutput += error;
                    }
                }
                else
                {
                    error      = proc.StandardError.ReadToEnd();
                    errOutput += error;
                    StreamReader reader = proc.StandardOutput;
                    outputResult = reader.ReadToEnd();
                    proc.Close();
                    StreamWriter outputfile = new StreamWriter(outputfilepath, false);
                    outputfile.Write(outputResult);
                    outputfile.Close();
                }
            }
            catch (Exception e)
            {
                errOutput = errOutput + e.Message;
                proc.Close();
            }
            finally
            {
                StreamWriter errorfile = new StreamWriter(errorfilepath, false);
                errorfile.Write(errOutput);
                errorfile.Close();
            }
            return(IsExecutionSuccess(filepath, filename + testnum));
        }
Ejemplo n.º 8
0
        public string CorrectTask(string ProgramFilePath, string cQID, string StuProgramFN, string questionNum)
        {
            try
            {
                List <HW_Exam>         StuHW   = new List <HW_Exam>();
                List <StuCouHWDe_prog> StuHWDe = new List <StuCouHWDe_prog>();
                Program_Answer         answer;

                if (StuProgramFN == "all")
                {
                    HWPathHelper HWFilePath = new HWPathHelper(ProgramFilePath);
                    HWFilePath.HWPathdir();
                    answer = this.Answerrepository.Get(x => x.cQID == cQID);
                    AnswerPathHelper         CorrectAnswer = new AnswerPathHelper(answer.cAnswer_Input);
                    List <string>            fileFullname  = new List <string>();
                    string                   fileName;
                    List <CorrectTaskReturn> FinalResult = new List <CorrectTaskReturn>();
                    DirectoryInfo            direct      = new DirectoryInfo(HWFilePath.HWProgFilePath);
                    foreach (var file in direct.EnumerateFiles("*.cpp", SearchOption.AllDirectories))
                    {
                        if (file.Name.IndexOf("afterMF") == -1)
                        {
                            fileFullname.Add(file.FullName);
                            fileName = Path.GetFileNameWithoutExtension(file.FullName);
                            StuCouHWDe_prog getdata = this.StuCouHWDerepository.Get(x => x.StuProgramFN == fileName);
                            StuHW.Add(this.HWrepository.Get(x => x.StuCouHWDe_ID == getdata.StuCouHWDe_ID));
                            StuHWDe.Add(getdata);
                        }
                    }
                    Parallel.ForEach(fileFullname, (onefile, loopState) =>
                    {
                        FinalResult.Add(StartCorrect(HWFilePath, onefile, answer));
                    });
                    foreach (var result in FinalResult)
                    {
                        StuCouHWDe_prog StuHWDeNow = StuHWDe.Find(x => x.StuProgramFN == result.resultID);
                        HW_Exam         StuHWNow   = StuHW.Find(x => x.StuCouHWDe_ID == StuHWDeNow.StuCouHWDe_ID);
                        StuHWNow.HW_Exam_grade       = result.score(questionNum);
                        StuHWDeNow.Pass_compilation  = result.ComplationErrorMessage;
                        StuHWDeNow.Success_execution = result.ExecutionErrorMessage;
                        StuHWDeNow.Compare_situation = result.compareresult.TrimEnd(',');
                        this.HWrepository.Update(StuHWNow);
                        this.StuCouHWDerepository.Update(StuHWDeNow);
                    }
                }
                else
                {
                    HWPathHelper HWFilePath = new HWPathHelper(ProgramFilePath);
                    HWFilePath.HWPathdir();
                    answer = this.Answerrepository.Get(x => x.cQID == cQID);
                    AnswerPathHelper  CorrectAnswer = new AnswerPathHelper(answer.cAnswer_Input);
                    DirectoryInfo     direct        = new DirectoryInfo(HWFilePath.HWProgFilePath);
                    string            fileName;
                    CorrectTaskReturn FinalResult = new CorrectTaskReturn();
                    foreach (var file in direct.EnumerateFiles(StuProgramFN + ".cpp", SearchOption.AllDirectories))
                    {
                        if (file.Name.IndexOf("afterMF") == -1)
                        {
                            StuCouHWDe_prog StuHWDeNow = StuHWDe.Find(x => x.StuProgramFN == FinalResult.resultID);
                            HW_Exam         StuHWNow   = StuHW.Find(x => x.StuCouHWDe_ID == StuHWDeNow.StuCouHWDe_ID);
                            fileName                     = Path.GetFileNameWithoutExtension(file.FullName);
                            FinalResult                  = StartCorrect(HWFilePath, file.FullName, answer);
                            StuHWNow.HW_Exam_grade       = FinalResult.score(questionNum);
                            StuHWDeNow.Pass_compilation  = FinalResult.ComplationErrorMessage;
                            StuHWDeNow.Success_execution = FinalResult.ExecutionErrorMessage;
                            StuHWDeNow.Compare_situation = FinalResult.compareresult.TrimEnd(',');
                            this.HWrepository.Update(StuHWNow);
                            this.StuCouHWDerepository.Update(StuHWDeNow);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                return("Some errors happen in the process when marking Programs!");
            }
            return("It is success when marking Programs!");
        }