Pagini recente » Cod sursa (job #1518591) | Cod sursa (job #36282) | Cod sursa (job #474692) | Cod sursa (job #2224204) | Cod sursa (job #1558117)
#include <fstream>
#include <cstdio>
#include <string>
bool isLetter(char x) {
return ('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z');
}
int wordDensity(const std::string& text) {
bool isChain = false;
int wordCount = 0, wordTotalLength = 0;
for (auto it = text.cbegin(); it != text.cend(); ++it)
if (isLetter(*it)) {
isChain = true;
++wordTotalLength;
} else if (isChain) {
isChain = false;
++wordCount;
}
if (isChain)
++wordCount;
if (wordCount)
return wordTotalLength / wordCount;
return 0;
}
int main() {
char const * const inputFile = "text.in",
* const outputFile = "text.out";
std::ifstream in(inputFile);
std::ofstream out(outputFile);
std::string line, text;
while (std::getline(in, line)) text += line;
out << wordDensity(text);
return 0;
}