Cod sursa(job #906135)
#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <functional>
#include <numeric>
#include <algorithm>
class TextTokenizer
{
private:
const char* inFileName_;
const char* outFileName_;
unsigned long noWords_;
unsigned long noChars_;
public:
TextTokenizer(const char* inFileName, const char* outFileName)
: outFileName_(outFileName), inFileName_(inFileName), noWords_(0), noChars_(0)
{
}
unsigned long wordsAvg()
{
std::ifstream in;
std::ofstream out;
std::string crt_line;
std::string prev_line = "";
unsigned long wc;
int a = 0;
in.open(inFileName_);
out.open(outFileName_);
while (!in.eof()) {
std::getline(in, crt_line);
wc = 0;
if (crt_line != prev_line) {
prev_line = crt_line;
if (crt_line.empty()) {
wc = 0;
} else {
wc = std::isalpha(*crt_line.begin()) ? 1 : 0;
for (std::string::const_iterator pen = crt_line.begin();
++ pen != crt_line.end();) {
noChars_ += std::isalpha(pen[0]) ? 1 : 0;
wc += (std::isalpha(pen[0]) ? 1 : 0) &&
(std::isalpha(pen[-1]) ? 0 : 1);
}
}
}
noWords_ += wc;
}
out << noChars_ / noWords_;
in.close();
out.close();
}
};
int main(int argc, char** argv)
{
TextTokenizer tk("text.in", "text.out");
tk.wordsAvg();
return 0;
}