diff --git a/1-ConsecutiveHeads/ConsecutiveHeads.pro b/1-ConsecutiveHeads/ConsecutiveHeads.pro index 85bdbce..35adfaf 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.3bfad9d b/1-ConsecutiveHeads/ConsecutiveHeads.pro.user.3bfad9d new file mode 100644 index 0000000..83634cd --- /dev/null +++ b/1-ConsecutiveHeads/ConsecutiveHeads.pro.user.3bfad9d @@ -0,0 +1,259 @@ + + + + + + EnvironmentId + {3bfad9dd-3b6b-48d6-8ec1-e670a3382d55} + + + 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:/googleDrive/cpp/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:/googleDrive/cpp/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:/googleDrive/cpp/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.7e432c6 b/1-ConsecutiveHeads/ConsecutiveHeads.pro.user.7e432c6 new file mode 100644 index 0000000..7c8ef91 --- /dev/null +++ b/1-ConsecutiveHeads/ConsecutiveHeads.pro.user.7e432c6 @@ -0,0 +1,259 @@ + + + + + + EnvironmentId + {7e432c67-97ba-4d1a-9c7a-f5d2d67dcde0} + + + 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:/googleDrive/cpp/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:/googleDrive/cpp/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:/googleDrive/cpp/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..9054c0a 100644 --- a/1-ConsecutiveHeads/src/ConsecutiveHeads.cpp +++ b/1-ConsecutiveHeads/src/ConsecutiveHeads.cpp @@ -1,10 +1,40 @@ +/*File ConsecutiveHeads.cpp + * ------------------------------------------------------ + * + * This program simulates a coin toss until heads fall three + * consecutive times. + */ + #include #include #include "console.h" #include "random.h" + using namespace std; + +/* Main program*/ int main() { - // TODO: fill in the code - return 0; + + const int NUM_OF_HEADS = 3; + + int counter = 0; + int counterFlips = 0; + + while (counter < NUM_OF_HEADS){ + if(randomBool()){ + counter++; + counterFlips++; + cout << "heads" << endl; + } + else { + counter = 0; + counterFlips++; + cout << "tails" << endl; + } + } + cout << endl << "It took " << counterFlips << " flips to get " << NUM_OF_HEADS << " consecutive heads."; + return 0; } + + diff --git a/2-Obenglobish/Obenglobish.pro b/2-Obenglobish/Obenglobish.pro index 85bdbce..35adfaf 100644 --- a/2-Obenglobish/Obenglobish.pro +++ b/2-Obenglobish/Obenglobish.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/2-Obenglobish/Obenglobish.pro.user.3bfad9d b/2-Obenglobish/Obenglobish.pro.user.3bfad9d new file mode 100644 index 0000000..cda5c50 --- /dev/null +++ b/2-Obenglobish/Obenglobish.pro.user.3bfad9d @@ -0,0 +1,259 @@ + + + + + + EnvironmentId + {3bfad9dd-3b6b-48d6-8ec1-e670a3382d55} + + + 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:/googleDrive/cpp/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:/googleDrive/cpp/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:/googleDrive/cpp/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/Obenglobish.pro.user.7e432c6 b/2-Obenglobish/Obenglobish.pro.user.7e432c6 new file mode 100644 index 0000000..4d0a30b --- /dev/null +++ b/2-Obenglobish/Obenglobish.pro.user.7e432c6 @@ -0,0 +1,259 @@ + + + + + + EnvironmentId + {7e432c67-97ba-4d1a-9c7a-f5d2d67dcde0} + + + 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:/googleDrive/cpp/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:/googleDrive/cpp/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:/googleDrive/cpp/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..9c97af0 100644 --- a/2-Obenglobish/src/Obenglobish.cpp +++ b/2-Obenglobish/src/Obenglobish.cpp @@ -1,3 +1,17 @@ + +/* File: Obenglobish.cpp + * --------------------------------------------------------- + * + * This program asks the user for word in English and translate + * it to Obenglobish dialect by folowing rule: + * + * Add text "od" before every vowel in the English word except + * vowels that follow other vowels and "e" character that occurs + * at the end of the word. + * + */ + + #include #include #include "console.h" @@ -5,7 +19,140 @@ #include "strlib.h" using namespace std; +/* Functions prototypes */ +string obenglobish(string word); +bool isVowel(char ch); +string filterString(string word); +void capitalizeWord(string &word); + +/* Main program*/ int main() { - // TODO: fill in the code + while (true) { + string word = getLine("Enter a word: "); + if (word != "") { + word = filterString(word); + cout << word << " -> " << obenglobish(word) << endl; + } + } return 0; } + +/* Function: obenglobish + * Usage: cout << word << " -> " << obenglobish(word) << endl; + * ----------------------------------------------------------- + * + * Translates received word to Obenglobish using specified rules. + * Check if the first letter if vowel and add's to result text "ob" + * and this letter. Than check other letters and if current letter is + * vowel and before this letter is not vowel add's text "ob" and this letter + * to the result. Otherwise add's only current letter. Also check last letter + * and if it "E" dont add's "ob" before it. + * + * @param word The word to translate into Obenglobish + * @return Translated word + */ + +string obenglobish(string word){ + + const string OB = "ob"; //text to add before vowels + + string result = ""; + + for(int i = 0; i < word.length(); i++){ + + if(i == 0 ){ //for the first letter in word + if(isVowel(word.at(i))){ + result += OB + word.at(i); + } + else { + result += word.at(i); + } + + } else if (i == word.length() - 1) { //for the last letter in word + if (isVowel(word.at(i)) && word.at(i) != 'E' && word.at(i) != 'e' ){ + result += OB + word.at(i); + } else { + result += word.at(i); + } + + } else { // for other letters + if (isVowel(word.at(i)) && !isVowel(word.at(i - 1))){ + result += OB + word.at(i); + } else { + result += word.at(i); + } + } + + + } + + capitalizeWord(result); + return result; +} + + +/* Function: filterString + * Usage: word = filterString(word); + * ----------------------------------------------------- + * + * Return received string without non alphabetic symbols + * + * @param word The text for filtering + * @return Filtered text +*/ +string filterString(string word){ + string result = ""; + + for (int i = 0; i < word.length(); i++ ){ + if(isalpha( word.at(i))){ + result += word.at(i); + } + + } + + return result; +} + +/* Function: isVowel + * Usage: if(isVowel(word.at(i)))... + * ---------------------------------------------------- + * + * Return TRUE if received charecter is vowel + * and FALSE if no. + * + * @param ch Character that will be checked + * @return Boolean result of the check + */ +bool isVowel(char ch){ + + switch (ch) { + case 'a': case 'e': case 'i': case 'o': case 'u': + case 'A': case 'E': case 'I': case 'O': case 'U': + return true; + + + default: + return false; + } +} + +/* Function: capitalizeWord + * Usage: capitalizeWord(result); + * --------------------------------------------------- + * + * Change text received in link transforming first letter to uppercase + * and other letters to lowercase + * + * @param word Link to string class object that will be capitalized + */ +void capitalizeWord(string &word){ + + for(int i = 0; i < word.length(); i++){ + if(i == 0){ + word.at(i) = toupper(word.at(i)); + } else { + word.at(i) = tolower(word.at(i)); + } + } + +} diff --git a/3-NumericConversion/NumericConversion.pro b/3-NumericConversion/NumericConversion.pro index 85bdbce..35adfaf 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/NumericConversion.pro.user.3bfad9d b/3-NumericConversion/NumericConversion.pro.user.3bfad9d new file mode 100644 index 0000000..6fd8e6f --- /dev/null +++ b/3-NumericConversion/NumericConversion.pro.user.3bfad9d @@ -0,0 +1,259 @@ + + + + + + EnvironmentId + {3bfad9dd-3b6b-48d6-8ec1-e670a3382d55} + + + 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:/googleDrive/cpp/cs-b-assignment1/build-NumericConversion-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:/googleDrive/cpp/cs-b-assignment1/build-NumericConversion-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 + + NumericConversion + + Qt4ProjectManager.Qt4RunConfiguration:D:/googleDrive/cpp/cs-b-assignment1/3-NumericConversion/NumericConversion.pro + + NumericConversion.pro + false + false + + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 18 + + + Version + 18 + + diff --git a/3-NumericConversion/src/NumericConversion.cpp b/3-NumericConversion/src/NumericConversion.cpp index d66d654..b608587 100644 --- a/3-NumericConversion/src/NumericConversion.cpp +++ b/3-NumericConversion/src/NumericConversion.cpp @@ -1,3 +1,17 @@ +/* File: NumericConverion.cpp + * ------------------------------------------------------------------------- + * + * This program is a recursive implementation of two functions: + * + * 1. intToString - Convert ingeger number to its string equivalent + * 2. stringToInt - Convert string number to it integer equivalent + * + * This program in an infinite loop asks the user for integer + * number, convert it to the string and print the result. Then convert + * the result back into the integer and print it. + * + * + */ #include #include #include "console.h" @@ -7,8 +21,86 @@ using namespace std; string intToString(int n); int stringToInt(string str); - +/* Main program*/ int main() { - // TODO: fill in the code - return 0; + + int intNumber = 0; + + while (true){ + cout << "Enter integer: "; + cin >> intNumber; + cout << "Number: " << intNumber << endl; + string strNumber = intToString(intNumber); + cout << "Integer like string: " << strNumber <0) { + result += intToString(n / 10) + (string() + char(n%10 + '0')); + } else { + result = "0"; + } + + /* Get rid of zero at the begining if positive number*/ + if (result.length() > 1 && result[0] == '0'){ + result = result.erase(0, 1); + } + + /* Get rid of zero at the begining if negtive number*/ + if(result[0] == '-' && result[1] == '0'){ + result = result.erase(1, 1); + } + return result; +} + +/* Function: stringToInt + * Usage: cout << "String like integer: " << stringToInt(strNumber) << endl; + * ------------------------------------------------------------------------ + * + * This function converts a string to its + * integer number equivalent using a recursive implementation. + * It cuts the last character of the string and converts it to the integer by + * removing from it '0', then calls itself without this character. + * + * @param str String number that will be converted to the integer number + * @return Converted number in the integer format + */ +int stringToInt(string str){ + int result = 0; + + /*Stop recursion at the last or alone character*/ + if (str.length() == 1){ + return str[0] - '0'; + } + + /*Removes '-' symbol and make result negative*/ + if (str[0] == '-'){ + return -stringToInt(str.erase(0,1)); + } + + result = (stringToInt(str.substr(0, str.length()-1)) * 10) + (str[str.length() - 1] - '0'); + + + return result; + } diff --git a/4-Flesch-Kincaid/FleschKincaid.pro b/4-Flesch-Kincaid/FleschKincaid.pro index 85bdbce..35adfaf 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/res/1.txt b/4-Flesch-Kincaid/res/1.txt new file mode 100644 index 0000000..bb136e3 --- /dev/null +++ b/4-Flesch-Kincaid/res/1.txt @@ -0,0 +1,3 @@ +Unity Unite Growth +Possibilities Nimble Me +Beautiful Manatee a 'friend' \ No newline at end of file diff --git a/4-Flesch-Kincaid/res/2.txt b/4-Flesch-Kincaid/res/2.txt new file mode 100644 index 0000000..4abc7d0 --- /dev/null +++ b/4-Flesch-Kincaid/res/2.txt @@ -0,0 +1 @@ +grille diff --git a/4-Flesch-Kincaid/res/3.txt b/4-Flesch-Kincaid/res/3.txt new file mode 100644 index 0000000..03216ce --- /dev/null +++ b/4-Flesch-Kincaid/res/3.txt @@ -0,0 +1,7 @@ +In the low-ceilinged canteen, deep underground, the lunch queue jerked slowly forward. The +room was already very full and deafeningly noisy. From the grille at the counter the steam +of stew came pouring forth, with a sour metallic smell which did not quite overcome the +fumes of Victory Gin. On the far side of the room there was a small bar, a mere hole in the +wall, where gin could be bought at ten cents the large nip. +'Just the man I was looking for,' said a voice at Winston's back. +He turned \ No newline at end of file diff --git a/4-Flesch-Kincaid/src/FleschKincaid.cpp b/4-Flesch-Kincaid/src/FleschKincaid.cpp index 0b16ad2..d02647f 100644 --- a/4-Flesch-Kincaid/src/FleschKincaid.cpp +++ b/4-Flesch-Kincaid/src/FleschKincaid.cpp @@ -1,8 +1,176 @@ #include +#include +#include +#include "simpio.h" +#include "strlib.h" #include "console.h" +#include "tokenscanner.h" using namespace std; +void enterFileName(ifstream & infile); +void fleschKincaidTest(ifstream & infile); +int syllablesIn(string word); +bool isVowel(char ch); +double calculateGradeLevel(int words, int syllables, int sentences); + +/* Main program */ int main() { - // TODO: fill in the code + ifstream infile; + enterFileName(infile); + fleschKincaidTest(infile); + infile.close(); return 0; } + +/* Function: enterFileName + * Usage: enterFileName(infile); + * -------------------------------------------------------------- + * + * This function asks user for filename, try to open file with this + * filename and if it wrong asks user again. Function is interrupted + * if file opened successfully. + * + * @param &infile Link to the input file stream + */ +void enterFileName(ifstream & infile){ + + while (true) { + string fileName = getLine("Enter input file name: "); + infile.open(fileName.c_str()); + if(!infile.fail()) break; + infile.clear(); + cout << "Unable to open that file. Try again." << endl; + } +} + +/* Function: fleschKincaidTest + * Usage: fleschKincaidTest(infile); + * + * This function evaluates the text from a file on the complexity + * of its reading using a Flesch Kincaid grade level test. + * At first it splits the text into words and punctuation using TokenScanner class. + * Then calculates number of the words, syllables and sentences in the text + * and calculates grade level using a special formula. + * + * @param &infile Link to the input file stream with opened file + */ +void fleschKincaidTest(ifstream &infile){ + TokenScanner scanner(infile); + string token = ""; + int wordCounter = 0; + int syllableCounter = 0; + int sentenceCounter = 0; + double grade = 0.0; + + scanner.ignoreWhitespace(); + scanner.addWordCharacters("'"); + + while (scanner.hasMoreTokens()) { + token = scanner.nextToken(); + + /*consider that it a word if the first character is letter or digit*/ + if (isalpha(token[0])){ + wordCounter++; + syllableCounter += syllablesIn(token); + //cout << token << " syllables: " << syllablesIn(token) << endl; + } else if (token[0] == '.' || token[0] == '!' || token[0] == '?'){ + sentenceCounter++; //count end of sentence symbols + } + } + + if (sentenceCounter == 0) sentenceCounter = 1; + + grade = calculateGradeLevel(wordCounter, syllableCounter, sentenceCounter); + + cout << "Words: " << wordCounter << endl; + cout << "Syllables: " << syllableCounter << endl; + cout << "Sentences: " << sentenceCounter << endl; + cout << "Grade level: " << grade << endl; + +} + +/* Function: syllablesIn + * Usage: syllableCounter += syllablesIn(token); + * ----------------------------------------------------------------------------- + * + * This function calsulates the number of syllables in the word. It count + * number of vowels in the word except vowels before that are other vowels + * end 'e' at the end of the word. + * + * @param word The word to count number syllables + * return Number of syllables in word + */ +int syllablesIn(string word){ + int counter = 0; + word = toLowerCase(word); + + for (int i = 0; i < word.length(); i++){ + if (isVowel(word.at(i))) { + + if (i == 0){ // if first symbol is vowel + counter++; + + /*If symbol beetwen first and last is wowel and before it not vowel*/ + } else if(i > 0 && i < word.length() - 1 && !isVowel(word.at(i-1))){ + counter++; + + /* If last symbol is vowel but not 'e'*/ + } else if (word.length() > 1 && i == word.length() - 1 && word.at(i) != 'e' && !isVowel(word.at(i-1))){ + counter++; + } + } + } + + /*any word have minimum one syllable*/ + if (counter == 0){ + return 1; + } + return counter; +} + +/* Function: isVowel + * Usage: if(isVowel(word.at(i)))... + * ---------------------------------------------------- + * + * Return TRUE if received charecter is vowel + * and FALSE if no. + * + * @param ch Character that will be checked + * @return Boolean result of the check + */ +bool isVowel(char ch){ + + switch (ch) { + case 'a': case 'e': case 'i': case 'o': case 'u': case 'y': + return true; + + + default: + return false; + } +} + +/* Function: calculateGradeLevel + * Usage: grade = calculateGradeLevel(wordCounter, syllableCounter, sentenceCounter); + * --------------------------------------------------------------------------------- + * + * This function calculates the grade level of the text using number of the words, + * number of the syllables, number of sentences and some strange + * constants in the special formula. + * + * @param word Number of words it text + * @param syllables Number of syllables in text + * @param sentences Number of sentences + * @return Grade level of the text + */ +double calculateGradeLevel(int words, int syllables, int sentences){ + /* Some constants for test*/ + const double c0 = -15.59; + const double c1 = 0.39; + const double c2 = 11.8; + + double result = 0.0; + + result = c0 + (c1 * (double(words) / sentences)) + double(c2 * (double(syllables) / words)); + return result; +}