Skip to content

Oleksandr #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
28 changes: 15 additions & 13 deletions exercises/basicTypes/basicTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,61 @@
int main() {
std::cout << "Using literals of different number types:\n";
print(5);
print(5/2); //FIXME
print(5.0/2.0); //FIXME змінимо тип змінної на "double"
print(100/2ull);
print(2 + 4ull);
print(2.f + 4ull);
print(0u - 1u); // FIXME
print(1.0000000001f); // FIXME Why is this number not represented correctly?
print(1. + 1.E-18); // FIXME
print(0 - 1); // FIXME Видалимо "u"
print(1.0000000001L ); // FIXME Змінимо тип змінної на "long double" або "double" Why is this number not represented correctly?
print(1.l + 1.E-18); // FIXME Змінимо тип "1" на "long double"

std::cout << "\nUsing increment and decrement operators:\n";
int a = 1;
int b;
int c;
print(b = a++); // Q: What is the difference between a++ and ++a?
print(b = a++); // Q: What is the difference between a++ and ++a? ++a фиконується раніше за a++
print(c = ++a);
print(a);
print(b);
print(c);

std::cout << "\nCompound assignment operators:\n";
int n = 1;
print(n *= 2); // Q: Is there a difference between this and the next line?
print(n *= 2); // Q: Is there a difference between this and the next line? У строці 39 змінна "n" спочатку переводиться в "double" для підрахунків. А потім записується у форматі "int".
print(n *= 2.9);
print(n -= 1.1f);
print(n /= 4); // Q: Based on the results of these expressions, is there a better type to be used for n?
print(n /= 4); // Q: Based on the results of these expressions, is there a better type to be used for n? "float" або "double"

std::cout << "\nLogic expressions:\n";
const bool alwaysTrue = true;
bool condition1 = false;
bool condition2 = true;
print( alwaysTrue && condition1 && condition2 );
print( alwaysTrue || condition1 && condition2 ); // Q: Why does operator precedence render this expression useless?
print( alwaysTrue || condition1 && condition2 ); // Q: Why does operator precedence render this expression useless? alwaysTrue завжди дає значення "1", а Оператор "&&" має вищий пріоретет за "||" отже ми зря обчислюємо "condition1 && condition2"
print( alwaysTrue && condition1 || condition2 );
print(condition1 != condition1); // Q: What is the difference between this and the following expression?
print(condition1 != condition1); // Q: What is the difference between this and the following expression? У строці 51 присвоюється протилежне значення для "condition2"
print(condition2 = !condition2);
print( alwaysTrue && condition1 && condition2 );
print( alwaysTrue || condition1 && condition2 );
print( alwaysTrue && condition1 || condition2 );

std::cout << '\n';
print( false || 0b10 ); // Q: What is the difference between || and | ?
print( false || 0b10 ); // Q: What is the difference between || and | ? "|" побітова операція, перевіряє усі цисла, записує значення в "int"
print( false | 0b10 );
printBinary( 0b1 & 0b10 );
printBinary( 0b1 | 0b10 );
printBinary( 0b1 && 0b10 ); // Q: Are the operators && and || appropriate for integer types?
printBinary( 0b1 && 0b10 ); // Q: Are the operators && and || appropriate for integer types? ні тікі для bool
printBinary( 0b1 || 0b10 );

std::cout << "\nPlay with characters and strings:\n";
print("a"); // Q: Why is this expression two bytes at run time, the next only one?
print("a"); // Q: Why is this expression two bytes at run time, the next only one? ".." Містять пихований символ '/0'
print('a');

char charArray[20];
//charArray[19] = '/0'; // Make sure that our string is terminated with the null byte
//Для виконання останнього завдання
std::fill(std::begin(charArray), std::end(charArray), '\0');
char* charPtr = charArray;
charArray[19] = 0; // Make sure that our string is terminated with the null byte

print(charArray);
print(charArray[0] = 'a');
Expand Down
45 changes: 22 additions & 23 deletions exercises/control/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,41 @@ bool isodd(unsigned int i) { return i % 2 == 1; }
void part1() {
unsigned int sum_odd = 0;
unsigned int sum_eve = 0;
for (int i = 0; i < 9; ++i) {
unsigned int num = numbers[i];
if (isodd(num)) {
sum_odd += num;
} else {
sum_eve += num;
}

for (unsigned int num : numbers) {
(isodd(num) ? sum_odd : sum_eve) += num;
}
std::cout << "Sums: odd = " << sum_odd << ", even = " << sum_eve << "\n";
}

void part2() {
// print smallest n for which 1 + 2 + ... + n > 10000
int sum = 0;
for (int i = 1; ; i++) {
int i=0;
/*while (sum<=10000) {
i++;
sum += i;
if (sum > 10000) {
std::cout << i << "\n";
break;
}
}
}*/
do{
i++;
sum += i;
}while (sum<=10000);
std::cout << i << "\n";
}

enum class Language { English, French, German, Italian, Other };

void part3(Language l) {
if (l == Language::English) {
std::cout << "Hello\n";
} else if (l == Language::French) {
std::cout << "Salut\n";
} else if (l == Language::German) {
std::cout << "Hallo\n";
} else if (l == Language::Italian) {
std::cout << "Ciao\n";
} else {
std::cout << "I don't speak your language\n";
switch (l) {
case Language::English: std::cout << "Hello\n";
break;
case Language::French: std::cout << "Salut\n";
break;
case Language::German: std::cout << "Hallo\n";
break;
case Language::Italian: std::cout << "Ciao\n";
break;
default: std::cout << "Don't speak your language\n";
}
}

Expand Down
4 changes: 2 additions & 2 deletions exercises/debug/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ int main()
{
constexpr auto arraySize = 100;
int* v = nullptr;
// create and reverse the vector of LEN numbers
reverse(v, 1000);
// create and reverse the vector of LEN number
v = createAndFillVector(arraySize);
reverse(v, arraySize);

// check if the revert worked:
const bool isReversed = std::is_sorted(v, v + arraySize, std::greater {});
Expand Down
15 changes: 14 additions & 1 deletion exercises/functions/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@ void printName(FastToCopy argument) {
std::cout << argument.name << '\n';
}

void inefficientPrintName(SlowToCopy argument) {
std::cout << argument.name << '\n';
argument.name = "a";
}

void printName(const SlowToCopy & argument) {
std::cout << argument.name << '\n';

}

int main() {
FastToCopy fast = {"Fast"};
printName(fast);

SlowToCopy slow = {"Slow"};
// print it here
printName(slow);

std::cout << "Now printing with copy:\n";
inefficientPrintName(slow);

return 0;
}
10 changes: 8 additions & 2 deletions exercises/loopsRefsAuto/loopsRefsAuto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ int main() {
// Task 1:
// Write a for loop that initialises each struct's resultA and resultB with ascending integers.
// Verify the output of the program before and after you do this.


for ( int i = 0 ; i<10 ; ++i ) {
collection[i].resultA = i;
collection[i].resultB = 2*i;
}
// було resultA = 0, resultB = 0
// стало resultA = 45, resultB = 90

// Task 2:
// We use a range-based for loop to analyse the array of structs.
Expand All @@ -28,7 +34,7 @@ int main() {
// Hint: Fix the type declaration "auto" in the loop head.
int resultA = 0;
int resultB = 0;
for (auto item : collection) {
for (auto const & item : collection) {
resultA += item.resultA;
resultB += item.resultB;
}
Expand Down
62 changes: 62 additions & 0 deletions exercises/operators/operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,68 @@
class Fraction {
public:
// TODO: constructors and operators

// Створюємо дріб
Fraction(int num) : m_num(num), m_denom(1) {}

Fraction(unsigned int num, unsigned int denom)
: m_num(num), m_denom(denom) {
normalize();
}

// Оператор виводу
friend std::ostream& operator<<(std::ostream& os, const Fraction& f) {
os << f.m_num << '/' << f.m_denom;
return os;
}

//перевірка через оператори
friend bool operator==(const Fraction& a, const Fraction& b) {
return a.m_num == b.m_num && a.m_denom == b.m_denom;
}

friend bool operator!=(const Fraction& a, const Fraction& b) {
return !(a == b); // використовуємо вже реалізований ==
}

//Оператори множення
Fraction& operator*=(const Fraction& other) {
m_num *= other.m_num;
m_denom *= other.m_denom;
normalize();
return *this;
}

friend Fraction operator*(Fraction a, const Fraction& b) {
a *= b; // використовуємо оператор *=
return a;
}

friend Fraction operator*(Fraction f, int x) {
return f * Fraction(x);
}

friend Fraction operator*(int x, Fraction f) {
return f * x; // симетрія
}

//Оператори порівняння
friend bool operator<(const Fraction& a, const Fraction& b) {
return a.m_num * b.m_denom < b.m_num * a.m_denom;
}

friend bool operator>(const Fraction& a, const Fraction& b) {
return b < a;
}

friend bool operator<=(const Fraction& a, const Fraction& b) {
return !(b < a);
}

friend bool operator>=(const Fraction& a, const Fraction& b) {
return !(a < b);
}


private:
void normalize() {
Expand Down
8 changes: 8 additions & 0 deletions exercises/polymorphism/trypoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@

int main() {
// create a Pentagon, call its perimeter method
Pentagon penta{1.0};
std::cout << "Penta : perimeter = " << penta.computePerimeter() << "\n\n";


// create a Hexagon, call its perimeter method
Hexagon hexta{1.0};
std::cout << "Hexta : perimeter = " << hexta.computePerimeter() << "\n\n";


// create a Hexagon, call the perimeter method through a reference to Polygon
Hexagon hexta2{1.0};
RegularPolygon &poly = hexta2;
std::cout << "Hexa : perimeter = " << hexta2.computePerimeter() << '\n'
<< "Hexa as Poly : perimeter = " << poly.computePerimeter() << '\n';


// retry virtual method
Expand Down
6 changes: 3 additions & 3 deletions exercises/virtual_inheritance/TextBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ void Drawable::draw() const {
Rectangle::Rectangle(int id, float width, float height) :
Drawable(id), m_width(width), m_height(height) {}

Text::Text(int id, const std::string &content) :
Text::Text(int id, std::string content) :
Drawable(id), m_content(content) {}

TextBox::TextBox(const std::string &content,
TextBox::TextBox(std::string content,
float width, float height) :
Rectangle(1, width, height), Text(2, content) {}
Drawable(3), Rectangle(1, width, height), Text(2, content) {}
10 changes: 5 additions & 5 deletions exercises/virtual_inheritance/TextBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ class Drawable {
int m_id;
};

class Rectangle : public Drawable {
class Rectangle : public virtual Drawable {
public:
Rectangle(int id, float width, float height);
protected:
float m_width;
float m_height;
};

class Text : public Drawable {
class Text : public virtual Drawable {
public:
Text(int id, const std::string &content);
Text(int id, std::string content);
protected:
std::string m_content;
};

class TextBox : public Rectangle, public Text {
public:
TextBox(const std::string &content,
TextBox(std::string content,
float width, float height);
};
}
15 changes: 7 additions & 8 deletions exercises/virtual_inheritance/trymultiherit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
#include <iostream>

int main() {
// create a TextBox and call draw

TextBox tb("my text", 10, 5);
Rectangle &r = tb;
r.draw();
Text &t = tb;
t.draw();


// Fix the code to call both draws by using types


// try with virtual inheritance


}
}