diff --git a/exercises/basicTypes/basicTypes.cpp b/exercises/basicTypes/basicTypes.cpp index 3a46116b..471ffc38 100644 --- a/exercises/basicTypes/basicTypes.cpp +++ b/exercises/basicTypes/basicTypes.cpp @@ -15,19 +15,19 @@ 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); @@ -35,39 +35,41 @@ int main() { 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'); diff --git a/exercises/control/control.cpp b/exercises/control/control.cpp index 574b9ed7..31700220 100644 --- a/exercises/control/control.cpp +++ b/exercises/control/control.cpp @@ -7,13 +7,9 @@ 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"; } @@ -21,28 +17,31 @@ void part1() { 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"; } } diff --git a/exercises/debug/debug.cpp b/exercises/debug/debug.cpp index a09da9de..044fa6ea 100644 --- a/exercises/debug/debug.cpp +++ b/exercises/debug/debug.cpp @@ -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 {}); diff --git a/exercises/functions/functions.cpp b/exercises/functions/functions.cpp index c532bcb6..384efc59 100644 --- a/exercises/functions/functions.cpp +++ b/exercises/functions/functions.cpp @@ -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; } diff --git a/exercises/loopsRefsAuto/loopsRefsAuto.cpp b/exercises/loopsRefsAuto/loopsRefsAuto.cpp index bc0e5ced..ebfc1dba 100644 --- a/exercises/loopsRefsAuto/loopsRefsAuto.cpp +++ b/exercises/loopsRefsAuto/loopsRefsAuto.cpp @@ -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. @@ -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; } diff --git a/exercises/operators/operators.cpp b/exercises/operators/operators.cpp index 163da43e..aff5704e 100644 --- a/exercises/operators/operators.cpp +++ b/exercises/operators/operators.cpp @@ -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() { diff --git a/exercises/polymorphism/trypoly.cpp b/exercises/polymorphism/trypoly.cpp index 97e200df..c2d8c2cd 100644 --- a/exercises/polymorphism/trypoly.cpp +++ b/exercises/polymorphism/trypoly.cpp @@ -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 diff --git a/exercises/virtual_inheritance/TextBox.cpp b/exercises/virtual_inheritance/TextBox.cpp index a790a978..9303d913 100644 --- a/exercises/virtual_inheritance/TextBox.cpp +++ b/exercises/virtual_inheritance/TextBox.cpp @@ -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) {} \ No newline at end of file diff --git a/exercises/virtual_inheritance/TextBox.hpp b/exercises/virtual_inheritance/TextBox.hpp index c0f86af8..10bc2ebe 100644 --- a/exercises/virtual_inheritance/TextBox.hpp +++ b/exercises/virtual_inheritance/TextBox.hpp @@ -10,7 +10,7 @@ class Drawable { int m_id; }; -class Rectangle : public Drawable { +class Rectangle : public virtual Drawable { public: Rectangle(int id, float width, float height); protected: @@ -18,15 +18,15 @@ class Rectangle : public Drawable { 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); -}; +} diff --git a/exercises/virtual_inheritance/trymultiherit.cpp b/exercises/virtual_inheritance/trymultiherit.cpp index 8410cecc..abbdcba8 100644 --- a/exercises/virtual_inheritance/trymultiherit.cpp +++ b/exercises/virtual_inheritance/trymultiherit.cpp @@ -2,13 +2,12 @@ #include 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 - - -} +} \ No newline at end of file