diff --git a/02_week/tasks/func_array/func_array.cpp b/02_week/tasks/func_array/func_array.cpp index b327e68d..24dd8124 100644 --- a/02_week/tasks/func_array/func_array.cpp +++ b/02_week/tasks/func_array/func_array.cpp @@ -1,6 +1,16 @@ #include +double ApplyOperations(double a, double b, double (*func[])(double x, double y), size_t size) +{ + long double sum = 0.; + if (func) + { + for (size_t i = 0; i < size; ++i) + { + if (*(func+i)) + sum += func[i](a, b); + } + } -double ApplyOperations(double a, double b /* other arguments */) { - throw std::runtime_error{"Not implemented"}; + return sum; } \ No newline at end of file diff --git a/02_week/tasks/last_of_us/last_of_us.cpp b/02_week/tasks/last_of_us/last_of_us.cpp index c7bf1a25..200660ea 100644 --- a/02_week/tasks/last_of_us/last_of_us.cpp +++ b/02_week/tasks/last_of_us/last_of_us.cpp @@ -1,6 +1,17 @@ #include - -/* return_type */ FindLastElement(/* ptr_type */ begin, /* ptr_type */ end, /* func_type */ predicate) { - throw std::runtime_error{"Not implemented"}; +const int* FindLastElement(const int* begin, const int* end, bool predicate(int x)) +{ + if (predicate && begin && end) + { + size_t size = end - begin; + for (long i = size-1; i>=0; --i) + { + if (predicate(begin[i])) + { + return (begin + i); + } + } + } + return end; } \ No newline at end of file diff --git a/02_week/tasks/little_big/little_big.cpp b/02_week/tasks/little_big/little_big.cpp index abe24379..ed7e6328 100644 --- a/02_week/tasks/little_big/little_big.cpp +++ b/02_week/tasks/little_big/little_big.cpp @@ -1,10 +1,70 @@ #include +#include +#include -void PrintMemory(int /* write arguments here */) { - throw std::runtime_error{"Not implemented"}; +void PrintMemory(int num, bool is_inverted = false) +{ + + int size = sizeof(int); + unsigned char *conv = reinterpret_cast(&num); // указатель на конкретный бит в инте + std::string str; // строка, которую мы выведем + + char dict[] = {"0123456789ABCDEF"}; // массив символов, в которые мы будем переводить + + if (is_inverted) //big endian + { + conv += size-1; + for (; conv >= reinterpret_cast(&num); --conv) + { + str.push_back(dict[*conv >> 4]); + str.push_back(dict[*conv & 0b00001111]); + } + } + + else // little endian + { + for (; conv - reinterpret_cast(&num) < size; ++conv) + { + str.push_back(dict[*conv >> 4]); + str.push_back(dict[*conv & 0b00001111]); + } + } + std::cout << "0x" << str << std::endl; + + } -void PrintMemory(double /* write arguments here */) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file +void PrintMemory(double num, bool is_inverted = false) +{ + + int size = sizeof(double); + unsigned char *conv = reinterpret_cast(&num); // указатель на конкретный бит в дабле + std::string str;// строка, которую мы выведем + + char dict[] = {"0123456789ABCDEF"}; // массив символов, в которые мы будем переводить + + if (is_inverted) //big endian + { + conv += size-1; + for (; conv >= reinterpret_cast(&num); --conv) + { + str.push_back(dict[*conv >> 4]); + str.push_back(dict[*conv & 0b00001111]); + } + } + + else // little endian + { + for (; conv - reinterpret_cast(&num) < size; ++conv) + { + str.push_back(dict[*conv >> 4]); + str.push_back(dict[*conv & 0b00001111]); + } + } + + + std::cout << "0x" << str << std::endl; + + +} diff --git a/02_week/tasks/longest/longest.cpp b/02_week/tasks/longest/longest.cpp index 04b3c354..c4cd9c2a 100644 --- a/02_week/tasks/longest/longest.cpp +++ b/02_week/tasks/longest/longest.cpp @@ -1,6 +1,55 @@ #include +char *FindLongestSubsequence(char *begin, char *end, size_t &count) +{ + count = 0; + if (begin && end) + { -/* return_type */ FindLongestSubsequence(/* ptr_type */ begin, /* ptr_type */ end, /* type */ count) { - throw std::runtime_error{"Not implemented"}; + char *subsequence = nullptr; + for (long i = 0; i < end - begin; ++i) + { + char *newsequence = begin + i; + size_t t = 1; + while (*newsequence == *(newsequence + t) && newsequence + t < end) + { + ++i; + ++t; + } + if (t > count) + { + count = t; + subsequence = newsequence; + } + } + return subsequence; + } + return nullptr; +} + +const char *FindLongestSubsequence(const char *begin, const char *end, size_t &count) +{ + count = 0; + size_t pos = 0; + if (begin && end) + { + + for (long i = 0; i < end - begin; ++i) + { + size_t t = 1; + const char *newsubsequence = begin + i; + while (*newsubsequence == *(newsubsequence + t) && i < end - begin - 1) + { + ++i; + ++t; + } + if (t > count) + { + count = t; + pos = i - t + 1; + } + } + return begin + pos; + } + return nullptr; } diff --git a/02_week/tasks/pretty_array/pretty_array.cpp b/02_week/tasks/pretty_array/pretty_array.cpp index 48eab341..2d7617b5 100644 --- a/02_week/tasks/pretty_array/pretty_array.cpp +++ b/02_week/tasks/pretty_array/pretty_array.cpp @@ -1,6 +1,51 @@ #include +#include +#include + +void PrintArray(std::nullptr_t, std::nullptr_t) +{ + std::cout<<"[]\n"; +} + + + + +template +void PrintArray(T* begin, T* end, size_t limiter = 0) +{ + if (begin && end && begin != end) + { + std::cout << '['; + + size_t len = abs(begin-end); + if (begin < end) + { + for (size_t i = 0; i < len; ++i) + { + if (limiter && i != 0 && !(i % limiter)) + std::cout << "...\n" << ' '; + + i!=(len-1)? ( std::cout << begin[i] << ", "): ( std::cout << begin[i] << "]\n"); + + } + + } + else + { + for (size_t i = 0; i < len; ++i) + { + if (limiter && i != 0 && !(i % limiter)) + std::cout << "...\n" << ' '; + T *current = begin - i; + current == end + 1 ? (std::cout << *current) : (std::cout << *current << ", "); + } + std::cout<<"]\n"; + } + } + else + { + std::cout<<"[]\n"; + } +} -void PrintArray(/* write arguments here */) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file diff --git a/02_week/tasks/swap_ptr/swap_ptr.cpp b/02_week/tasks/swap_ptr/swap_ptr.cpp index 93db625d..0064efe8 100644 --- a/02_week/tasks/swap_ptr/swap_ptr.cpp +++ b/02_week/tasks/swap_ptr/swap_ptr.cpp @@ -1,6 +1,34 @@ #include -void SwapPtr(/* write arguments here */) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file + + +void SwapPtr(int*& a, int*& b) +{ + int* c = a; + a = b; + b = c; +} + +void SwapPtr(const int*& a, const int*& b){ + const int* c = a; + a = b; + b = c; +} + +void SwapPtr(int**& a, int**& b) +{ + int** c = a; + a = b; + b = c; +} + + +/* рабочий вариант через template для любого типа данных. на лекции не проходили, просто показать что я нашел и такую штуку +template +void SwapPtr(T*& a, T*& b){ + T* c = a; + a = b; + b = c; +} + */ \ No newline at end of file diff --git a/03_week/tasks/data_stats/data_stats.cpp b/03_week/tasks/data_stats/data_stats.cpp index b941c211..729a8e74 100644 --- a/03_week/tasks/data_stats/data_stats.cpp +++ b/03_week/tasks/data_stats/data_stats.cpp @@ -1,11 +1,38 @@ #include - +#include +#include struct DataStats { double avg = 0.0; double sd = 0.0; }; -/* return_type */ CalculateDataStats(/* args */) { - throw std::runtime_error{"Not implemented"}; +struct DataStats CalculateDataStats(std::vector data) { + + struct DataStats output; + if (data.empty()){ + output.avg = 0.; + output.sd = 0.; + return output; + } + + /* + стандартная девиация считается как sqrt(сумма((i - avg)^2)/size) + если разложим квадратное уравнение: sqrt(сумма(i^2 - 2*i*avg + avg^2)/size)=> + sqrt((сумма(i^2)+ сумма(- 2* i*avg)+сумма(avg^2))/size) - его имплементируем + */ + + double first_num = 0.; + double second_num= 0.; + + for (size_t i=0; i< data.size(); ++i){ + output.avg+=data[i]; + first_num += static_cast(data[i])*static_cast(data[i]); + second_num+= data[i]; + } + output.avg /= data.size(); + output.sd = first_num - 2. * second_num * output.avg + output.avg * output.avg * data.size(); + output.sd /= data.size(); + output.sd = sqrt(output.sd); + return output; } diff --git a/03_week/tasks/easy_compare/easy_compare.cpp b/03_week/tasks/easy_compare/easy_compare.cpp index dd5cb7f6..adb5d420 100644 --- a/03_week/tasks/easy_compare/easy_compare.cpp +++ b/03_week/tasks/easy_compare/easy_compare.cpp @@ -1,16 +1,49 @@ #include +#include - -struct Date { +struct Date +{ unsigned year; unsigned month; unsigned day; }; -struct StudentInfo { +struct StudentInfo +{ size_t id; char mark; int score; unsigned course; Date birth_date; -}; \ No newline at end of file +}; + +bool operator<(const Date &s1, const Date &s2) +{ + return std::tie(s1.year, s1.month, s1.day) < std::tie(s2.year, s2.month, s2.day); +} + +bool operator==(const Date &s1, const Date &s2) +{ + return std::tie(s1.year, s1.month, s1.day) == std::tie(s2.year, s2.month, s2.day); +} + +bool operator==(const StudentInfo &s1, const StudentInfo &s2) +{ + return std::tie(s1.mark, s1.score) == std::tie(s2.mark, s2.score); +} + +bool operator<(const StudentInfo &s1, const StudentInfo &s2) +{ + return std::tie(s2.mark, s1.score, s2.course, s1.birth_date) < + std::tie(s1.mark, s2.score, s1.course, s2.birth_date); +} + +bool operator!=(const StudentInfo &s1, const StudentInfo &s2) { return !(s1 == s2); } +bool operator>(const StudentInfo &s1, const StudentInfo &s2) { return s2 < s1; } +bool operator<=(const StudentInfo &s1, const StudentInfo &s2) { return !(s2 < s1); } +bool operator>=(const StudentInfo &s1, const StudentInfo &s2) { return !(s1 < s2); } + +bool operator!=(const Date &d1, const Date &d2) { return !(d1 == d2); } +bool operator>(const Date &d1, const Date &d2) { return d2 < d1; } +bool operator<=(const Date &d1, const Date &d2) { return !(d2 < d1); } +bool operator>=(const Date &d1, const Date &d2) { return !(d1 < d2); } diff --git a/03_week/tasks/enum_operators/enum_operators.cpp b/03_week/tasks/enum_operators/enum_operators.cpp index a539be38..e9815b2c 100644 --- a/03_week/tasks/enum_operators/enum_operators.cpp +++ b/03_week/tasks/enum_operators/enum_operators.cpp @@ -1,7 +1,9 @@ #include -#include +#include +#include -enum class CheckFlags : uint8_t { +enum class CheckFlags : u_int8_t +{ NONE = 0, TIME = (1 << 0), DATE = (1 << 1), @@ -12,22 +14,97 @@ enum class CheckFlags : uint8_t { ALL = TIME | DATE | USER | CERT | KEYS | DEST }; -/* return_type */ operator|(/* args */) { - throw std::runtime_error{"Not implemented"}; +CheckFlags operator|(CheckFlags f, CheckFlags s) +{ + return static_cast(((static_cast(f) & 0x3F) | (static_cast(s) & 0x3F))); } -/* return_type */ operator&(/* args */) { - throw std::runtime_error{"Not implemented"}; +bool operator&(CheckFlags f, CheckFlags s) +{ + // Преобразуем в числовые значения + u_int8_t f_val = static_cast(f) & 0x3F; + u_int8_t s_val = static_cast(s) & 0x3F; + + // Если один из операндов NONE, возвращаем false + if (f_val == 0 || s_val == 0) + return false; + + // Проверяем пересечение флагов + return ((f_val & s_val) ==f_val)||((f_val & s_val)==s_val); } - -/* return_type */ operator^(/* args */) { - throw std::runtime_error{"Not implemented"}; +CheckFlags operator^(CheckFlags f, CheckFlags s) +{ + return static_cast((static_cast(f) ^ static_cast(s))& 0x3F); } -/* return_type */ operator~(/* args */) { - throw std::runtime_error{"Not implemented"}; +CheckFlags operator~(CheckFlags f) +{ + return static_cast(~static_cast(f)& 0x3F); } -/* return_type */ operator<<(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::ostream& operator<<(std::ostream& out, CheckFlags flags) +{ + u_int8_t value = static_cast(flags) & 0x3F; + + if (value > 0b00111111 || value == static_cast(CheckFlags::NONE)) + { // Если установлены биты вне диапазона 0-5 + out << "NONE"; + return out; + } + + /*if (value == static_cast(CheckFlags::ALL)) + { + out << "CheckFlags::ALL"; + return out; + }*/ + + bool first = true; + + if (static_cast(flags) & static_cast(CheckFlags::TIME)) + { + out << "TIME"; + first = false; + } + + if (static_cast(flags) & static_cast(CheckFlags::DATE)) + { + if (!first) + out << ", "; + first = false; + out << "DATE"; + } + + if (static_cast(flags) & static_cast(CheckFlags::USER)) + { + if (!first) + out << ", "; + first = false; + out << "USER"; + } + + if (static_cast(flags) & static_cast(CheckFlags::CERT)) + { + if (!first) + out << ", "; + first = false; + out << "CERT"; + } + + if (static_cast(flags) & static_cast(CheckFlags::KEYS)) + { + if (!first) + out << ", "; + first = false; + out << "KEYS"; + } + + if (static_cast(flags) & static_cast(CheckFlags::DEST)) + { + if (!first) + out << ", "; + first = false; + out << "DEST"; + } + + return out; } diff --git a/03_week/tasks/filter/filter.cpp b/03_week/tasks/filter/filter.cpp index 6648cb39..4c15b97a 100644 --- a/03_week/tasks/filter/filter.cpp +++ b/03_week/tasks/filter/filter.cpp @@ -1,6 +1,23 @@ #include +#include - -/* return_type */ Filter(/* args */) { - throw std::runtime_error{"Not implemented"}; +void Filter(std::vector &data, bool (*func)(int)) +{ + if (!data.empty() && func != nullptr) + { + size_t counter = 0; + + for (size_t i = 0; i < data.size(); ++i) + { + if (func(data[i])) + { + if (i!= counter){ + data[counter] = data[i]; + } + ++counter; + } + } + data.resize(counter); + } + return; } \ No newline at end of file diff --git a/03_week/tasks/find_all/find_all.cpp b/03_week/tasks/find_all/find_all.cpp index 74f393b2..ce2a47ce 100644 --- a/03_week/tasks/find_all/find_all.cpp +++ b/03_week/tasks/find_all/find_all.cpp @@ -1,6 +1,22 @@ #include +#include - -/* return_type */ FindAll(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::vector FindAll(const std::vector& data, bool (*func)(int)) +{ + std::vector found; + if (func && !data.empty()) + { + size_t counter = 0; + for (size_t i = 0; i < data.size(); ++i) + { + if (func(data[i])) + { + found.push_back(i); + ++counter; + } + } + found.resize(counter); + found.shrink_to_fit(); + } + return found; } \ No newline at end of file diff --git a/03_week/tasks/minmax/minmax.cpp b/03_week/tasks/minmax/minmax.cpp index c2869799..16ef0802 100644 --- a/03_week/tasks/minmax/minmax.cpp +++ b/03_week/tasks/minmax/minmax.cpp @@ -1,6 +1,36 @@ #include +#include +#include +std::pair::const_iterator, std::vector::const_iterator> MinMax(const std::vector &data) +{ -/* return_type */ MinMax(/* args */) { - throw std::runtime_error{"Not implemented"}; + if (!data.empty()) + { + std::pair::const_iterator, std::vector::const_iterator> counters = {data.cbegin(), data.cbegin()}; + size_t size = data.size(); + + for (size_t i = 0; i < size; ++i) + { + std::vector::const_iterator it = data.cbegin() + i; + if (*it < *counters.first) + { + counters.first = it; + } + else + { + if (*it >= *counters.second) + { + counters.second = it; + } + } + } + + return counters; + } + else + { + std::pair::const_iterator, std::vector::const_iterator> counters; + return counters; + } } diff --git a/03_week/tasks/os_overload/os_overload.cpp b/03_week/tasks/os_overload/os_overload.cpp index e473418d..743a1c59 100644 --- a/03_week/tasks/os_overload/os_overload.cpp +++ b/03_week/tasks/os_overload/os_overload.cpp @@ -1,21 +1,58 @@ #include #include #include +#include - -struct Coord2D { +struct Coord2D +{ int x; int y; }; -struct Circle { +struct Circle +{ Coord2D coord; unsigned radius; + + // конструкторы + Circle() : coord(), radius(1) {} + Circle(Coord2D coords, unsigned r = 1) : coord(coords), radius(r) {} }; using CircleRegion = std::pair; using CircleRegionList = std::vector; -/* return_type */ operator<<(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::ostream& operator<<(std::ostream& os, Coord2D circle) +{ + return os << '(' << circle.x << ", " << circle.y << ')'; +} + +std::ostream& operator<<(std::ostream& os, Circle circle) +{ + if (!circle.radius) + return os << "circle[]"; + return os << "circle[" << circle.coord << ", r = " << circle.radius << "]"; } + +std::ostream& operator<<(std::ostream& os, CircleRegion circle) +{ + circle.second ? (os << '+') : (os << '-'); + return os << circle.first; +} + +std::ostream& operator<<(std::ostream& os, CircleRegionList circle) +{ + if (circle.empty()) + { + return os << "{}"; + } + else + { + os << "{\n"; + for (size_t i = 0; i < circle.size(); ++i) + { + i + 1 == circle.size() ? (os << "\t" << circle[i] << "\n") : (os << "\t" << circle[i] << ",\n"); + } + return os << '}'; + } +} \ No newline at end of file diff --git a/03_week/tasks/range/range.cpp b/03_week/tasks/range/range.cpp index d2085495..f66545b2 100644 --- a/03_week/tasks/range/range.cpp +++ b/03_week/tasks/range/range.cpp @@ -1,7 +1,21 @@ #include #include +#include -std::vector Range(int from, int to, int step) { - throw std::runtime_error{"Not implemented"}; +std::vector Range(int from, int to, int step = 1) { + long long it = static_cast(std::ceil((to - from)/static_cast(step))); + if (step != 0 && to != from && it > 0){ + std::vector result; + result.reserve(it); + for (size_t i = 0; i < static_cast(it); ++i){ + result.push_back(from + step*i); + } + result.shrink_to_fit(); + return result; + } + else{ + std::vector null; + return null; + } } diff --git a/03_week/tasks/unique/unique.cpp b/03_week/tasks/unique/unique.cpp index 9d2545bb..90481c29 100644 --- a/03_week/tasks/unique/unique.cpp +++ b/03_week/tasks/unique/unique.cpp @@ -1,6 +1,21 @@ #include #include -/* return_type */ Unique(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::vector Unique(const std::vector& v) { + std::vector result; + result.reserve(v.size()); + for (size_t i = 0; i < v.size(); ++i){ + bool is_in_result = false; + for (size_t k = 0; k < result.size(); ++k){ + if (result[k] == v[i]){ + is_in_result = true; + } + } + if (!is_in_result){ + result.push_back(v[i]); + } + + } + result.shrink_to_fit(); + return result; } diff --git a/04_week/tasks/queue/queue.cpp b/04_week/tasks/queue/queue.cpp index 2a9f8493..1346536a 100644 --- a/04_week/tasks/queue/queue.cpp +++ b/04_week/tasks/queue/queue.cpp @@ -1,6 +1,216 @@ #include +#include +class Queue +{ +public: + // конструкторы + Queue(std::initializer_list num) + { + auto it = num.begin(); + for (; it < num.end(); ++it) + { + this->Push(*it); + } + } -class Queue { + Queue(size_t size) + { + this->in->reserve(size); + this->out->reserve(size); + }; + Queue(int size) + { + this->in->reserve(size); + this->out->reserve(size); + }; + Queue(long size) + { + this->in->reserve(size); + this->out->reserve(size); + }; + Queue() {}; + + Queue(std::vector &v) + { + auto size = v.size(); + for (size_t i = 0; i < size; ++i) + { + this->Push(v[i]); + } + } + + Queue(std::stack s) + { + auto size = s.size(); + for (size_t i = 0; i < size; ++i) + { + this->out->push_back(s.top()); + s.pop(); + } + } + + Queue(const Queue& other) + : in(new std::vector(*other.in)) + , out(new std::vector(*other.out)) {} + + ~Queue() + { + delete in; + delete out; + } + + // методы + int &Front() + { + if (this->out->empty()) + { + while (!in->empty()) + { + out->push_back(in->back()); + in->pop_back(); + } + } + return this->out->back(); + } + int &Back() + { + if (!in->empty()) + return this->in->back(); + else + return this->out->front(); + } + const int &Front() const + { + Queue* temp = const_cast(this); + if (temp->out->empty()) + { + while (!temp->in->empty()) + { + temp->out->push_back(in->back()); + temp->in->pop_back(); + } + } + return temp->out->back(); + } + + const int &Back() const + { + Queue* temp = const_cast(this); + if (temp->out->empty()) + { + while (!temp->in->empty()) + { + temp->out->push_back(in->back()); + temp->in->pop_back(); + } + } + if (!in->empty()) + return temp->in->back(); + else + return temp->out->front(); + } + + size_t Size() + { + return this->in->size() + this->out->size(); + } + size_t Size() const + { + return this->in->size() + this->out->size(); + } + void Clear() + { + this->in->clear(); + this->out->clear(); + } + void Push(int a) + { + in_s.push_back(a); + } + bool Pop() + { + if (out->empty()) + { + while (!in->empty()) + { + out->push_back(in->back()); + in->pop_back(); + } + } + if (out->empty()) + { + return false; + } + out->pop_back(); + return true; + } + + bool Empty() + { + return in->empty() && out->empty(); + } + bool Empty() const + { + if (!in || !out) return true; + return in->empty() && out->empty(); + } + void Swap(Queue &q2) + { + std::vector *new_in = q2.get_ptr_in(); + std::vector *new_out = q2.get_ptr_out(); + + q2.set_in(in); + q2.set_out(out); + this->in = new_in; + this->out = new_out; + return; + } + bool operator==(const Queue& other) const { + if (Size() != other.Size()) return false; + + // Простое поэлементное сравнение + Queue copy1 = *this; + Queue copy2 = other; + + while (!copy1.Empty()) { + if (copy1.Front() != copy2.Front()) return false; + copy1.Pop(); + copy2.Pop(); + } + return true; + } + + bool operator!=(const Queue q2) const + { + return !(*this == q2); + } + +private: + + + std::vector *in = new std::vector; + std::vector *out = new std::vector; + std::vector& in_s = *in; + std::vector& out_s = *out; + + + std::vector *get_ptr_in() const + { + return this->in; + } + std::vector *get_ptr_out() const + { + return this->out; + } + + void set_in(std::vector *x) + { + this->in = x; + } + void set_out(std::vector *x) + { + this->out = x; + } };