diff --git a/1-ConsecutiveHeads/ConsecutiveHeads.pro b/1-ConsecutiveHeads/ConsecutiveHeads.pro
index 85bdbce..15fef49 100644
--- a/1-ConsecutiveHeads/ConsecutiveHeads.pro
+++ b/1-ConsecutiveHeads/ConsecutiveHeads.pro
@@ -110,7 +110,7 @@ win32 {
QMAKE_LFLAGS += -Wl,--stack,536870912
LIBS += -lDbghelp
LIBS += -lbfd
- LIBS += -liberty
+ # LIBS += -liberty
LIBS += -limagehlp
}
macx {
diff --git a/1-ConsecutiveHeads/ConsecutiveHeads.pro.user.8ad8611 b/1-ConsecutiveHeads/ConsecutiveHeads.pro.user.8ad8611
new file mode 100644
index 0000000..2a3c445
--- /dev/null
+++ b/1-ConsecutiveHeads/ConsecutiveHeads.pro.user.8ad8611
@@ -0,0 +1,259 @@
+
+
+
+
+
+ EnvironmentId
+ {8ad86111-aa40-444e-8d9e-29a91329f9e6}
+
+
+ ProjectExplorer.Project.ActiveTarget
+ 0
+
+
+ ProjectExplorer.Project.EditorSettings
+
+ true
+ false
+ true
+
+ Cpp
+
+ CppGlobal
+
+
+
+ QmlJS
+
+ QmlJSGlobal
+
+
+ 2
+ UTF-8
+ false
+ 4
+ false
+ 80
+ true
+ true
+ 1
+ true
+ false
+ 0
+ true
+ 0
+ 8
+ true
+ 1
+ true
+ true
+ true
+ false
+
+
+
+ ProjectExplorer.Project.PluginSettings
+
+
+
+ ProjectExplorer.Project.Target.0
+
+ Desktop Qt 5.5.0 MinGW 32bit
+ Desktop Qt 5.5.0 MinGW 32bit
+ qt.55.win32_mingw492_kit
+ 0
+ 0
+ 0
+
+ D:/Sunny4KA/CS/Cpp/Assignments/cs-b-assignment1/build-ConsecutiveHeads-Desktop_Qt_5_5_0_MinGW_32bit-Debug
+
+
+ true
+ qmake
+
+ QtProjectManager.QMakeBuildStep
+ false
+ true
+
+ false
+ false
+ false
+
+
+ true
+ Сборка
+
+ Qt4ProjectManager.MakeStep
+
+ false
+
+
+
+ 2
+ Сборка
+
+ ProjectExplorer.BuildSteps.Build
+
+
+
+ true
+ Сборка
+
+ Qt4ProjectManager.MakeStep
+
+ true
+ clean
+
+
+ 1
+ Очистка
+
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ Отладка
+
+ Qt4ProjectManager.Qt4BuildConfiguration
+ 2
+ true
+
+
+ D:/Sunny4KA/CS/Cpp/Assignments/cs-b-assignment1/build-ConsecutiveHeads-Desktop_Qt_5_5_0_MinGW_32bit-Release
+
+
+ true
+ qmake
+
+ QtProjectManager.QMakeBuildStep
+ false
+ true
+
+ false
+ false
+ false
+
+
+ true
+ Сборка
+
+ Qt4ProjectManager.MakeStep
+
+ false
+
+
+
+ 2
+ Сборка
+
+ ProjectExplorer.BuildSteps.Build
+
+
+
+ true
+ Сборка
+
+ Qt4ProjectManager.MakeStep
+
+ true
+ clean
+
+
+ 1
+ Очистка
+
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ Выпуск
+
+ Qt4ProjectManager.Qt4BuildConfiguration
+ 0
+ true
+
+ 2
+
+
+ 0
+ Установка
+
+ ProjectExplorer.BuildSteps.Deploy
+
+ 1
+ Локальная установка
+
+ ProjectExplorer.DefaultDeployConfiguration
+
+ 1
+
+
+
+ false
+ false
+ false
+ false
+ true
+ 0.01
+ 10
+ true
+ 1
+ 25
+
+ 1
+ true
+ false
+ true
+ valgrind
+
+ 0
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+ 10
+ 11
+ 12
+ 13
+ 14
+
+ 2
+
+ ConsecutiveHeads
+
+ Qt4ProjectManager.Qt4RunConfiguration:D:/Sunny4KA/CS/Cpp/Assignments/cs-b-assignment1/1-ConsecutiveHeads/ConsecutiveHeads.pro
+
+ ConsecutiveHeads.pro
+ false
+ false
+
+ 3768
+ false
+ true
+ false
+ false
+ true
+
+ 1
+
+
+
+ ProjectExplorer.Project.TargetCount
+ 1
+
+
+ ProjectExplorer.Project.Updater.FileVersion
+ 18
+
+
+ Version
+ 18
+
+
diff --git a/1-ConsecutiveHeads/ConsecutiveHeads.pro.user.e083ff4 b/1-ConsecutiveHeads/ConsecutiveHeads.pro.user.e083ff4
new file mode 100644
index 0000000..8c2a6c9
--- /dev/null
+++ b/1-ConsecutiveHeads/ConsecutiveHeads.pro.user.e083ff4
@@ -0,0 +1,259 @@
+
+
+
+
+
+ EnvironmentId
+ {e083ff44-9a0b-4415-87f5-c1098ab0ba6f}
+
+
+ ProjectExplorer.Project.ActiveTarget
+ 0
+
+
+ ProjectExplorer.Project.EditorSettings
+
+ true
+ false
+ true
+
+ Cpp
+
+ CppGlobal
+
+
+
+ QmlJS
+
+ QmlJSGlobal
+
+
+ 2
+ UTF-8
+ false
+ 4
+ false
+ 80
+ true
+ true
+ 1
+ true
+ false
+ 0
+ true
+ 0
+ 8
+ true
+ 1
+ true
+ true
+ true
+ false
+
+
+
+ ProjectExplorer.Project.PluginSettings
+
+
+
+ ProjectExplorer.Project.Target.0
+
+ Desktop Qt 5.5.0 MinGW 32bit
+ Desktop Qt 5.5.0 MinGW 32bit
+ qt.55.win32_mingw492_kit
+ 0
+ 0
+ 0
+
+ D:/Sunny4KA/CS/Cpp/Assignments/cs-b-assignment1/build-ConsecutiveHeads-Desktop_Qt_5_5_0_MinGW_32bit-Debug
+
+
+ true
+ qmake
+
+ QtProjectManager.QMakeBuildStep
+ false
+ true
+
+ false
+ false
+ false
+
+
+ true
+ Сборка
+
+ Qt4ProjectManager.MakeStep
+
+ false
+
+
+
+ 2
+ Сборка
+
+ ProjectExplorer.BuildSteps.Build
+
+
+
+ true
+ Сборка
+
+ Qt4ProjectManager.MakeStep
+
+ true
+ clean
+
+
+ 1
+ Очистка
+
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ Отладка
+
+ Qt4ProjectManager.Qt4BuildConfiguration
+ 2
+ true
+
+
+ D:/Sunny4KA/CS/Cpp/Assignments/cs-b-assignment1/build-ConsecutiveHeads-Desktop_Qt_5_5_0_MinGW_32bit-Release
+
+
+ true
+ qmake
+
+ QtProjectManager.QMakeBuildStep
+ false
+ true
+
+ false
+ false
+ false
+
+
+ true
+ Сборка
+
+ Qt4ProjectManager.MakeStep
+
+ false
+
+
+
+ 2
+ Сборка
+
+ ProjectExplorer.BuildSteps.Build
+
+
+
+ true
+ Сборка
+
+ Qt4ProjectManager.MakeStep
+
+ true
+ clean
+
+
+ 1
+ Очистка
+
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ Выпуск
+
+ Qt4ProjectManager.Qt4BuildConfiguration
+ 0
+ true
+
+ 2
+
+
+ 0
+ Установка
+
+ ProjectExplorer.BuildSteps.Deploy
+
+ 1
+ Локальная установка
+
+ ProjectExplorer.DefaultDeployConfiguration
+
+ 1
+
+
+
+ false
+ false
+ false
+ false
+ true
+ 0.01
+ 10
+ true
+ 1
+ 25
+
+ 1
+ true
+ false
+ true
+ valgrind
+
+ 0
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+ 10
+ 11
+ 12
+ 13
+ 14
+
+ 2
+
+ ConsecutiveHeads
+ ConsecutiveHeads2
+ Qt4ProjectManager.Qt4RunConfiguration:D:/CS/Cpp/Assignments/cs-b-assignment1/1-ConsecutiveHeads/ConsecutiveHeads.pro
+
+ ConsecutiveHeads.pro
+ false
+ false
+
+ 3768
+ false
+ true
+ false
+ false
+ true
+
+ 1
+
+
+
+ ProjectExplorer.Project.TargetCount
+ 1
+
+
+ ProjectExplorer.Project.Updater.FileVersion
+ 18
+
+
+ Version
+ 18
+
+
diff --git a/1-ConsecutiveHeads/src/ConsecutiveHeads.cpp b/1-ConsecutiveHeads/src/ConsecutiveHeads.cpp
index 4b7f6b3..335d50c 100644
--- a/1-ConsecutiveHeads/src/ConsecutiveHeads.cpp
+++ b/1-ConsecutiveHeads/src/ConsecutiveHeads.cpp
@@ -4,7 +4,27 @@
#include "random.h"
using namespace std;
+/*
+ * Coin flip simulator. Сauses the random number generator which returns true or false
+ * until it randomly gives three times in a row the value true that would mean heads.
+ * At the end it displays a message about the number of flips that are needed to achieve results.
+ */
int main() {
- // TODO: fill in the code
- return 0;
+ int consecutiveHeads = 3; //the required count of consecutive heads for finishing the game;
+ int countOfHeads = 0;
+ int countOfFlips = 0;
+ while(countOfHeads < consecutiveHeads) {
+ if(randomBool()){ //if true then we assume that it is heads
+ cout << "heads" <
+
+
+
+
+ EnvironmentId
+ {e083ff44-9a0b-4415-87f5-c1098ab0ba6f}
+
+
+ ProjectExplorer.Project.ActiveTarget
+ 0
+
+
+ ProjectExplorer.Project.EditorSettings
+
+ true
+ false
+ true
+
+ Cpp
+
+ CppGlobal
+
+
+
+ QmlJS
+
+ QmlJSGlobal
+
+
+ 2
+ UTF-8
+ false
+ 4
+ false
+ 80
+ true
+ true
+ 1
+ true
+ false
+ 0
+ true
+ 0
+ 8
+ true
+ 1
+ true
+ true
+ true
+ false
+
+
+
+ ProjectExplorer.Project.PluginSettings
+
+
+
+ ProjectExplorer.Project.Target.0
+
+ Desktop Qt 5.5.0 MinGW 32bit
+ Desktop Qt 5.5.0 MinGW 32bit
+ qt.55.win32_mingw492_kit
+ 0
+ 0
+ 0
+
+ D:/CS/Cpp/Assignments/cs-b-assignment1/build-Obenglobish-Desktop_Qt_5_5_0_MinGW_32bit-Debug
+
+
+ true
+ qmake
+
+ QtProjectManager.QMakeBuildStep
+ false
+ true
+
+ false
+ false
+ false
+
+
+ true
+ Сборка
+
+ Qt4ProjectManager.MakeStep
+
+ false
+
+
+
+ 2
+ Сборка
+
+ ProjectExplorer.BuildSteps.Build
+
+
+
+ true
+ Сборка
+
+ Qt4ProjectManager.MakeStep
+
+ true
+ clean
+
+
+ 1
+ Очистка
+
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ Отладка
+
+ Qt4ProjectManager.Qt4BuildConfiguration
+ 2
+ true
+
+
+ D:/CS/Cpp/Assignments/cs-b-assignment1/build-Obenglobish-Desktop_Qt_5_5_0_MinGW_32bit-Release
+
+
+ true
+ qmake
+
+ QtProjectManager.QMakeBuildStep
+ false
+ true
+
+ false
+ false
+ false
+
+
+ true
+ Сборка
+
+ Qt4ProjectManager.MakeStep
+
+ false
+
+
+
+ 2
+ Сборка
+
+ ProjectExplorer.BuildSteps.Build
+
+
+
+ true
+ Сборка
+
+ Qt4ProjectManager.MakeStep
+
+ true
+ clean
+
+
+ 1
+ Очистка
+
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ Выпуск
+
+ Qt4ProjectManager.Qt4BuildConfiguration
+ 0
+ true
+
+ 2
+
+
+ 0
+ Установка
+
+ ProjectExplorer.BuildSteps.Deploy
+
+ 1
+ Локальная установка
+
+ ProjectExplorer.DefaultDeployConfiguration
+
+ 1
+
+
+
+ false
+ false
+ false
+ false
+ true
+ 0.01
+ 10
+ true
+ 1
+ 25
+
+ 1
+ true
+ false
+ true
+ valgrind
+
+ 0
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+ 10
+ 11
+ 12
+ 13
+ 14
+
+ 2
+
+ Obenglobish
+
+ Qt4ProjectManager.Qt4RunConfiguration:D:/CS/Cpp/Assignments/cs-b-assignment1/2-Obenglobish/Obenglobish.pro
+
+ Obenglobish.pro
+ false
+ false
+
+ 3768
+ false
+ true
+ false
+ false
+ true
+
+ 1
+
+
+
+ ProjectExplorer.Project.TargetCount
+ 1
+
+
+ ProjectExplorer.Project.Updater.FileVersion
+ 18
+
+
+ Version
+ 18
+
+
diff --git a/2-Obenglobish/src/Obenglobish.cpp b/2-Obenglobish/src/Obenglobish.cpp
index f9e31cb..ef4a436 100644
--- a/2-Obenglobish/src/Obenglobish.cpp
+++ b/2-Obenglobish/src/Obenglobish.cpp
@@ -5,7 +5,102 @@
#include "strlib.h"
using namespace std;
+
+/*
+ * Makes translation from english to obenglobish "language" by adding
+ * "ob" before each spoken vowel (if before vowel is not a vowel,
+ * exept 'e' at the end of the word excluding words with single syllable).
+ * Takes a string as a parameter to be translated and returns "translation" of this string.
+ */
+string obenglobish(string);
+
+/*
+ * Checks whether there is a vowel before the letter.
+ * @param string takes the line by reference where checks belonging vowel to one syllable
+ * by checking a previous letter to belong to vowel.
+ * @param int takes a position of a letter in the word which is checked.
+ */
+bool isOneVowel(const string &, int);
+
+/*
+ * Takes a character as a parameter and checks whether a character is a vowel.
+ */
+bool isVowel(char);
+
+
+/*
+ * Adds subword to the text.
+ * @ param string& the text passed by reference in which is inserted the subword
+ * @ param string the subword which is inserted.
+ * @param int the position where to insert the word
+ */
+void addSubwordToTheWord(string &, string, int);
+
+
+/*
+ * Reads an input line and outputs an entered data with its translation.
+ */
int main() {
- // TODO: fill in the code
- return 0;
+ while(true){
+ string text = getLine("Enter a text for translation: ");
+ if (text == "") {
+ break;
+ }
+ string translation = obenglobish(text);
+ cout << text << " -> " << translation << endl;
+ }
+ return 0;
}
+
+string obenglobish (string text){
+ text += " "; //add one space to the end of the string for simpler control of the last character
+ string subword = "ob"; //declare a string that to be inserted before vowels
+ int countOfSyllables = 0; //declare a variable for counts of syllables
+ int i = 0;
+ if(isVowel(text[i])){
+ addSubwordToTheWord(text, subword, i);
+ /* iterator moves to the right by the length of the inserted substring plus one for move
+ * to the next character after vowel*/
+ i += (subword.length() + 1);
+ ++countOfSyllables;
+ }
+ while(i < text.length() - 1){
+ if(isOneVowel(text, i)){
+ if((tolower(text[i]) == 'e') && (!isalpha(text[i+1])) && (countOfSyllables > 0)) {
+ ++i;
+ }
+ else {
+ addSubwordToTheWord(text, subword, i);
+ ++countOfSyllables;
+ i += (subword.length());//iterator moves to the right by the length of the inserted substring
+ }
+ }
+ if(text[i] == ' '){
+ countOfSyllables = 0; // resets the counter to zero when finds a spase (after the space starts a new word)
+ }
+ ++i;
+ }
+ return text.substr(0, text.length() - 1); // remove the extra space from the end of the line
+}
+
+
+bool isOneVowel(const string &text, int position){
+ return ((isVowel(tolower(text[position]))) && (!isVowel(tolower(text[position - 1]))));
+}
+
+bool isVowel(char letter){
+ string vowels = "aeiou";
+ for(char vowel : vowels){
+ if(letter == vowel){
+ return true;
+ }
+ }
+ return false;
+}
+
+
+void addSubwordToTheWord(string &text, string subword, int possition){
+ text = text.insert(possition, subword);
+}
+
+
diff --git a/3-NumericConversion/NumericConversion.pro b/3-NumericConversion/NumericConversion.pro
index 85bdbce..f5b48c0 100644
--- a/3-NumericConversion/NumericConversion.pro
+++ b/3-NumericConversion/NumericConversion.pro
@@ -110,7 +110,7 @@ win32 {
QMAKE_LFLAGS += -Wl,--stack,536870912
LIBS += -lDbghelp
LIBS += -lbfd
- LIBS += -liberty
+ # LIBS += -liberty
LIBS += -limagehlp
}
macx {
diff --git a/3-NumericConversion/src/NumericConversion.cpp b/3-NumericConversion/src/NumericConversion.cpp
index d66d654..9672103 100644
--- a/3-NumericConversion/src/NumericConversion.cpp
+++ b/3-NumericConversion/src/NumericConversion.cpp
@@ -1,14 +1,81 @@
#include
#include
#include "console.h"
+#include
using namespace std;
// Function prototypes
+/*
+ * Takes an integer as a parameter and returns a string identical
+ * to the passed number (takes into account the negative sign).
+ */
string intToString(int n);
+
+/*
+ * Takes a string, which may contain only a sign ('+' or '-') and
+ * digits of the number, as a parameter and returns an integer identical
+ * to the passed string (takes into account the negative or positive sign).
+ */
int stringToInt(string str);
+/*
+ * Asks the user to enter two numbers that are put into variables
+ * of type string. Converts strings to integers and displays the result
+ * of the mathematical addition. After that, converts the integers
+ * back to strings and displays the result of a string concatenation.
+ */
int main() {
- // TODO: fill in the code
+ string firstNumber = "";
+ string secondNumber = "";
+ cout << "Enter a first number: ";
+ cin >> firstNumber;
+
+ cout << "Enter a second number: ";
+ cin >> secondNumber;
+
+ int a = stringToInt(firstNumber);
+ int b = stringToInt(secondNumber);
+
+ cout << "Mathematical addition of two entered numbers: "
+ << firstNumber << " + " << secondNumber << " = "
+ << (a + b) << endl;
+
+ cout << "Adding the digits of the first number to the end of the second one: "
+ << firstNumber << " and " << secondNumber << " -> "
+ << (intToString(a) + intToString(b)) << endl;
+
return 0;
}
+
+int stringToInt(string digits){
+ if(digits.length() == 1){
+ return (digits[0] - '0');
+ }
+ if((digits[0] == '-') || (digits[0] == '+')){
+ char sign = digits[0]; // memorize the sign
+ digits = digits.substr(1); // and delete the sign
+ if(sign == '-'){
+ if (digits.length() == 1){ // conversion for negative single-digit numbers
+ return -1*(digits[0] - '0');
+ }
+ else return -1*((digits[0] - '0')*pow(10, (digits.length()-1)) + stringToInt(digits.substr(1)));
+ }
+ else return (digits[0] - '0'); //conversion for positive single-digit numbers
+ }
+ return (digits[0] - '0')*pow(10, (digits.length()-1)) + stringToInt(digits.substr(1));
+}
+
+
+string intToString(int number){
+ string sign = ""; // accept that there is no sign
+ if (number < 0){
+ sign = "-"; // if the number is negative then memorize the negative sign
+ number *= -1; // convert the number into a positive
+ }
+ if (number/10 == 0){
+ return (sign + char(number + '0'));
+ }
+ // if the passed number was negative then convert it back into the negative for pass recursively
+ return intToString((sign == "-" ? -1*number : number)/10) + char(number % 10 + '0');
+}
diff --git a/4-Flesch-Kincaid/FleschKincaid.pro b/4-Flesch-Kincaid/FleschKincaid.pro
index 85bdbce..f5b48c0 100644
--- a/4-Flesch-Kincaid/FleschKincaid.pro
+++ b/4-Flesch-Kincaid/FleschKincaid.pro
@@ -110,7 +110,7 @@ win32 {
QMAKE_LFLAGS += -Wl,--stack,536870912
LIBS += -lDbghelp
LIBS += -lbfd
- LIBS += -liberty
+ # LIBS += -liberty
LIBS += -limagehlp
}
macx {
diff --git a/4-Flesch-Kincaid/src/FleschKincaid.cpp b/4-Flesch-Kincaid/src/FleschKincaid.cpp
index 0b16ad2..bb2bcf5 100644
--- a/4-Flesch-Kincaid/src/FleschKincaid.cpp
+++ b/4-Flesch-Kincaid/src/FleschKincaid.cpp
@@ -1,8 +1,227 @@
#include
+#include
+#include
+#include
#include "console.h"
+#include "strlib.h"
+#include "tokenscanner.h"
using namespace std;
+
+/*
+ * Prompts the user for a filename and tries to open file. If an unsuccessful attempt
+ * to open a file again prompts the user for a filename. Otherwise returns a filename.
+ */
+string requestForFileName(ifstream &);
+
+/*
+ * Calculates the complexity of the reading text by the formula:
+ * C_0 + C_1*(numWords)/numSentences) + C_2*(numSyllables)/numWords)
+ * where C_0, C_1, C_2 are constants.
+ */
+double fleschKincaidGradeLevelTest(ifstream &);
+
+/*
+ * Checks whether an apostrophe is a part of the contraction of words.
+ * @param string takes a token and checks it.
+ * @param TokenScanner allows to read the following token.
+ * @params int takes a number of words by reference for counting.
+ */
+void checkForContractionOfWords(TokenScanner &, string &, int &);
+
+/*
+ * Makes calculations the approximate number of the syllables in the word.
+ * @param string takes the token in which which counted the syllables.
+ * @params int takes a number of syllables by reference for counting.
+ */
+void countingSyllables(string &, int &);
+
+/*
+ * Checks whether there is a vowel before the letter.
+ * @param string takes a token where cheks belonging vowel to one syllable.
+ * @param int takes a possition of the letter in the token which is cheked.
+ */
+bool isOneVowel(string &, int);
+
+/*
+ * Takes a character as a parameter and checks it for belonging to vowel.
+ */
+bool isVowel(char);
+
+
+/*
+ * Checks: is this the end of the sentence or not by checking
+ * whether a token is such punctuation mark which indicate the end of a sentence.
+ * @param string takes a token and checks it.
+ * @param TokenScanner allows to read the following token.
+ * @params int takes a number of sentences by reference for counting.
+ */
+void countingSentences(TokenScanner &, string &, int &);
+
+/*
+ * Checks whether a token is such punctuation mark which indicate the end of a sentence.
+ * @param string takes a token which is a punctuation mark and
+ * compares it with punctuation marks that indicate the end of a sentence.
+ */
+bool isTheEndOfSentenceChar(string &);
+
+
+/*
+ * Creats object for reading file. Prompts the user for a filename and opens it.
+ * Displays result of calculation the complexity of the text from open file.
+ */
int main() {
- // TODO: fill in the code
+ ifstream readFile;
+ requestForFileName(readFile);
+ cout << "Grade level of this text = " << setprecision(2)
+ << fleschKincaidGradeLevelTest(readFile) << endl;
return 0;
}
+
+
+string requestForFileName(ifstream & readFile){
+ while (true){
+ string fileName = "";
+ cout << "Please, enter file name: " << endl;
+ cin >> fileName;
+ readFile.open(fileName.c_str()); //leads a string to a c-style to open a file
+ if(readFile){
+ return fileName;
+ }
+ readFile.clear(); // resets the status indecator in the stream for reuse it.
+ cout << "The file can not be found. Check the correctness of the file name." << endl;
+ }
+}
+
+
+double fleschKincaidGradeLevelTest(ifstream & readFile){
+ double C_0 = -15.59, C_1 = 0.39, C_2 = 11.8; // constants for calculation the complexity of the text
+ int numWords = 0, numSentences = 0, numSyllables = 0;
+ TokenScanner scanner(readFile); //Initializes a scanner object to read from file by tokens.
+ while (scanner.hasMoreTokens()) {
+ scanner.ignoreWhitespace(); //skip whitespace characters.
+ string token = scanner.nextToken();
+ if(isalpha(token[0])){
+ countingSyllables(token, numSyllables);
+ ++numWords;
+ }
+ if(ispunct(token[0])){
+ checkForContractionOfWords(scanner, token, numWords);
+ countingSentences(scanner, token, numSentences);
+ }
+ }
+ readFile.close();
+
+ if (numWords < 1){
+ numWords = 1; // to avoid division by zero equate number of words to one
+ }
+ if (numSentences < 1){
+ numSentences = 1; // to avoid division by zero equate number of sentences to one
+ }
+
+ // displays the results counting of syllables, words and sentences
+ cout << "Number of syllables = " << numSyllables << endl;
+ cout << "Number of words = " < 1))
+ || ((toLowerCase(token) == "re") || (toLowerCase(token) == "ve"))){
+ --numSyllables;
+ }
+ }
+ return;
+}
+
+
+
+//Checks whether a vowel before another vowel
+bool isOneVowel(string & line, int possition){
+ return ((isVowel(tolower(line[possition]))) && (!isVowel(tolower(line[possition - 1]))));
+}
+
+
+bool isVowel(char letter){
+ string vowels = "aeiouy"; // a list of vowels and plus charecter 'y'
+ for (char vowel : vowels){
+ if (letter == vowel){
+ return true;
+ }
+ }
+ return false;
+}
+
+
+void checkForContractionOfWords(TokenScanner & scanner, string &token, int &numWords){
+ /*
+ * If the token is an apostrophe then check whether the next token (if it exists)
+ * is a part of a contraction of the words. To do this, read the next token and
+ * check it for compliance with the following conditions:
+ * - a token is a single letter and it is a consonant (to exclude the following contraction: "..'s", "..'t", "..'d" and etc);
+ * - a token is a such contraction of the words as "..'ve", "..'re" or "..'ll".
+ * if one of these conditions is true then reduce the number of words by one.
+ */
+ if((token == "'") && (scanner.hasMoreTokens())){
+ string nextToken = scanner.nextToken();
+ if(((nextToken.length() == 1) && (tolower(!isVowel(nextToken[0]))))
+ || (toLowerCase(nextToken) == "re")
+ || (toLowerCase(nextToken) == "ve")
+ || (toLowerCase(nextToken) == "ll")){
+ --numWords;
+ }
+ scanner.saveToken(nextToken); // read token put back
+ }
+}
+
+
+void countingSentences(TokenScanner &scanner, string &token, int &numSentences){
+ /*
+ * If the token is one of the punctuation marks which indicate the end of a sentence
+ * then read the next token (if it exists) and check it for the same.
+ * If the next token is not a punctuation mark which indicate the end of a sentence
+ * then increase the number of sentences by one. If passed token was the last one
+ * then also increase the number of sentences by one.
+ */
+ if(isTheEndOfSentenceChar(token)){
+ if(scanner.hasMoreTokens()){
+ string nextToken = scanner.nextToken();
+ if(!isTheEndOfSentenceChar(nextToken)){
+ scanner.saveToken(nextToken); // read token put back
+ ++numSentences;
+ }
+ }
+ else{
+ ++numSentences;
+ }
+ }
+}
+
+
+bool isTheEndOfSentenceChar(string & token){
+ string punctuationMarks = ".?!"; //punctuation at the end of the sentence
+ //return true if passed token was found in the list otherwise retutn false
+ return (punctuationMarks.find(token) != string::npos);
+}