Skip to content
Open
11 changes: 8 additions & 3 deletions 01_week/tasks/addition/addition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#include <stdexcept>


int64_t Addition(int a, int b) {
throw std::runtime_error{"Not implemented"};
}

int64_t Addition(int a, int b)
{
return static_cast<int64_t>(a) + static_cast<int64_t>(b);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лишний каст, второй операнд не следует приводить явно, так как он бы преобразовался с помощью неявного каста. Принято использовать такую возможность и не писать явный каст самостоятельно второй раз

  • лишний отступ (8 пробелов сейчас)

}



39 changes: 38 additions & 1 deletion 01_week/tasks/quadratic/quadratic.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
#include <stdexcept>
#include <cmath>
#include <iomanip>


void SolveQuadratic(int a, int b, int c) {
throw std::runtime_error{"Not implemented"};
if (a == 0)
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

оформление if должно быть в одном стиле

if (b == 0) {
if (c == 0) {
std::cout << "infinite solutions";
}
else {
std::cout << "no solutions";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше избавиться от вложженности и для каждого особого случая сделать короткую ветвь с return будет лучше читаться

}
}
else {
double sol = -static_cast<double>(c) / static_cast<double>(b);
std::cout << std::setprecision(6) << sol;
}
}
else {
// ���������� ���������
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Комментарии не видны, вероятно проблемы с кодировкой, нужно для исходного файла задать UTF-8, тогда в репозитории буудет корректно отражаться

double discriminant = static_cast<double>(b) * static_cast<double>(b) - 4.0 * static_cast<double>(a) * static_cast<double>(c);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лишние касты, достаточно у одного b, поскольку 4.0 литерал для числа с плавающей точкой, то a и b приведутся к double перед умножением на 4

if (discriminant < 0) {
std::cout << "no solutions";
}
else if (discriminant == 0) {
double sol = -b / (2.0 * a);
std::cout << std::setprecision(6) << sol;
}
else {
double sqr = std::sqrt(discriminant);
double x1 = (-b - sqr) / (2.0 * a);
double x2 = (-b + sqr) / (2.0 * a);
// ������������ ������� x1 < x2
if (x1 > x2) {
std::swap(x1, x2);
}
std::cout << std::setprecision(6) << x1 << " " << x2;
}
}
}
15 changes: 13 additions & 2 deletions 01_week/tasks/rms/rms.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
#include <cstdef>
#include <cstddef>
#include <stdexcept>
#include <cmath>


double CalculateRMS(double values[], size_t size) {
throw std::runtime_error{"Not implemented"};
if (size == 0 || values == nullptr) {
return 0.0;
}

double sumofSquares = 0.0;
for (size_t i = 0; i < size; ++i) {
sumofSquares += values[i] * values[i];
}

double meanofSquares = sumofSquares / static_cast<double>(size);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лишняя переменная и лишний каст, можно записать выражение сразу в строку нижу, так как переменная не используется больше, кастовать size к double не нужно он приведется неявно поскольку sumofSquares имеет тип double

return std::sqrt(meanofSquares);
}
18 changes: 15 additions & 3 deletions 02_week/tasks/func_array/func_array.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
#include <stdexcept>


double ApplyOperations(double a, double b /* other arguments */) {
throw std::runtime_error{"Not implemented"};
}
double ApplyOperations(double a, double b, double (**funks)(double, double), size_t size) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

funcs тогда, от слова functions, имена лучше не русифицирвоать, + нагляднее был бы массив указателей на функцию чем указатель на указатель на функцию

if (funks == nullptr || size == 0) {
return 0.0;
}

double Sum = 0.0;

for (size_t i = 0; i < size; ++i) {
if (funks[i] != nullptr) {
Sum += funks[i](a, b);
}
}

return Sum;
}
14 changes: 11 additions & 3 deletions 02_week/tasks/last_of_us/last_of_us.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include <stdexcept>


/* return_type */ FindLastElement(/* ptr_type */ begin, /* ptr_type */ end, /* func_type */ predicate) {
throw std::runtime_error{"Not implemented"};
}
int* FindLastElement(int* begin,int* end, bool (*predicate)(int)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

пробел после запятой пропал, + функция должна принимать и возвращать указатели на константу, так как функция не изменяет элементы и требований к возможности изменению по возвращаемому указателю нет

if (begin == nullptr || end == nullptr || begin > end) {
return end;
}
for (int* p = end - 1; p >= begin; --p) {
if (predicate(*p)) {
return p;
}
}
return end;
}
6 changes: 4 additions & 2 deletions 02_week/tasks/swap_ptr/swap_ptr.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <stdexcept>


void SwapPtr(/* write arguments here */) {
throw std::runtime_error{"Not implemented"};
void SwapPtr(int* a, int* b) {
int* c = a;
a = b;
b = c;
}
12 changes: 10 additions & 2 deletions 03_week/tasks/filter/filter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include <stdexcept>
#include <vector>


/* return_type */ Filter(/* args */) {
throw std::runtime_error{"Not implemented"};
void Filter(std::vector<int>& v, bool (*predicate)(int)) {
size_t ind = 0;
for (size_t i = 0; i < v.size(); ++i) {
if (predicate(v[i])) {
v[ind] = v[i];
++ind;
}
}
v.resize(ind);
}
13 changes: 11 additions & 2 deletions 03_week/tasks/find_all/find_all.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#include <stdexcept>
#include <vector>


/* return_type */ FindAll(/* args */) {
throw std::runtime_error{"Not implemented"};
std::vector<size_t> FindAll(const std::vector<int>& container, bool (*predicate)(int)) {
std::vector<size_t> cont2;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pos - позиции было бы лучше для навания


for (size_t i = 0; i < container.size(); ++i) {
if (predicate(container[i])) {
cont2.push_back(i);
}
}

return cont2;
}
24 changes: 22 additions & 2 deletions 03_week/tasks/unique/unique.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
#include <stdexcept>
#include <vector>

/* return_type */ Unique(/* args */) {
throw std::runtime_error{"Not implemented"};
std::vector<int> Unique(const std::vector<int>& v) {
if (v.empty()) {
return {};
}
size_t uniq = 1;
for (size_t i = 1; i < v.size(); ++i) {
if (v[i] != v[i - 1]) {
++uniq;
}
}
std::vector<int> res(uniq);
res[0] = v[0];

size_t index = 1;
for (size_t i = 1; i < v.size(); ++i) {
if (v[i] != v[i - 1]) {
res[index] = v[i];
++index;
}
}

return res;
}
4 changes: 2 additions & 2 deletions 04_week/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Гарантирует использов
set(EXAMPLES_DIR examples) # Определим переменную с именем директории
set(TASKS_DIR tasks)

add_subdirectory(tasks)
#add_subdirectory(tasks)

# Создать исполняемый файл для каждого примера
if (BUILD_EXAMPLES_04_WEEK)
# add_example(struct_examples ${EXAMPLES_DIR}/struct_examples.cpp)
endif()
endif()