Pagini recente » Cod sursa (job #1928340) | Cod sursa (job #2707523) | Cod sursa (job #323099) | Cod sursa (job #1519547) | Cod sursa (job #906160)
Cod sursa(job #906160)
#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#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)
{
}
void wordsAvg()
{
std::ifstream in;
std::ofstream out;
std::string crt_line;
std::string prev_line = "";
unsigned long wc = 0;
unsigned long avg = 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 {
noChars_ += (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;
}
avg = noWords_ == 0 ? 0 : noChars_ / noWords_;
out << avg;
in.close();
out.close();
}
};
int main(int argc, char** argv)
{
TextTokenizer tk("text.in", "text.out");
tk.wordsAvg();
return 0;
}