Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 1-ConsecutiveHeads/ConsecutiveHeads.pro
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ win32 {
QMAKE_LFLAGS += -Wl,--stack,536870912
LIBS += -lDbghelp
LIBS += -lbfd
LIBS += -liberty
# LIBS += -liberty
LIBS += -limagehlp
}
macx {
Expand Down
22 changes: 20 additions & 2 deletions 1-ConsecutiveHeads/src/ConsecutiveHeads.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
#include <iostream>
#include <string>
#include <ctime>
#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;
}
2 changes: 1 addition & 1 deletion 2-Obenglobish/Obenglobish.pro
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ win32 {
QMAKE_LFLAGS += -Wl,--stack,536870912
LIBS += -lDbghelp
LIBS += -lbfd
LIBS += -liberty
# LIBS += -liberty
LIBS += -limagehlp
}
macx {
Expand Down
57 changes: 56 additions & 1 deletion 2-Obenglobish/src/Obenglobish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion 3-NumericConversion/NumericConversion.pro
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ win32 {
QMAKE_LFLAGS += -Wl,--stack,536870912
LIBS += -lDbghelp
LIBS += -lbfd
LIBS += -liberty
# LIBS += -liberty
LIBS += -limagehlp
}
macx {
Expand Down
91 changes: 86 additions & 5 deletions 3-NumericConversion/src/NumericConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion 4-Flesch-Kincaid/FleschKincaid.pro
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ win32 {
QMAKE_LFLAGS += -Wl,--stack,536870912
LIBS += -lDbghelp
LIBS += -lbfd
LIBS += -liberty
# LIBS += -liberty
LIBS += -limagehlp
}
macx {
Expand Down
122 changes: 121 additions & 1 deletion 4-Flesch-Kincaid/src/FleschKincaid.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,128 @@
#include <iostream>
#include <fstream>
#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;
}