Advertisement
arjunarul

Input testfile formatting - Validator template

Apr 5th, 2019 (edited)
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1.  
  2.  
  3.  
  4. // Constraints + Formatting Validator
  5.  
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8.  
  9. // -------------------- Input Checker Start --------------------
  10.  
  11. // This function reads a long long, character by character, and returns it as a whole long long. It makes sure that it lies in the range [l, r], and the character after the long long is endd. l and r should be in [-1e18, 1e18].
  12. long long readInt(long long l, long long r, char endd)
  13. {
  14.     long long x = 0;
  15.     int cnt = 0, fi = -1;
  16.     bool is_neg = false;
  17.     while(true)
  18.     {
  19.         char g = getchar();
  20.         if(g == '-')
  21.         {
  22.             if(!(fi == -1))
  23.                 cerr << "- in between integer\n";
  24.             assert(fi == -1);
  25.             is_neg = true; // It's a negative integer
  26.             continue;
  27.         }
  28.         if('0' <= g && g <= '9')
  29.         {
  30.             x *= 10;
  31.             x += g - '0';
  32.             if(cnt == 0)
  33.                 fi = g - '0'; // fi is the first digit
  34.             cnt++;
  35.            
  36.             // There shouldn't be leading zeroes. eg. "02" is not valid and assert will fail here.
  37.             if(!(fi != 0 || cnt == 1))
  38.                 cerr << "Leading zeroes found\n";
  39.             assert(fi != 0 || cnt == 1);
  40.            
  41.             // "-0" is invalid
  42.             if(!(fi != 0 || is_neg == false))
  43.                 cerr << "-0 found\n";
  44.             assert(fi != 0 || is_neg == false);
  45.            
  46.             // The maximum number of digits should be 19, and if it is 19 digits long, then the first digit should be a '1'.
  47.             if(!(!(cnt > 19 || (cnt == 19 && fi > 1))))
  48.                 cerr << "Value greater than 1e18 found\n";
  49.             assert(!(cnt > 19 || (cnt == 19 && fi > 1)));
  50.         }
  51.         else if(g == endd)
  52.         {
  53.             if(is_neg)
  54.                 x = -x;
  55.             if(!(l <= x && x <= r))
  56.             {
  57.                 // We've reached the end, but the long long isn't in the right range.
  58.                 cerr << "Constraint violated: Lower Bound = " << l << " Upper Bound = " << r << " Violating Value = " << x << '\n';
  59.                 assert(false);
  60.             }
  61.             return x;
  62.         }
  63.         else if((g == ' ') && (endd == '\n'))
  64.         {
  65.             cerr << "Extra space found. It should instead have been a new line.\n";
  66.             assert(false);
  67.         }
  68.         else if((g == '\n') && (endd == ' '))
  69.         {
  70.             cerr << "A new line found where it should have been a space.\n";
  71.             assert(false);
  72.         }
  73.         else
  74.         {
  75.             cerr << "Something weird has happened.\n";
  76.             assert(false);
  77.         }
  78.     }
  79. }
  80.  
  81. string readString(int l, int r, char endd)
  82. {
  83.     string ret = "";
  84.     int cnt = 0;
  85.     while(true)
  86.     {
  87.         char g = getchar();
  88.        
  89.         assert(g != -1);
  90.         if(g == endd)
  91.             break;
  92.         cnt++;
  93.         ret += g;
  94.     }
  95.     if(!(l <= cnt && cnt <= r))
  96.         cerr << "String length not within constraints\n";
  97.     assert(l <= cnt && cnt <= r);
  98.     return ret;
  99. }
  100.  
  101. long long readIntSp(long long l, long long r) { return readInt(l, r, ' '); }
  102. long long readIntLn(long long l, long long r) { return readInt(l, r, '\n'); }
  103. string readStringLn(int l, int r) { return readString(l, r, '\n'); }
  104. string readStringSp(int l, int r) { return readString(l, r, ' '); }
  105. void readEOF()
  106. {
  107.     char g = getchar();
  108.     if(g != EOF)
  109.     {
  110.         if(g == ' ')
  111.             cerr << "Extra space found where the file shold have ended\n";
  112.         if(g == '\n')
  113.             cerr << "Extra newline found where the file shold have ended\n";
  114.         else
  115.             cerr << "File didn't end where expected\n";
  116.     }
  117.     assert(g == EOF);
  118. }
  119.  
  120. vector<int> readVectorInt(int n, long long l, long long r)
  121. {
  122.     vector<int> a(n);
  123.     for(int i = 0; i < n - 1; i++)
  124.         a[i] = readIntSp(l, r);
  125.     a[n - 1] = readIntLn(l, r);
  126.     return a;
  127. }
  128.  
  129. // -------------------- Input Checker End --------------------
  130.  
  131. int main(){
  132.     ios::sync_with_stdio(false);cin.tie(0);
  133.    
  134. // CODE TO BE MODIFIED 
  135.    
  136.     int t, x;
  137.     t=readIntLn(1,100); // This reads an integer whose value is 1 <= t <= 100, and the line should end right after this integer.
  138.     while(t--)
  139.     {
  140.         x=readIntLn(1,1000); // This reads an integer whose value is 1 <= x <= 1000, and the line should end right after this integer.
  141.     }
  142.     readEOF(); // This ensures that there is nothing more to read in the input file.
  143. }
  144.  
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement