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
6 changes: 5 additions & 1 deletion 1-ConsecutiveHeads/ConsecutiveHeads.pro
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,20 @@ QMAKE_CXXFLAGS_WARN_ON += -Wno-sign-compare
QMAKE_CXXFLAGS_WARN_ON += -Wno-unused-parameter
QMAKE_CXXFLAGS_WARN_ON += -Wno-write-strings


unix:!macx {
QMAKE_CXXFLAGS += -rdynamic
QMAKE_LFLAGS += -rdynamic
QMAKE_LFLAGS += -Wl,--export-dynamic
QMAKE_CXXFLAGS += -Wl,--export-dynamic

LIBS+=-L/usr/local/lib
LIBS+=-lexecinfo
}
!win32 {
QMAKE_CXXFLAGS += -Wno-dangling-field
QMAKE_CXXFLAGS += -Wno-unused-const-variable
LIBS += -ldl
# LIBS += -ldl
}

# increase system stack size (helpful for recursive programs)
Expand Down
17 changes: 14 additions & 3 deletions 1-ConsecutiveHeads/src/ConsecutiveHeads.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
#include <iostream>
#include <string>
#include "console.h"
#include "random.h"
using namespace std;

int consecutiveHeads(int nheads);
const int NHEADS = 3;

int main() {
// TODO: fill in the code
return 0;
int result = consecutiveHeads(NHEADS);
std::cout << "It took " << result << " flips to get " << NHEADS << " consecutive heads." << endl;
return 0;
}

int consecutiveHeads(int nheads) {
if (nheads == 0) return 0;
int i = randomInteger(0, 1);
std::cout << ((i == 1) ? "heads" : "tails") << endl;
if (i == 1) return 1 + consecutiveHeads(nheads-1); // decrease required heads
else return 1 + consecutiveHeads(NHEADS);
}
5 changes: 4 additions & 1 deletion 2-Obenglobish/Obenglobish.pro
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ unix:!macx {
QMAKE_LFLAGS += -rdynamic
QMAKE_LFLAGS += -Wl,--export-dynamic
QMAKE_CXXFLAGS += -Wl,--export-dynamic

LIBS+=-L/usr/local/lib
LIBS+=-lexecinfo
}
!win32 {
QMAKE_CXXFLAGS += -Wno-dangling-field
QMAKE_CXXFLAGS += -Wno-unused-const-variable
LIBS += -ldl
# LIBS += -ldl
}

# increase system stack size (helpful for recursive programs)
Expand Down
22 changes: 20 additions & 2 deletions 2-Obenglobish/src/Obenglobish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@
#include <string>
#include "console.h"
#include "simpio.h"
#include "strlib.h"
//#include "strlib.h"
using namespace std;

string obenglobish(string word);

int main() {
// TODO: fill in the code
while (true) {
string word = getLine("Enter a word: ");
if (word == "") continue;
string translation = obenglobish(word);
cout << word << " -> " << translation << endl;
}
return 0;
}

string obenglobish(string word) {
string vowels = "aeiou";
if (word == "e" || word == "") return word; //skip silence 'e'
if (vowels.find(word[0]) != string::npos) { //if char is vowel
if (vowels.find(word[1]) != string::npos) {
// skip next vowel
return string("ob") + word[0] + word[1] + obenglobish(word.substr(2));
} else return string("ob") + word[0] + obenglobish(word.substr(1));
} else return word[0] + obenglobish(word.substr(1));
}
5 changes: 4 additions & 1 deletion 3-NumericConversion/NumericConversion.pro
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ unix:!macx {
QMAKE_LFLAGS += -rdynamic
QMAKE_LFLAGS += -Wl,--export-dynamic
QMAKE_CXXFLAGS += -Wl,--export-dynamic

LIBS+=-L/usr/local/lib
LIBS+=-lexecinfo
}
!win32 {
QMAKE_CXXFLAGS += -Wno-dangling-field
QMAKE_CXXFLAGS += -Wno-unused-const-variable
LIBS += -ldl
# LIBS += -ldl
}

# increase system stack size (helpful for recursive programs)
Expand Down
36 changes: 35 additions & 1 deletion 3-NumericConversion/src/NumericConversion.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <iostream>
#include <string>
#include "console.h"
#include "simpio.h"
//#include "strlib.h"
using namespace std;

// Function prototypes
Expand All @@ -9,6 +11,38 @@ int stringToInt(string str);


int main() {
// TODO: fill in the code
while (true) {
string str = getLine("Enter a string: ");
int number = stringToInt(str);
cout << str << " -> " << number << endl;
int num = getInteger("Enter a number (add a trailing space at the end): ");
string string = intToString(num);
cout << num << " -> " << string << endl;
}
return 0;
}

int stringToInt(string str) {
int sign = 1;
if (str[0] == '-') {
sign = -1; //transform into negative int
str = str.substr(1);
}
if (str == "") return 0;
else {
// Going from the end
return sign*((static_cast<int>(str[str.length()-1]) - static_cast<int>('0'))
+ stringToInt(str.substr(0, str.length()-1))*10);
}
}

string intToString(int n) {
string str;
if (n < 0) {
str = "-"; // add negative sign and transform absolute value
n *= -1;
}
if (n == 0) return "0";
if (n%10 != 0 & n/10 == 0) return str + char(n%10 + '0');
else return str + intToString(n/10) + char(n%10 + '0');
}
Loading