From e3b99c764ef6b63e0fb6923830fa205607e2e556 Mon Sep 17 00:00:00 2001 From: alyabihova Date: Wed, 26 Aug 2015 16:01:10 +0300 Subject: [PATCH 1/3] [ready but 4th task iet without using TokenScanner] --- 1-ConsecutiveHeads/ConsecutiveHeads.pro | 2 +- .../ConsecutiveHeads.pro.user.8ad8611 | 259 ++++++++++++++++++ 1-ConsecutiveHeads/src/ConsecutiveHeads.cpp | 24 +- 2-Obenglobish/Obenglobish.pro | 2 +- 2-Obenglobish/Obenglobish.pro.user.e083ff4 | 259 ++++++++++++++++++ 2-Obenglobish/src/Obenglobish.cpp | 99 ++++++- 3-NumericConversion/NumericConversion.pro | 2 +- 3-NumericConversion/src/NumericConversion.cpp | 69 ++++- 4-Flesch-Kincaid/FleschKincaid.pro | 2 +- 4-Flesch-Kincaid/src/FleschKincaid.cpp | 162 ++++++++++- 10 files changed, 870 insertions(+), 10 deletions(-) create mode 100644 1-ConsecutiveHeads/ConsecutiveHeads.pro.user.8ad8611 create mode 100644 2-Obenglobish/Obenglobish.pro.user.e083ff4 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/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..853381c 100644 --- a/4-Flesch-Kincaid/src/FleschKincaid.cpp +++ b/4-Flesch-Kincaid/src/FleschKincaid.cpp @@ -1,8 +1,168 @@ #include +#include +#include #include "console.h" +#include "strlib.h" using namespace std; + +/* + * Prompts the user for a filename and tries to open file. In 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 &); + +/* + * Makes calculations of number of syllables, words and sentences in the passed string. + * @param string takes the line where makes calculations. + * @params int take a number of words, sentences and syllables in this sequence. + */ +void processingLine(string &, int &, int &, int &); + +/* + * Removes extra spaces between words in passed line. + */ +void removeExtraSpaces(string &); + +/* + * Checks whether there is a vowel before the letter. + * @param string takes the line where cheks belonging vowel to one syllable. + * @param int takes a possition of the letter in the line 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. + * @param string takes a line where makes checking passed charecter and next charecter. + * @param int takes a possition of a checking character. + */ +bool isSentences(string &, int); + + +/* + * 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; + string line = ""; + while (getline(readFile, line)) { + if(line != ""){ // checks for empty string + processingLine(line, numWords, numSentences, numSyllables); + } + } + 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 + } + return C_0 + C_1*(((double)numWords)/numSentences) + C_2*(((double)numSyllables)/numWords); +} + + +void processingLine(string &line, int &numWords, int &numSentences, int &numSyllables){ + int numSyllablesInWord = 0; //for counting number of syllables in one word + removeExtraSpaces(line); + line = " " + line; // add one space to the begining of the line for easier counting (to be within the string) + for(int i = 1; i < line.length() - 1; ++i){ // loop iterates from the second to the penultimate character, because at the beginning and the end are the spaces + if(isspace(line[i])){ + ++numWords; + numSyllablesInWord = 0; + } + if(isSentences(line, i)){ + ++numSentences; + } + if(isOneVowel(line, i)){ + if((tolower(line[i]) == 'e') && (!isalpha(line[i+1])) && (numSyllablesInWord > 0)){ + continue; + } + else { + ++numSyllables; + ++numSyllablesInWord; + } + } + } + ++numWords; // accept that the end of the line is the end of a word too and increase the number of words. +} + + +void removeExtraSpaces(string &line){ + line = trim(line) + " "; // add one space to the end of the line for easier checking (to be within the string) + for(int i = 0; i < line.length() - 1; ++i){ + if(isspace(line[i]) && (isspace(line[i+1]))){ + line.erase(i, 1); //if there are two or more consecutive spaces that remove unwanted one + --i; // return the iterator by one ago + } + } +} + +//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"; + for (char vowel : vowels){ + if (letter == vowel){ + return true; + } + } + return false; +} + + +bool isSentences(string & line, int possition){ + string punctuationMarks = ".?!"; //punctuation at the end of the sentence + for (char mark : punctuationMarks){ + /* + * checks whether a character is on a passed position of punctuation + * from a predefined list and checks the next character whether it is a space + * or quotes. Thus determine the approximate number of sentences. + */ + if (((line[possition] == mark) && (isspace(line[possition + 1]))) + || ((line[possition] == mark) && (line[possition + 1] == '"'))){ + return true; + } + } + return false; +} From 0f0db1223af65f86e1d796d1a34917cf4047ce25 Mon Sep 17 00:00:00 2001 From: alyabihova Date: Wed, 26 Aug 2015 23:45:10 +0300 Subject: [PATCH 2/3] [ready but 4th task jet without using TokenScanner] --- .../ConsecutiveHeads.pro.user.e083ff4 | 259 ++++++++++++++++++ 4-Flesch-Kincaid/src/FleschKincaid.cpp | 17 ++ 2 files changed, 276 insertions(+) create mode 100644 1-ConsecutiveHeads/ConsecutiveHeads.pro.user.e083ff4 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/4-Flesch-Kincaid/src/FleschKincaid.cpp b/4-Flesch-Kincaid/src/FleschKincaid.cpp index 853381c..f6b757d 100644 --- a/4-Flesch-Kincaid/src/FleschKincaid.cpp +++ b/4-Flesch-Kincaid/src/FleschKincaid.cpp @@ -31,6 +31,14 @@ void processingLine(string &, int &, int &, int &); */ void removeExtraSpaces(string &); +/* + * Checks: is this a single-character punctuation. + * @param string takes a line where makes checking passed charecter + * and also next and previous charecters. + * @param int takes a possition of a checking character. + */ +bool isASingleCharacterPunctuation(string &, int); + /* * Checks whether there is a vowel before the letter. * @param string takes the line where cheks belonging vowel to one syllable. @@ -94,6 +102,7 @@ double fleschKincaidGradeLevelTest(ifstream & readFile){ if (numSentences < 1){ numSentences = 1; // to avoid division by zero equate number of sentences to one } + cout << numSyllables << " -> " << numWords << " -> " << numSentences << endl; return C_0 + C_1*(((double)numWords)/numSentences) + C_2*(((double)numSyllables)/numWords); } @@ -107,6 +116,9 @@ void processingLine(string &line, int &numWords, int &numSentences, int &numSyll ++numWords; numSyllablesInWord = 0; } + if (isASingleCharacterPunctuation(line, i)){ + --numWords; + } if(isSentences(line, i)){ ++numSentences; } @@ -151,6 +163,11 @@ bool isVowel(char letter){ } +bool isASingleCharacterPunctuation(string &line, int possition){ + return ((ispunct(line[possition])) && (isspace(line[possition - 1])) && (isspace(line[possition + 1]))); +} + + bool isSentences(string & line, int possition){ string punctuationMarks = ".?!"; //punctuation at the end of the sentence for (char mark : punctuationMarks){ From 204906f0defa47d247215418957dc5f799106cc8 Mon Sep 17 00:00:00 2001 From: alyabihova Date: Fri, 28 Aug 2015 17:17:43 +0300 Subject: [PATCH 3/3] [4th task done with TokenScanner] --- 4-Flesch-Kincaid/src/FleschKincaid.cpp | 182 +++++++++++++++---------- 1 file changed, 112 insertions(+), 70 deletions(-) diff --git a/4-Flesch-Kincaid/src/FleschKincaid.cpp b/4-Flesch-Kincaid/src/FleschKincaid.cpp index f6b757d..bb2bcf5 100644 --- a/4-Flesch-Kincaid/src/FleschKincaid.cpp +++ b/4-Flesch-Kincaid/src/FleschKincaid.cpp @@ -1,13 +1,15 @@ #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. In an unsuccessful attempt + * 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 &); @@ -20,29 +22,24 @@ string requestForFileName(ifstream &); double fleschKincaidGradeLevelTest(ifstream &); /* - * Makes calculations of number of syllables, words and sentences in the passed string. - * @param string takes the line where makes calculations. - * @params int take a number of words, sentences and syllables in this sequence. + * 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 processingLine(string &, int &, int &, int &); +void checkForContractionOfWords(TokenScanner &, string &, int &); /* - * Removes extra spaces between words in passed line. + * 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 removeExtraSpaces(string &); - -/* - * Checks: is this a single-character punctuation. - * @param string takes a line where makes checking passed charecter - * and also next and previous charecters. - * @param int takes a possition of a checking character. - */ -bool isASingleCharacterPunctuation(string &, int); +void countingSyllables(string &, int &); /* * Checks whether there is a vowel before the letter. - * @param string takes the line where cheks belonging vowel to one syllable. - * @param int takes a possition of the letter in the line which is cheked. + * @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); @@ -51,12 +48,22 @@ bool isOneVowel(string &, int); */ 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: is this the end of the sentence or not. - * @param string takes a line where makes checking passed charecter and next charecter. - * @param int takes a possition of a checking character. + * 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 isSentences(string &, int); +bool isTheEndOfSentenceChar(string &); /* @@ -86,65 +93,70 @@ string requestForFileName(ifstream & readFile){ } } + 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; - string line = ""; - while (getline(readFile, line)) { - if(line != ""){ // checks for empty string - processingLine(line, numWords, numSentences, numSyllables); + 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 } - cout << numSyllables << " -> " << numWords << " -> " << numSentences << endl; + + // displays the results counting of syllables, words and sentences + cout << "Number of syllables = " << numSyllables << endl; + cout << "Number of words = " < 0)){ - continue; - } - else { + if(isVowel(tolower(token[0]))){ // checks whether the first letter is a vowel + ++numSyllables; + ++numSyllablesInWord; + } + if(token.length() > 1){ // if length of the token more than one then check the rest of the charecters + for(int i = 1; i < token.length(); ++i){ + if(isOneVowel(token, i)){ ++numSyllables; ++numSyllablesInWord; } } + /* + * Reduced the total number of syllables by one in next cases: + * - if the last character is a letter 'e' and the previous character is a consonant + * moreover the number of syllables in this token more than one; + * - if a token is a part of a contraction of the words, such as "..'ve" or "..'re" + */ + if(((tolower(token[token.length() - 1]) == 'e') && (!isVowel(token[token.length() - 2])) && (numSyllablesInWord > 1)) + || ((toLowerCase(token) == "re") || (toLowerCase(token) == "ve"))){ + --numSyllables; + } } - ++numWords; // accept that the end of the line is the end of a word too and increase the number of words. + return; } -void removeExtraSpaces(string &line){ - line = trim(line) + " "; // add one space to the end of the line for easier checking (to be within the string) - for(int i = 0; i < line.length() - 1; ++i){ - if(isspace(line[i]) && (isspace(line[i+1]))){ - line.erase(i, 1); //if there are two or more consecutive spaces that remove unwanted one - --i; // return the iterator by one ago - } - } -} //Checks whether a vowel before another vowel bool isOneVowel(string & line, int possition){ @@ -153,7 +165,7 @@ bool isOneVowel(string & line, int possition){ bool isVowel(char letter){ - string vowels = "aeiouy"; + string vowels = "aeiouy"; // a list of vowels and plus charecter 'y' for (char vowel : vowels){ if (letter == vowel){ return true; @@ -163,23 +175,53 @@ bool isVowel(char letter){ } -bool isASingleCharacterPunctuation(string &line, int possition){ - return ((ispunct(line[possition])) && (isspace(line[possition - 1])) && (isspace(line[possition + 1]))); +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 + } } -bool isSentences(string & line, int possition){ - string punctuationMarks = ".?!"; //punctuation at the end of the sentence - for (char mark : punctuationMarks){ - /* - * checks whether a character is on a passed position of punctuation - * from a predefined list and checks the next character whether it is a space - * or quotes. Thus determine the approximate number of sentences. - */ - if (((line[possition] == mark) && (isspace(line[possition + 1]))) - || ((line[possition] == mark) && (line[possition + 1] == '"'))){ - return true; +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; } } - return false; +} + + +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); }