arjunarul

Custom Judge to ignore relative or absolute error upto 10^-6

Feb 8th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. const int
  8.         RV_POSITIVE = 0,
  9.         RV_NEGATIVE = 1,
  10.         RV_INTERNALERROR = 2;
  11. const int
  12.         MAX_NUMSTRLEN = 64,
  13.         TP_DOUBLE = 1,
  14.         TP_CHAR = 2,
  15.         TP_EOF = 3;
  16.  
  17. double epsilon=0.000001;
  18.  
  19. // <0-255> normal, -1 white, -2 eof
  20.  
  21. class lexem
  22. {
  23. private:
  24.     char c;
  25.     double d;
  26.  
  27. public:
  28.  
  29.     int type;
  30.    
  31.     lexem get ()
  32.     {
  33.         type = TP_EOF;
  34.         return *this;
  35.     }
  36.  
  37.     lexem get (char _c)
  38.     {
  39.         type = TP_CHAR;
  40.         c = _c;
  41.         return *this;
  42.     }
  43.  
  44.     lexem get (double _d)
  45.     {
  46.         type = TP_DOUBLE;
  47.         d = _d;
  48.         return *this;
  49.     }
  50.  
  51.     static bool equal (lexem a, lexem b)
  52.     {
  53.         if (a.type != b.type) return false;
  54.         if (a.type==TP_EOF) return true;
  55.         if (a.type==TP_CHAR) return (a.c==b.c);
  56.         if (fabs(a.d-b.d)<=epsilon) return true;
  57.         if (fabs(a.d)<=epsilon || fabs(b.d)<=epsilon) return false;
  58.         return fabs(a.d-b.d)/fmax(fabs(a.d),fabs(b.d))<=epsilon;
  59.     }
  60. };
  61.  
  62.  
  63. class myFileReader
  64. {
  65.     char buf[MAX_NUMSTRLEN+2];
  66.     bool last_was_whitespace;
  67.     int buf_pos, buf_end;
  68.     lexem last;
  69.  
  70. public:
  71.  
  72.     myFileReader ()
  73.     {
  74.         last_was_whitespace=true;
  75.         buf_pos=buf_end=0;
  76.     }
  77.  
  78.     lexem getChar (FILE *f)
  79.     {
  80.         bool white;
  81.         char ch;
  82.         if (!last_was_whitespace && buf_pos<buf_end)
  83.         {
  84.             buf_pos++;
  85.             if (buf[buf_pos-1]==' ') last_was_whitespace=true;
  86.             return last.get(buf[buf_pos-1]);
  87.         }
  88.         do
  89.         {
  90.             ch=getc (f);
  91.             if (ch==EOF)
  92.             {
  93.                 if (last_was_whitespace)
  94.                     return last.get();
  95.                 else
  96.                 {
  97.                     last_was_whitespace=true;
  98.                     return last.get(' ');
  99.                 }
  100.             }
  101.             if (ch==' ' || ch=='\n' || ch=='\t' || ch=='\r')
  102.                 white=true;
  103.             else white=false;
  104.         }
  105.         while (white && last_was_whitespace);
  106.         if (white)
  107.         {
  108.             last_was_whitespace=true;
  109.             return last.get (' ');
  110.         }
  111.         if (!last_was_whitespace)
  112.             return last.get (ch);
  113.         last_was_whitespace=false;
  114.         bool is_a_number = true, has_had_dot = false;
  115.         buf_pos=0;
  116.         buf_end=0;
  117.         while (buf_end<MAX_NUMSTRLEN)
  118.         {
  119.             if (ch=='\n' || ch=='\t' || ch=='\r')
  120.                 ch=' ';
  121.             buf[buf_end]=ch;
  122.             if (ch==' ')
  123.                 break;
  124.             if (( ch<'0' || ch>'9' ) && (ch!='.' || has_had_dot) && ch!='-' && ch!='+' && ch!='e' && ch!='E')
  125.             {
  126.                 is_a_number=false;
  127.                 break;
  128.             }
  129.             if (ch=='.') has_had_dot=true;
  130.             ch=getc(f);
  131.             if (ch==EOF) ch=' ';
  132.             buf_end++;
  133.         }
  134.         if (buf_end==MAX_NUMSTRLEN) is_a_number=false;
  135.             else buf_end++;
  136.         if (!is_a_number)
  137.         {
  138.             buf_pos=1;
  139.             return last.get(buf[0]);
  140.         }
  141.         else
  142.         {
  143.             double f;
  144.             buf[buf_end]=0;
  145.             char *sc;
  146.             f=strtod (buf, &sc);
  147.             if(*sc)
  148.             {
  149.                 buf_end=1;
  150.                 buf_pos=0;
  151.                 buf[0]=' ';
  152.                 return last.get(f);
  153.             }
  154.             buf_pos++;
  155.             return last.get(buf[0]);
  156.         }
  157.     }
  158. };
  159.  
  160. int main(int argc, char *argv[]) {
  161.         FILE *file1 = fdopen(3, "r");
  162.         if (!file1)
  163.                 exit(RV_INTERNALERROR);
  164.        
  165.         FILE *file2 = fdopen(4, "r");
  166.         if (!file2)
  167.                 exit(RV_INTERNALERROR);
  168.        
  169.         epsilon*=1.00001;
  170.        
  171.         myFileReader f1, f2;
  172.         lexem l1 = f1.getChar(file1);
  173.         lexem l2 = f2.getChar(file2);
  174.         while (lexem::equal(l1,l2))
  175.         {
  176.                 if (l1.type==TP_EOF)
  177.                     exit(RV_POSITIVE);
  178.                 l1 = f1.getChar(file1);
  179.                 l2 = f2.getChar(file2);
  180.         };
  181.         return RV_NEGATIVE;
  182. }
Add Comment
Please, Sign In to add comment