Monday, April 1, 2013

Simple C++ code understanding


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <unistd.h>

#define Inf 2147483647
#define Pi acos(-1.0)
#define N 1000000
#define LL long long

inline LL Power(int b, int p) { LL ret = 1; for ( int i = 1; i <= p; i++ ) ret *= b; return ret; }

#define F(i, a, b) for( int i = (a); i < (b); i++ )
#define Fs(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fe(i, x) for(typeof (x.begin()) i = x.begin(); i != x.end (); i++)
#define Set(a, s) memset(a, s, sizeof (a))
#define max(a, b)  (a < b ? b : a)
#define min(a, b)  (a > b ? b : a)

#define FILENAME "modulo"   // input data file name
#define CASES 10            // number of input files 

using namespace std;

/*
    This is the code function
    Use this function as if it is the "main()" function  
*/

void callOut()
{
    int ret = 0;
    bool vis [42];
    int num;

    Set (vis, false);

    for ( int i = 0; i < 10; i++ ) {
        scanf ("%d", &num);

        vis [num % 42] = true;
    }

    for ( int i = 0; i < 42; i++ ) {
        if ( vis [i] ) {
            ret++;
        }
    }

 printf ("%d\n", ret);
}

bool startCase(int caseNum)
{
    string pathName = "test_data/";
    pathName += FILENAME;

    string inStream = ".in.";
    string outStream = ".out.";

    string caseNumber = static_cast<ostringstream*>( &(ostringstream() << caseNum) )->str();

    string inputPath = pathName + ".in." + caseNumber;
    string outputPath = pathName + ".out." + caseNumber;
    string thisOutputPath = pathName + ".thisOut." + caseNumber;

    //cout << "Input: " << inputPath << endl;
    //cout << "Output: " << outputPath << endl;
    //cout << "thisOutput: " << thisOutputPath << endl;

    int    fd;
    fpos_t pos;

    fflush(stdout);
    fgetpos(stdout, &pos);
    fd = dup(fileno(stdout));

    freopen (inputPath.c_str(), "r", stdin);
    freopen (thisOutputPath.c_str(), "w", stdout);

    callOut();

    fclose(stdin);

 fflush(stdout);
    dup2(fd, fileno(stdout));
    close(fd);
    clearerr(stdout);
    fsetpos(stdout, &pos);

 char bufferIn [N];
 char bufferOut [N];

 FILE *inFile = fopen (outputPath.c_str() , "r");
 FILE *outFile = fopen (thisOutputPath.c_str(), "r");

 do {
        size_t fpIn = fread (bufferIn, 1, N, inFile);
        size_t fpOut = fread (bufferOut, 1, N, outFile);

        if (fpIn != fpOut || memcmp (bufferIn, bufferOut, fpIn)) {
            return false;
        }
 } while (!feof (inFile) && !feof(outFile));

 return feof(inFile) && feof(outFile);
}

int main ()
{
    for ( int i = 1; i <= CASES; i++ ) {
        printf ("Case %d: ", i);
        if (startCase(i)) {
            printf ("*Yes*\n");
        } else {
            printf ("Wrong ans!\n");
        }
    }

 return 0;
}

// @END_OF_SOURCE_CODE

No comments:

Post a Comment