From 3495610e6f9228cbc20e0f936b86413f59ea5076 Mon Sep 17 00:00:00 2001 From: shpp-skobzin Date: Thu, 27 Aug 2015 15:46:21 +0300 Subject: [PATCH 1/2] Task 1-3 is ok. --- 1-ConsecutiveHeads/ConsecutiveHeads.pro | 2 +- 1-ConsecutiveHeads/src/ConsecutiveHeads.cpp | 22 ++++- 2-Obenglobish/Obenglobish.pro | 2 +- 2-Obenglobish/src/Obenglobish.cpp | 57 +++++++++++- 3-NumericConversion/NumericConversion.pro | 2 +- 3-NumericConversion/src/NumericConversion.cpp | 91 ++++++++++++++++++- 6 files changed, 165 insertions(+), 11 deletions(-) diff --git a/1-ConsecutiveHeads/ConsecutiveHeads.pro b/1-ConsecutiveHeads/ConsecutiveHeads.pro index 85bdbce..4570e8d 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/src/ConsecutiveHeads.cpp b/1-ConsecutiveHeads/src/ConsecutiveHeads.cpp index 4b7f6b3..b773e96 100644 --- a/1-ConsecutiveHeads/src/ConsecutiveHeads.cpp +++ b/1-ConsecutiveHeads/src/ConsecutiveHeads.cpp @@ -1,10 +1,28 @@ #include #include +#include #include "console.h" #include "random.h" using namespace std; int main() { - // TODO: fill in the code - return 0; + int flipsCounter = 0; + int headsCounter = 0; + // Generate new random generator seed. + srand(time(0)); + while (headsCounter < 3) { + if(rand() % 2) { + // Is heads... + headsCounter ++; + cout << "heads" << endl; + } else { + // Is tails... + headsCounter = 0; + cout << "tails" << endl; + } + flipsCounter ++; + } + // Output resultson the console. + cout << "It tooks " << flipsCounter << " to get 3 consecutive heads." << endl; + return 0; } diff --git a/2-Obenglobish/Obenglobish.pro b/2-Obenglobish/Obenglobish.pro index 85bdbce..4570e8d 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/src/Obenglobish.cpp b/2-Obenglobish/src/Obenglobish.cpp index f9e31cb..d6c2028 100644 --- a/2-Obenglobish/src/Obenglobish.cpp +++ b/2-Obenglobish/src/Obenglobish.cpp @@ -5,7 +5,62 @@ #include "strlib.h" using namespace std; +/** + * Define the letter is the vowel. + * + * @param letter - Current letter. + * @return true if the letter is vowel or return false in other case. + */ +bool isVowel(char letter) { + const char vowelsChars[5] = {'a', 'e', 'i', 'o', 'u'}; + for (int i = 0; i < 5; i ++) + if (letter == vowelsChars[i]) + return true; + return false; +} +/** + * Define can add "ob" in the current position of the word. + * + * @param word - The word. + * @param i - The current position. + * @return - true if can add "ob" in the current position or return false in other case. + */ +bool canAddOb(string word, int i) { + // Keep out consonants letters. + if (!isVowel(word[i])) + return false; + // Keep out last "e" letter. + if ((i == word.length() - 1) && (word[i] == 'e')) + return false; + // Keep out double vowel letter. + if ((i != 0) && (isVowel(word[i - 1]))) + return false; + // In this case can add "ob" in the current position of the word. + return true; +} + +/** + * Translate english to the obenglobish words. + * + * @param word - The english word. + * @return - The oenglobish word. + */ +string obenglobish(string word) { + for (int i = 0; i < word.length(); i ++) + if (canAddOb(word, i)) { + word.insert(i, "ob"); + i += 2; + } + return word; +} + int main() { - // TODO: fill in the code + while (true) { + string word = getLine("Enter a word: "); + if (word == "") + break; + string translation = obenglobish(toLowerCase(word)); + cout << word << " -> " << translation << endl; + } return 0; } diff --git a/3-NumericConversion/NumericConversion.pro b/3-NumericConversion/NumericConversion.pro index 85bdbce..4570e8d 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..7e891c5 100644 --- a/3-NumericConversion/src/NumericConversion.cpp +++ b/3-NumericConversion/src/NumericConversion.cpp @@ -3,12 +3,93 @@ #include "console.h" using namespace std; -// Function prototypes -string intToString(int n); -int stringToInt(string str); +/** + * Define the sign of the int number and return it in the string format. + * + * @param n - Int format number. + * @return - String format of the numbers sign. + */ +string defineSign(int &n){ + if (n < 0) { + n *= -1; + return "-"; + } else + return ""; +} + +/** + * Define the sign of the string number and return it in the int format. + * + * @param str - String format number. + * @return - Int format of the numbers sign. + */ +int defineSign(string &str){ + if (str[0] == '-') { + str.erase(0, 1); + return -1; + } else + return 1; +} +/** + * Logic of the recursion convertion numbers int to string format. + * + * @param n - Int format number. + * @return - String format number. + */ +string doRecursionIntToString(int n){ + if (n < 10) + return string() + char(n + '0'); + else + return doRecursionIntToString(n / 10) + (string() + char(n % 10 + '0')); +} + +/** + * Convert numbers int to string format with sign. + * + * @param n - Int format number. + * @return - String format number. + */ +string intToString(int n) { + string sign = defineSign(n); + return sign + doRecursionIntToString(n); +} + +/** + * Logic of the recursion convertion numbers string to int format. + * + * @param str - String format number. + * @return - Int format number. + */ +int doRecursionStringToInt(string str) { + if (str.length() == 1) + return int(str[0] - '0'); + else + return doRecursionStringToInt(str.substr(0, str.length() - 1)) * 10 + + int(str[str.length() - 1] - '0'); +} + +/** + * Convert numbers string to int format with sign. + * + * @param str - String format number. + * @return - Int format number. + */ +int stringToInt(string str) { + int sign = defineSign(str); + return sign * doRecursionStringToInt(str); +} int main() { - // TODO: fill in the code - return 0; + cout << "Testing stringToInt function:" << endl; + cout << "String = \"0\" -> int = " << stringToInt("0") << endl; + cout << "String = \"123\" -> int = " << stringToInt("123") << endl; + cout << "String = \"-456\" -> int = " << stringToInt("-456") << endl; + cout << endl; + cout << "Testing intToString function:" << endl; + cout << "Int = 0 -> string = \"" << intToString(0) << "\"" << endl; + cout << "Int = 123 -> string = \"" << intToString(123) << "\"" << endl; + cout << "Int = -456 -> string = \"" << intToString(-456) << "\"" << endl; + + return 0; } From d24701a53bffa4d4d2f5e399158e486c4d0fd356 Mon Sep 17 00:00:00 2001 From: shpp-skobzin Date: Sun, 30 Aug 2015 22:22:00 +0300 Subject: [PATCH 2/2] All tasks is done. --- 4-Flesch-Kincaid/FleschKincaid.pro | 2 +- 4-Flesch-Kincaid/src/FleschKincaid.cpp | 122 ++++++++++++++++++++++++- 2 files changed, 122 insertions(+), 2 deletions(-) diff --git a/4-Flesch-Kincaid/FleschKincaid.pro b/4-Flesch-Kincaid/FleschKincaid.pro index 85bdbce..4570e8d 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..f8288f3 100644 --- a/4-Flesch-Kincaid/src/FleschKincaid.cpp +++ b/4-Flesch-Kincaid/src/FleschKincaid.cpp @@ -1,8 +1,128 @@ #include +#include +#include "strlib.h" #include "console.h" +#include "tokenscanner.h" using namespace std; +/** + * Request file name and open file if name is valid. + * + * @param inputFile - File for open. + */ +void openFile(ifstream &inputFile) { + string fileName; + while (true) { + cout << "Enter a file name: "; + cin >> fileName; + inputFile.open(fileName); + if (inputFile.is_open()) + break; + cout << "File name is incorrect." << endl; + } + cout << "File is opened." << endl; +} + +/** + * Define the letter is the vowel. + * + * @param letter - Current letter. + * @return true if the letter is vowel or return false in other case. + */ +bool isVowel(char letter) { + return ((letter == 'a') || (letter == 'e') || (letter == 'i') || + (letter == 'o') || (letter == 'u') || (letter == 'y')); +} + +/** + * Define the token is the word. + * + * @param token - Token for check. + * @return - 1 if the token is the word or return 0 in other case. + */ +int addWords(string token) { + return ((isalpha(token[0])) || ((token[0] == '\'') && (token.length() != 1))) ? 1 : 0; +} + +/** + * Define the token is the end of the sentence punkt. + * + * @param token - Token to check. + * @return - 1 if the token is the end of the sentence punkt or return 0 in other case + */ +int addSentences(string token) { + return ((token[0] == '.') || (token[0] == '?') || (token[0] == '!')) ? 1 : 0; +} + +/** + * Define count of the syllables in the token. + * + * @param token - Token to check. + * @return - Count of the syllables in the token. + */ +int addSyllables(string token) { + int numSyllables = 0; + token = toLowerCase(token); + for (int i = 0; i < token.length(); i ++) { + // Keep out consonants letters. + if (!isVowel(token[i])) + continue; + // Keep out last "e" letter. + if ((i == token.length() - 1) && (token[i] == 'e')) + continue; + // Keep out double vowel letter. + if ((i != 0) && (isVowel(token[i - 1]))) + continue; + numSyllables ++; + } + // Word must contain at least one syllable. + return ((numSyllables == 0) && (token.length() != 1)) ? 1 : numSyllables; +} + +/** + * Read file by tokens and calculate grade of the text. + * + * @param inputFile - File for read. + * @return - Calculated grade of the text. + */ +double getGrade(ifstream &inputFile) { + const double c0 = -15.59, c1 = 0.39, c2 = 11.8; + int numWords = 0, numSentences = 0, numSyllables = 0; + TokenScanner scanner(inputFile); + // Ignore white space tokens. + scanner.ignoreWhitespace(); + // Set the "'" symbol like the part of the word. + scanner.addWordCharacters("'"); + cout << "Do file processing..." << endl; + // Read file by tokens. + while (scanner.hasMoreTokens()) { + string token = scanner.nextToken(); + numWords += addWords(token); + numSentences += addSentences(token); + numSyllables += addSyllables(token); + // For show debugging information uncomment folow line. + //cout << token << " w: " << addWords(token) << " sen: " << addSentences(token) << " syl: " << addSyllables(token) << endl; + } + cout << "Found " << numWords << " words, " << numSentences << " sentences and " << numSyllables << + " syllables." << endl; + return c0 + c1 * numWords / numSentences + c2 * numSyllables / numWords; +} + +/** + * Close file. + * + * @param inputFile - File for close. + */ +void closeFile(ifstream &inputFile) { + inputFile.close(); + cout << "File is closed." << endl; +} + int main() { - // TODO: fill in the code + ifstream inputFile; + openFile(inputFile); + double grade = getGrade(inputFile); + closeFile(inputFile); + cout << "Calculated grade of this text is " << grade << "." << endl; return 0; }