Cod sursa(job #974390)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 17 iulie 2013 01:40:59
Problema Text Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <fstream>
#include <iostream>
#include <cstring>
#include <iterator>
#include <algorithm>

#define MAX_SIZE (1024 * 1024 * 1024 + 1)

using namespace std;

char text[MAX_SIZE];

int main()
{
    fstream fin("text.in", fstream::in);
    fstream fout("text.out", fstream::out);   
    
    fin.seekg (0, fin.end);
    int length = fin.tellg();
    fin.seekg (0, fin.beg);
    
    fin.read(text, length);
    
    //cout << text << endl;
    
    int numWords = 0;
    int wordsLength = 0;
    int currentWordLength = 0;
    
    for (int i=0; i<length; ++i)
    {
        if (isalpha(text[i]))
        {
            currentWordLength++;
        }
        else
        {
            if (currentWordLength > 0)
            {
                numWords++;
                wordsLength += currentWordLength;
            }

            currentWordLength = 0;
        }
    }
    
    //cout << numWords << " " << wordsLength << endl;
    fout << wordsLength / numWords << "\n";

    return 0;
}