using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace ua.org.uoi
{
public enum CheckResult
{
NotChecked,
OK,
WrongAnswer,
BadFile,
NoFile,
Timeout,
RTE,
HasOutput,
CheckerError,
CompileError,
PresentationError,
CheckerProblem
};
public class Checker
{
private double Dist(double x, double y)
{
return Math.Sqrt(x * x + y * y);
}
///
/// Check problem DIVIDE
///
/// input file
/// etalon file
/// solution file
/// Result of check
public CheckResult CheckDivide(byte[] dat, byte[] eta, byte[] sol)
{
double EPS_COINCIDE = 1e-4;
double EPS_DIST = 1e-2;
string strEta, strSol;
bool bad_presentation = false;
try
{
System.IO.StreamReader inpStream = new StreamReader(new System.IO.MemoryStream(sol));
strSol = inpStream.ReadToEnd();
}
catch
{
return CheckResult.BadFile;
}
try
{
System.IO.StreamReader inpStream = new StreamReader(new System.IO.MemoryStream(eta));
strEta = inpStream.ReadToEnd();
}
catch
{
return CheckResult.CheckerProblem;
}
{
int spaces = 0;
for (int i = 0; i < strSol.Length; ++i)
if (strSol[i] == ' ')
spaces++;
if (spaces != 3)
bad_presentation = true;
if (!strSol.EndsWith("\r\n"))
bad_presentation = true;
if (strSol.IndexOf("\r\n") != strSol.LastIndexOf("\r\n"))
bad_presentation = true;
strSol = strSol.Replace("\r\n", " ");
while (strSol.IndexOf(" ") >= 0)
strSol = strSol.Replace(" ", " ");
}
{
while (strEta.IndexOf(" ") >= 0)
strEta = strEta.Replace(" ", " ");
strEta = strEta.Trim();
}
double ex1, ey1, ex2, ey2;
double x1=0, y1=0, x2=0, y2=0;
try
{
string [] str = strSol.Trim().Split(new char [] {' '});
int cur = 0;
for (int i = 0; i < str.Length; ++i)
if (str[i].Trim().Length == 0)
return CheckResult.WrongAnswer;
else
{
try
{
double el = System.Convert.ToDouble(str[i]);
if (cur == 0)
x1 = el;
else if (cur == 1)
y1 = el;
else if (cur ==2 )
x2 = el;
else if (cur == 3)
y2 = el;
else
return CheckResult.WrongAnswer;
cur++;
}
catch
{
return CheckResult.WrongAnswer;
}
}
}
catch
{
return CheckResult.WrongAnswer;
}
try
{
int i1, i2, i3;
i1 = strEta.IndexOf(' ');
i3 = strEta.LastIndexOf(' ');
for (i2 = i1+1; strEta[i2] != ' '; ++i2);
ex1 = System.Convert.ToDouble(strEta.Substring(0, i1));
ey1 = System.Convert.ToDouble(strEta.Substring(i1, i2-i1));
ex2 = System.Convert.ToDouble(strEta.Substring(i2, i3-i2));
ey2 = System.Convert.ToDouble(strEta.Substring(i3));
}
catch
{
return CheckResult.CheckerProblem;
}
double a, b, c, d;
a = ey1 - ey2;
b = ex2 - ex1;
c = ex1 * ey2 - ex2 * ey1;
// normalizing
d = Dist(a, b);
a /= d;
b /= d;
c /= d;
// check if given point coincide
if (Dist(x1 - x2, y1 - y2) < EPS_COINCIDE)
return CheckResult.WrongAnswer;
if (Math.Abs(a * x1 + b * y1 + c) < EPS_DIST && Math.Abs(a * x2 + b * y2 + c) < EPS_DIST)
{
// answer seems to be ok, points do not coincide
// check for presentation error
if (bad_presentation)
return CheckResult.PresentationError;
else
return CheckResult.OK;
}
return CheckResult.WrongAnswer;
}
}
}