Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

How to check answers online?

$
0
0

First of all sorry , if such questions are not allowed here and if such questions are allowed then please help me.I am trying to write a Java code which will take source file in C , C++ and Java and will compile and execute it and will check the solution as code chef and other websites does.Now problem is that my code is going in infinite loop and i do not know why it is happening and please if you have better way to check codes then please share your view . Here is my code :

import java.io.*;
import java.util.Scanner;

class test
{
    public static void main(String args[])
    {
        String s=null,file_name,extension;
        int pos = args[0].lastIndexOf(".");

        extension = args[0].substring(pos+1);
        file_name = args[0].substring(0,pos);

        int lang = 0; //  1 -> c,c++ , 2 -> java


        try
        {

            Process compile = null;

            switch(extension)
            {
                case "c"    :    compile = Runtime.getRuntime().exec("gcc -g "+ args[0] + " -o "+file_name+" -lm");
                            lang = 1;           
                            break;
                case "cpp"  :    compile = Runtime.getRuntime().exec("g++ -g "+ args[0] + " -o "+file_name);
                            lang = 1;
                            break;
                case "java" :    compile = Runtime.getRuntime().exec("javac "+ args[0]);
                            lang = 2;
            }

            BufferedReader stdError = new BufferedReader(new InputStreamReader(compile.getErrorStream()));

            if((s = stdError.readLine()) != null)
            {
                System.out.println("Compile Time Error OR Warning : ");

                System.out.println(s);
                while((s = stdError.readLine()) != null)
                {
                    System.out.println(s);
                }
            }

            double startTime, run_time;
            Process run;

            if(lang == 1)
            {
                 startTime = System.nanoTime();

                 run = Runtime.getRuntime().exec("./"+file_name+" < "+file_name+"_input.txt > "+file_name+"_output.txt");

                 run_time = (System.nanoTime()-startTime)/(double)Math.pow(10,6);
            }
            else
            {

                startTime = System.nanoTime();

                 run = Runtime.getRuntime().exec("java "+file_name+" < "+file_name+"_input.txt > "+file_name+"_output.txt");

                 run_time = (System.nanoTime()-startTime)/(double)Math.pow(10,6);
            }



            System.out.println("RunTime : "+ run_time+" ms");

            BufferedReader out_put = new BufferedReader(new FileReader(new File(file_name+"_output.txt")));

            BufferedReader run_stdError = new BufferedReader(new InputStreamReader(run.getErrorStream()));


            if(( s = run_stdError.readLine()) != null)
            {
                System.out.println("Runtime Error : ");

                System.out.println(s);

                while((s = run_stdError.readLine()) != null )
                {
                    System.out.println(s);
                }
            }
            else if((s = out_put.readLine()) != null)
            {
                String s_string = null;
                int failed = 0;

                File fs = new File(file_name+".txt");

                BufferedReader br  = new BufferedReader(new FileReader(fs));

                if((!s.equals(s_string = br.readLine())))
                {
                    failed = 1;
                }

                while(((s = out_put.readLine()) != null) & ((s_string = br.readLine()) != null) & (failed == 0))
                {
                    if(!s.equals(s_string) )
                    {
                        failed = 1;
                        break;
                    }
                }

                if((failed == 1) || s != null || s_string != null)
                {
                    System.out.println("Submmision Failed : ");
                    System.out.println("Either Output Is Wrong.\nOR\nYour Output Is Not According To The Given Format. ");
                    System.exit(0);

                }
                else
                {
                    System.out.println("Submission Successful.");
                }

            }
        }   
        catch(IOException e)
        {
            System.out.println("Some Error Has Occured : ");
            e.printStackTrace();
            System.exit(-1);
        }

    }
}

Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>