Pagini recente » Cod sursa (job #1380784) | Cod sursa (job #3345369) | Cod sursa (job #3345905) | Cod sursa (job #1749189) | Cod sursa (job #3317680)
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
bool isLetter(char c) {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
int returnWordsNumber(string line, int &totalLetters) {
int length = line.size();
int hasWord = 0;
int numbersOfWords = 0;
for (int i = 0; i < length; ++i) {
if (isLetter(line[i]) && hasWord == 0) {
hasWord = 1;
++totalLetters;
} else if (isLetter(line[i]) && hasWord == 1) {
++totalLetters;
} else if (!isLetter(line[i]) && hasWord == 1) {
++numbersOfWords;
hasWord = 0;
}
}
if (hasWord == 1) {
++numbersOfWords;
}
return numbersOfWords;
}
int main() {
ifstream fin("text.in");
ofstream fout("text.out");
string line;
int totalLetters = 0;
int totalWordsNumber = 0;
while (!fin.eof()) {
getline(fin, line);
totalWordsNumber += returnWordsNumber(line, totalLetters);
}
if (totalWordsNumber != 0) {
fout << totalLetters / totalWordsNumber;
} else {
fout << 0;
}
fout.close();
return 0;
}