diff --git a/01_week/tasks/addition/addition.cpp b/01_week/tasks/addition/addition.cpp index 92872802..aef47d9c 100644 --- a/01_week/tasks/addition/addition.cpp +++ b/01_week/tasks/addition/addition.cpp @@ -1,7 +1,6 @@ #include #include - int64_t Addition(int a, int b) { - throw std::runtime_error{"Not implemented"}; + return static_cast(a) + b; } \ No newline at end of file diff --git a/01_week/tasks/char_changer/char_changer.cpp b/01_week/tasks/char_changer/char_changer.cpp index 3a7344d9..11be8cc1 100644 --- a/01_week/tasks/char_changer/char_changer.cpp +++ b/01_week/tasks/char_changer/char_changer.cpp @@ -1,7 +1,47 @@ #include #include +#include size_t CharChanger(char array[], size_t size, char delimiter = ' ') { - throw std::runtime_error{"Not implemented"}; + size_t cntRepeat = 0; + char res[size] = {0}; + size_t resInd = 0; + + auto parse = [&](size_t i, char cymbol) { + if (i > 0 && array[i] == array[i-1]) { + if (cntRepeat != 1) + --resInd; + ++cntRepeat; + res[resInd] = (cntRepeat > 9) ? '0' : '0' + cntRepeat; + } + else { + res[resInd] = cymbol; + cntRepeat = 1; + } + ++resInd; + }; + + for (size_t i = 0; i < size; ++i) { + if ('0' <= array[i] && array[i] <= '9') + parse(i, '*'); + else if ('a' <= array[i] && array[i] <= 'z') + parse(i, array[i] - ' '); + else if ('A' <= array[i] && array[i] <= 'Z') + parse(i, array[i]); + else if (array[i] == ' ') { + res[resInd] = delimiter; + ++resInd; + while (array[i] == ' ') + ++i; + --i; + } + else if (array[i] != '\0') + parse(i, '_'); + } + + for (size_t i = 0; i < size; ++i) + array[i] = res[i]; + + return resInd; } diff --git a/01_week/tasks/check_flags/check_flags.cpp b/01_week/tasks/check_flags/check_flags.cpp index 75e7c652..4cdc735a 100644 --- a/01_week/tasks/check_flags/check_flags.cpp +++ b/01_week/tasks/check_flags/check_flags.cpp @@ -1,5 +1,6 @@ #include #include +#include enum class CheckFlags : uint8_t { @@ -14,5 +15,35 @@ enum class CheckFlags : uint8_t { }; void PrintCheckFlags(CheckFlags flags) { - throw std::runtime_error{"Not implemented"}; + uint8_t value = static_cast(flags); + + if (value == static_cast(CheckFlags::NONE)) { + std::cout << "[]"; + return; + } + else if (value >= 1 << 6) { + std::cout << ""; + return; + } + + std::cout << "["; + + bool first = true; + auto try_print = [&](CheckFlags f, const char* name) { + if (value & static_cast(f)) { + if (!first) + std::cout << ","; + std::cout << name; + first = false; + } + }; + + try_print(CheckFlags::TIME, "TIME"); + try_print(CheckFlags::DATE, "DATE"); + try_print(CheckFlags::USER, "USER"); + try_print(CheckFlags::CERT, "CERT"); + try_print(CheckFlags::KEYS, "KEYS"); + try_print(CheckFlags::DEST, "DEST"); + + std::cout << "]"; } diff --git a/01_week/tasks/length_lit/length_lit.cpp b/01_week/tasks/length_lit/length_lit.cpp index e69de29b..9e4adb3b 100644 --- a/01_week/tasks/length_lit/length_lit.cpp +++ b/01_week/tasks/length_lit/length_lit.cpp @@ -0,0 +1,17 @@ +#pragma once + +constexpr long double operator"" _ft_to_m(long double x) {return x * 0.3048L;} +constexpr long double operator"" _ft_to_cm(long double x) {return x * 30.48L;} +constexpr long double operator"" _ft_to_in(long double x) {return x * 12.0L;} + +constexpr long double operator"" _in_to_m(long double x) {return x * 0.0254L;} +constexpr long double operator"" _in_to_cm(long double x) {return x * 2.54L;} +constexpr long double operator"" _in_to_ft(long double x) {return x / 12.0L;} + +constexpr long double operator"" _m_to_ft(long double x) {return x * 3.280839895L;} +constexpr long double operator"" _m_to_in(long double x) {return x * 39.37007874L;} +constexpr long double operator"" _m_to_cm(long double x) {return x * 100.0L;} + +constexpr long double operator"" _cm_to_m(long double x) {return x / 100.0L;} +constexpr long double operator"" _cm_to_ft(long double x) {return x * 0.0328084L;} +constexpr long double operator"" _cm_to_in(long double x) {return x * 0.3937007874L;} \ No newline at end of file diff --git a/01_week/tasks/print_bits/print_bits.cpp b/01_week/tasks/print_bits/print_bits.cpp index a48a43c1..ffcf5076 100644 --- a/01_week/tasks/print_bits/print_bits.cpp +++ b/01_week/tasks/print_bits/print_bits.cpp @@ -1,7 +1,32 @@ #include #include +#include void PrintBits(long long value, size_t bytes) { - throw std::runtime_error{"Not implemented"}; + size_t bits = bytes*8; + bool reverse = false; + bool res[bits] = {0}; + + if (value < 0) { + reverse = true; + value = -value - 1; + } + + for (size_t i = bits; i > 0 && value != 0; --i) { + res[i-1] = value % 2; + value /= 2; + } + + if (reverse) + for (size_t i = 0; i < bits; ++i) + res[i] ^= 1; + + std::cout << "0b"; + for (size_t i = 0; i < bits; ++i) { + if (i % 4 == 0 && i != 0) + std::cout << '\''; + std::cout << res[i]; + } + std::cout << '\n'; } diff --git a/01_week/tasks/quadratic/quadratic.cpp b/01_week/tasks/quadratic/quadratic.cpp index abf7d632..0159ecbf 100644 --- a/01_week/tasks/quadratic/quadratic.cpp +++ b/01_week/tasks/quadratic/quadratic.cpp @@ -1,6 +1,49 @@ +#include #include +#include +#include void SolveQuadratic(int a, int b, int c) { - throw std::runtime_error{"Not implemented"}; + if (a == 0 && b == 0 && c == 0) + std::cout << "infinite solutions"; + else if (a == 0 && b == 0 && c != 0) + std::cout << "no solutions"; + else if (a == 0 && b != 0 && c == 0) + std::cout << "0"; + else if (a == 0 && b != 0 && c != 0) + std::cout << std::setprecision(6) << static_cast(-c) / b; + else if (a != 0 && b == 0 && c == 0) + std::cout << "0"; + else if (a != 0 && b == 0 && c != 0) { + if (c > 0) + std::cout << "no solutions"; + else { + long double x = sqrt(static_cast(-c) / a); + std::cout << std::setprecision(6) << -x << ' ' << x; + } + } + else if (a != 0 && b != 0 && c == 0) { + long double x = static_cast(-b) / a; + if (x < 0) + std::cout << std::setprecision(6) << x << " 0"; + else + std::cout << std::setprecision(6) << "0 " << x; + } + else { + long long d = static_cast(b)*b - 4LL*a*c; + if (d < 0) + std::cout << "no solutions"; + else if (d == 0) + std::cout << std::setprecision(6) << static_cast(-b) / (2.0L*a); + else { + long double sqrtd = sqrt(d); + long double x1 = (static_cast(-b) + sqrtd) / (2.0L*a); + long double x2 = (static_cast(-b) - sqrtd) / (2.0L*a); + if (x1 < x2) + std::cout << std::setprecision(6) << x1 << ' ' << x2; + else + std::cout << std::setprecision(6) << x2 << ' ' << x1; + } + } } \ No newline at end of file diff --git a/01_week/tasks/rms/rms.cpp b/01_week/tasks/rms/rms.cpp index 6882f0a9..f2eb7dc2 100644 --- a/01_week/tasks/rms/rms.cpp +++ b/01_week/tasks/rms/rms.cpp @@ -1,7 +1,13 @@ -#include #include +#include double CalculateRMS(double values[], size_t size) { - throw std::runtime_error{"Not implemented"}; + if (size == 0 || values == nullptr) + return 0.0; + long double sum = 0.0L; + for (size_t i = 0; i < size; ++i) + sum += values[i] * values[i]; + sum /= size; + return sqrt(sum); } \ No newline at end of file diff --git a/02_week/tasks/func_array/func_array.cpp b/02_week/tasks/func_array/func_array.cpp index b327e68d..73276d1e 100644 --- a/02_week/tasks/func_array/func_array.cpp +++ b/02_week/tasks/func_array/func_array.cpp @@ -1,6 +1,11 @@ +#include #include -double ApplyOperations(double a, double b /* other arguments */) { - throw std::runtime_error{"Not implemented"}; +double ApplyOperations(double a, double b, double (*funcs[])(double a, double b), size_t size) { + double res = 0.0; + for (size_t i = 0; i < size; ++i) + if (funcs[i]) + res += funcs[i](a, b); + return res; } \ 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..71f5aae1 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,13 @@ #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 a)) { + if (!(begin && end && predicate && begin < end)) + return end; + const int* first = begin; + const int* last = end-1; + for (; last >= first; --last) + if (predicate(*last)) + return last; + 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..ab441ba1 100644 --- a/02_week/tasks/little_big/little_big.cpp +++ b/02_week/tasks/little_big/little_big.cpp @@ -1,10 +1,42 @@ #include +#include +#include +#include +void PrintByte(unsigned char byte) { + std::cout << std::setw(2) << static_cast(byte); +} + +void PrintMemory(int value, bool reverse = false) { + const size_t size = sizeof(int); + unsigned char bytes[size]; + + std::memcpy(bytes, &value, size); + std::cout << "0x" << std::hex << std::uppercase << std::setfill('0'); -void PrintMemory(int /* write arguments here */) { - throw std::runtime_error{"Not implemented"}; + if (reverse) + for (size_t i = size; i > 0; --i) + PrintByte(bytes[i-1]); + else + for (size_t i = 0; i < size; ++i) + PrintByte(bytes[i]); + + std::cout << std::endl; } -void PrintMemory(double /* write arguments here */) { - throw std::runtime_error{"Not implemented"}; +void PrintMemory(double value, bool reverse = false) { + const size_t size = sizeof(double); + unsigned char bytes[size]; + + std::memcpy(bytes, &value, size); + std::cout << "0x" << std::hex << std::uppercase << std::setfill('0'); + + if (reverse) + for (size_t i = size; i > 0; --i) + PrintByte(bytes[i-1]); + else + for (size_t i = 0; i < size; ++i) + PrintByte(bytes[i]); + + std::cout << std::endl; } \ No newline at end of file diff --git a/02_week/tasks/longest/longest.cpp b/02_week/tasks/longest/longest.cpp index 04b3c354..1fd3f4d6 100644 --- a/02_week/tasks/longest/longest.cpp +++ b/02_week/tasks/longest/longest.cpp @@ -1,6 +1,30 @@ #include -/* return_type */ FindLongestSubsequence(/* ptr_type */ begin, /* ptr_type */ end, /* type */ count) { - throw std::runtime_error{"Not implemented"}; +char* FindLongestSubsequence(char* begin, char* end, size_t& count) { + if (!begin || !end || end - begin == 0) { + count = 0; + return nullptr; + } + + size_t curLen = 0; + char* startPtr = nullptr; + char curChar = *begin; + for (; begin <= end; ++begin) { + if (curChar != *begin) { + if (curLen > count) { + count = curLen; + startPtr = begin - curLen; + } + curLen = 1; + curChar = *begin; + } + else + ++curLen; + } + return startPtr; } + +const char* FindLongestSubsequence(const char* begin, const char* end, size_t& count) { + return FindLongestSubsequence(const_cast(begin), const_cast(end), count); +} \ No newline at end of file diff --git a/02_week/tasks/pretty_array/pretty_array.cpp b/02_week/tasks/pretty_array/pretty_array.cpp index 48eab341..18ae55bb 100644 --- a/02_week/tasks/pretty_array/pretty_array.cpp +++ b/02_week/tasks/pretty_array/pretty_array.cpp @@ -1,6 +1,35 @@ #include +#include +void PrintArray(const int* begin, const int* end, size_t limit = 0) { + const bool reverse = (begin > end); + const size_t len = std::abs(end - begin); -void PrintArray(/* write arguments here */) { - throw std::runtime_error{"Not implemented"}; + if (len == 0) { + std::cout << "[]\n"; + return; + } + + const int* cur = begin; + size_t cnt = 0; + + std::cout << "["; + + for (size_t i = 0; i < len; ++i) { + std::cout << *cur; + if (i != len - 1) { + if ((limit > 0) && (cnt == limit - 1)) { + std::cout << ", ...\n "; + cnt = 0; + } + else { + std::cout << ", "; + ++cnt; + } + } + + if (i != len - 1) + cur = reverse ? cur - 1 : cur + 1; + } + std::cout << "]\n"; } \ 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..5515ea45 100644 --- a/02_week/tasks/swap_ptr/swap_ptr.cpp +++ b/02_week/tasks/swap_ptr/swap_ptr.cpp @@ -1,6 +1,20 @@ #include -void SwapPtr(/* write arguments here */) { - throw std::runtime_error{"Not implemented"}; +void SwapPtr(int*& ptr1, int*& ptr2) { + int* temp = ptr1; + ptr1 = ptr2; + ptr2 = temp; +} + +void SwapPtr(const int*& ptr1, const int*& ptr2) { + const int* temp = ptr1; + ptr1 = ptr2; + ptr2 = temp; +} + +void SwapPtr(int**& ptr1, int**& ptr2) { + int** temp = ptr1; + ptr1 = ptr2; + ptr2 = temp; } \ 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..39880346 100644 --- a/03_week/tasks/data_stats/data_stats.cpp +++ b/03_week/tasks/data_stats/data_stats.cpp @@ -1,11 +1,25 @@ #include - +#include struct DataStats { double avg = 0.0; double sd = 0.0; }; -/* return_type */ CalculateDataStats(/* args */) { - throw std::runtime_error{"Not implemented"}; +DataStats CalculateDataStats(std::vector container) { + DataStats result; + + if (container.size() == 0) + return result; + + for (int x : container) + result.avg += static_cast(x); + result.avg /= container.size(); + + for (int x : container) + result.sd += pow(x - result.avg, 2); + result.sd /= container.size(); + result.sd = sqrt(result.sd); + + return result; } diff --git a/03_week/tasks/easy_compare/easy_compare.cpp b/03_week/tasks/easy_compare/easy_compare.cpp index dd5cb7f6..580e3633 100644 --- a/03_week/tasks/easy_compare/easy_compare.cpp +++ b/03_week/tasks/easy_compare/easy_compare.cpp @@ -5,12 +5,70 @@ struct Date { unsigned year; unsigned month; unsigned day; + + Date (unsigned y = 0u, unsigned m = 0u, unsigned d = 0u) : year(y), month(m), day(d) {} }; +bool operator==(const Date& leftDate, const Date& rightDate) { + return std::tie(leftDate.year, leftDate.month, leftDate.day) == std::tie(rightDate.year, rightDate.month, rightDate.day); +} + +bool operator!=(const Date& leftDate, const Date& rightDate) { + return !(leftDate == rightDate); +} + +bool operator<(const Date& leftDate, const Date& rightDate) { + return std::tie(leftDate.year, leftDate.month, leftDate.day) < std::tie(rightDate.year, rightDate.month, rightDate.day); +} + +bool operator>(const Date& leftDate, const Date& rightDate) { + return rightDate < leftDate; +} + +bool operator<=(const Date& leftDate, const Date& rightDate) { + return leftDate < rightDate || leftDate == rightDate; +} + +bool operator>=(const Date& leftDate, const Date& rightDate) { + return leftDate > rightDate || leftDate == rightDate; +} + struct StudentInfo { size_t id; char mark; int score; unsigned course; Date birth_date; -}; \ No newline at end of file +}; + +bool operator==(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return std::tie(leftStudent.mark, leftStudent.score) == std::tie(rightStudent.mark, rightStudent.score); +} + +bool operator!=(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return !(leftStudent == rightStudent); +} + +bool operator<(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + if (leftStudent.mark != rightStudent.mark) + return leftStudent.mark > rightStudent.mark; + if (leftStudent.score != rightStudent.score) + return leftStudent.score < rightStudent.score; + if (leftStudent.course != rightStudent.course) + return leftStudent.course > rightStudent.course; + if (leftStudent.birth_date != rightStudent.birth_date) + return leftStudent.birth_date < rightStudent.birth_date; + return false; +} + +bool operator>(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return rightStudent < leftStudent; +} + +bool operator<=(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return leftStudent < rightStudent || leftStudent == rightStudent; +} + +bool operator>=(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return leftStudent > rightStudent || leftStudent == rightStudent; +} \ No newline at end of file diff --git a/03_week/tasks/enum_operators/enum_operators.cpp b/03_week/tasks/enum_operators/enum_operators.cpp index a539be38..bc3db3fd 100644 --- a/03_week/tasks/enum_operators/enum_operators.cpp +++ b/03_week/tasks/enum_operators/enum_operators.cpp @@ -1,5 +1,7 @@ +#include #include -#include +#include +#include enum class CheckFlags : uint8_t { NONE = 0, @@ -12,22 +14,55 @@ enum class CheckFlags : uint8_t { ALL = TIME | DATE | USER | CERT | KEYS | DEST }; -/* return_type */ operator|(/* args */) { - throw std::runtime_error{"Not implemented"}; +constexpr uint8_t flagAll = static_cast(CheckFlags::ALL); + +constexpr uint8_t toUi(CheckFlags flag) { + return static_cast(flag) & flagAll; +} + +constexpr CheckFlags operator|(CheckFlags lFlag, CheckFlags rFlag) { + return static_cast(toUi(lFlag) | toUi(rFlag)); } -/* return_type */ operator&(/* args */) { - throw std::runtime_error{"Not implemented"}; +constexpr bool operator&(CheckFlags lFlag, CheckFlags rFlag) { + uint8_t lFlagUi = toUi(lFlag); + uint8_t rFlagUi = toUi(rFlag); + + if (lFlagUi == 0 || rFlagUi == 0) + return false; + + return (lFlagUi & rFlagUi) == lFlagUi || (lFlagUi & rFlagUi) == rFlagUi; } -/* return_type */ operator^(/* args */) { - throw std::runtime_error{"Not implemented"}; +constexpr CheckFlags operator^(CheckFlags lFlag, CheckFlags rFlag) { + return static_cast(toUi(lFlag) ^ toUi(rFlag)); } -/* return_type */ operator~(/* args */) { - throw std::runtime_error{"Not implemented"}; +constexpr CheckFlags operator~(CheckFlags flag) { + return static_cast(flagAll & ~toUi(flag)); } -/* return_type */ operator<<(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::ostream& operator<<(std::ostream& os, CheckFlags flag) { + uint8_t num = toUi(flag); + bool first = true; + auto try_print = [&](CheckFlags f, const char* name) { + if (num & toUi(f)) { + if (!first) + os << ", "; + os << name; + first = false; + } + }; + + try_print(CheckFlags::TIME, "TIME"); + try_print(CheckFlags::DATE, "DATE"); + try_print(CheckFlags::USER, "USER"); + try_print(CheckFlags::CERT, "CERT"); + try_print(CheckFlags::KEYS, "KEYS"); + try_print(CheckFlags::DEST, "DEST"); + + if (first) + os << "NONE"; + + return os; } diff --git a/03_week/tasks/filter/filter.cpp b/03_week/tasks/filter/filter.cpp index 6648cb39..269c1cf3 100644 --- a/03_week/tasks/filter/filter.cpp +++ b/03_week/tasks/filter/filter.cpp @@ -1,6 +1,19 @@ +#include +#include #include +#include -/* return_type */ Filter(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::vector& Filter(std::vector& vec, bool (*func)(int)) { + if (func == nullptr) + return vec; + + size_t pos = 0; + size_t len = vec.size(); + for (size_t i = 0; i < len; ++i) + if (func(vec[i])) + vec[pos++] = vec[i]; + + vec.resize(pos); + return vec; } \ 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..4cdd8e6f 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 +#include +std::vector FindAll(const std::vector& vec, bool (*func)(int)) { + if (func == nullptr) + return {}; + + size_t count = 0; + for (size_t i = 0; i < vec.size(); ++i) + if (func(vec[i])) + ++count; + std::vector result(count); -/* return_type */ FindAll(/* args */) { - throw std::runtime_error{"Not implemented"}; + size_t pos = 0; + size_t len = vec.size(); + for (size_t i = 0; i < len; ++i) + if (func(vec[i])) + result[pos++] = i; + + return result; } \ No newline at end of file diff --git a/03_week/tasks/minmax/minmax.cpp b/03_week/tasks/minmax/minmax.cpp index c2869799..d4f8ed79 100644 --- a/03_week/tasks/minmax/minmax.cpp +++ b/03_week/tasks/minmax/minmax.cpp @@ -1,6 +1,20 @@ #include +#include -/* return_type */ MinMax(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::pair::const_iterator, std::vector::const_iterator> MinMax(const std::vector& vec) { + if (vec.empty()) + return {vec.end(), vec.end()}; + + auto minEl = vec.begin(); + auto maxEl = vec.begin(); + + for (auto x = vec.begin(); x != vec.end(); ++x) { + if (*x < *minEl) + minEl = x; + if (*x >= *maxEl) + maxEl = x; + } + + return {minEl, maxEl}; } diff --git a/03_week/tasks/os_overload/os_overload.cpp b/03_week/tasks/os_overload/os_overload.cpp index e473418d..7f14d0fa 100644 --- a/03_week/tasks/os_overload/os_overload.cpp +++ b/03_week/tasks/os_overload/os_overload.cpp @@ -1,21 +1,54 @@ +#include #include #include #include struct Coord2D { - int x; - int y; + int x = 0; + int y = 0; }; struct Circle { Coord2D coord; - unsigned radius; + unsigned radius = 1; }; using CircleRegion = std::pair; using CircleRegionList = std::vector; -/* return_type */ operator<<(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::ostream& operator<<(std::ostream& os, const Coord2D& coord) { + os << '(' << coord.x << ", " << coord.y << ')'; + return os; } + +std::ostream& operator<<(std::ostream& os, const Circle& circle) { + os << "circle["; + if (circle.radius != 0) + os << circle.coord << ", r = " << circle.radius; + os << ']'; + return os; +} + +std::ostream& operator<<(std::ostream& os, const CircleRegion& circle) { + os << ((circle.second) ? '+' : '-') << circle.first; + return os; +} + +std::ostream& operator<<(std::ostream& os, const CircleRegionList& list) { + os << '{'; + size_t len = list.size(); + for (size_t i = 0; i < len; ++i) { + if (i == 0) + os << "\n\t"; + + os << list[i]; + + if (i != len-1) + os << ",\n\t"; + else + os << '\n'; + } + os << '}'; + 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..f4976670 100644 --- a/03_week/tasks/range/range.cpp +++ b/03_week/tasks/range/range.cpp @@ -2,6 +2,19 @@ #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) { + if ((from <= to && step < 0) || (from >= to && step > 0) || step == 0) + return {}; + + int cnt = (to - from) / step; + if ((to - from) % step != 0) + ++cnt; + + std::vector result; + result.reserve(cnt); + + for (int el = from; (step > 0 ? el < to : el > to); el += step) + result.push_back(el); + + return result; } diff --git a/03_week/tasks/unique/unique.cpp b/03_week/tasks/unique/unique.cpp index 9d2545bb..ab2d94cb 100644 --- a/03_week/tasks/unique/unique.cpp +++ b/03_week/tasks/unique/unique.cpp @@ -1,6 +1,23 @@ #include #include -/* return_type */ Unique(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::vector Unique(const std::vector& vec) { + if (vec.size() == 0) + return {}; + + size_t len = vec.size(); + size_t cnt = 1; + for (size_t i = 1; i < len; ++i) + if (vec[i] != vec[i - 1]) + ++cnt; + + std::vector result; + result.reserve(cnt); + + result.push_back(vec[0]); + for (size_t i = 1; i < len; ++i) + if (vec[i] != vec[i - 1]) + result.push_back(vec[i]); + + return result; } diff --git a/04_week/tasks/phasor/phasor.cpp b/04_week/tasks/phasor/phasor.cpp index 3ec1b9ad..083050b8 100644 --- a/04_week/tasks/phasor/phasor.cpp +++ b/04_week/tasks/phasor/phasor.cpp @@ -1,4 +1,5 @@ - +#include +#include struct ExpTag {}; struct DegTag {}; @@ -6,5 +7,284 @@ struct AlgTag {}; class Phasor { +public: + // Конструкторы (объявления) + Phasor(); + Phasor(double amplitude, double phase); + Phasor(double amplitude, double phase, ExpTag); + Phasor(double amplitude, double phase, DegTag); + Phasor(double real, double imag, AlgTag); + + // Методы (объявления) + void SetPolar(double amplitude, double phase); + void SetCartesian(double real, double imag); + double Magnitude() const; + double Abs() const; + double Phase() const; + double Angle() const; + double PhaseDeg() const; + double AngleDeg() const; + double Real() const; + double Imag() const; + Phasor Conj() const; + Phasor Inv() const; + + // Операторы (объявления) + Phasor operator-() const; + Phasor& operator+=(const Phasor& other); + Phasor& operator-=(const Phasor& other); + Phasor& operator*=(const Phasor& other); + Phasor& operator/=(const Phasor& other); + Phasor& operator+=(double val); + Phasor& operator-=(double val); + Phasor& operator*=(double val); + Phasor& operator/=(double val); + bool operator==(const Phasor& other) const; + bool operator!=(const Phasor& other) const; + +private: + double re; + double im; + static constexpr double PI = 3.14159265358979323846; }; + +// Арифметические операторы (объявления) +Phasor operator+(Phasor a, const Phasor& b); +Phasor operator-(Phasor a, const Phasor& b); +Phasor operator*(Phasor a, const Phasor& b); +Phasor operator/(Phasor a, const Phasor& b); + +Phasor operator+(Phasor a, double b); +Phasor operator+(double a, const Phasor& b); +Phasor operator-(Phasor a, double b); +Phasor operator-(double a, const Phasor& b); +Phasor operator*(Phasor a, double b); +Phasor operator*(double a, const Phasor& b); +Phasor operator/(Phasor a, double b); +Phasor operator/(double a, const Phasor& b); + +// Методы создания (объявления) +Phasor MakePhasorCartesian(double re, double im); +Phasor MakePhasorPolar(double amplitude, double phase); +Phasor MakePhasorPolarDeg(double amplitude, double phase); + +// // Вывод в поток (объявление) +std::ostream& operator<<(std::ostream& os, const Phasor& p); + + +// Конструкторы (реализации) + +Phasor::Phasor() : re(0.0), im(0.0) {} + +Phasor::Phasor(double amplitude, double phase) { + SetPolar(amplitude, phase); +} + +Phasor::Phasor(double amplitude, double phase, ExpTag) : Phasor(amplitude, phase) {} + +Phasor::Phasor(double amplitude, double phase, DegTag) { + SetPolar(amplitude, phase * PI / 180.0); +} + +Phasor::Phasor(double real, double imag, AlgTag) : re(real), im(imag) {} + + +// Методы (реализации) + +void Phasor::SetPolar(double amplitude, double phase) { + re = amplitude * std::cos(phase); + im = amplitude * std::sin(phase); +} + +void Phasor::SetCartesian(double real, double imag) { + re = real; + im = imag; +} + +double Phasor::Magnitude() const { + return std::hypot(re, im); +} + +double Phasor::Abs() const { + return Magnitude(); +} + +double Phasor::Phase() const { + double p = std::atan2(im, re); + return (p <= -PI + 1e-20) ? PI : p; +} + +double Phasor::Angle() const { + return Phase(); +} + +double Phasor::PhaseDeg() const { + return Phase() * 180.0 / PI; +} + +double Phasor::AngleDeg() const { + return PhaseDeg(); +} + +double Phasor::Real() const { + return re; +} + +double Phasor::Imag() const { + return im; +} + +Phasor Phasor::Conj() const { + return Phasor(re, -im, AlgTag()); +} + +Phasor Phasor::Inv() const { + double denom = re * re + im * im; + return Phasor(re / denom, -im / denom, AlgTag()); +} + + +// Операторы (реализации) + +Phasor Phasor::operator-() const { + return Phasor(-re, -im, AlgTag()); +} + +Phasor& Phasor::operator+=(const Phasor& other) { + re += other.re; + im += other.im; + return *this; +} + +Phasor& Phasor::operator-=(const Phasor& other) { + re -= other.re; + im -= other.im; + return *this; +} + +Phasor& Phasor::operator*=(const Phasor& other) { + double old_re = re; + re = old_re * other.re - im * other.im; + im = old_re * other.im + im * other.re; + return *this; +} + +Phasor& Phasor::operator/=(const Phasor& other) { + double denom = other.re * other.re + other.im * other.im; + double old_re = re; + re = (old_re * other.re + im * other.im) / denom; + im = (im * other.re - old_re * other.im) / denom; + return *this; +} + +Phasor& Phasor::operator+=(double val) { + re += val; + return *this; +} + +Phasor& Phasor::operator-=(double val) { + re -= val; + return *this; +} + +Phasor& Phasor::operator*=(double val) { + re *= val; + im *= val; + return *this; +} + +Phasor& Phasor::operator/=(double val) { + re /= val; + im /= val; + return *this; +} + +bool Phasor::operator==(const Phasor& other) const { + return this->re == other.re && this->im == other.im; +} + +bool Phasor::operator!=(const Phasor& other) const { + return !(*this == other); +} + + +// Арифметические операторы (реализации) + +Phasor operator+(Phasor a, const Phasor& b) { + return a += b; +} + +Phasor operator-(Phasor a, const Phasor& b) { + return a -= b; +} + +Phasor operator*(Phasor a, const Phasor& b) { + return a *= b; +} + +Phasor operator/(Phasor a, const Phasor& b) { + return a /= b; +} + +Phasor operator+(Phasor a, double b) { + return a += b; +} + +Phasor operator+(double a, const Phasor& b) { + return Phasor(a, 0, AlgTag()) + b; +} + +Phasor operator-(Phasor a, double b) { + return a -= b; +} + +Phasor operator-(double a, const Phasor& b) { + return Phasor(a, 0, AlgTag()) - b; +} + +Phasor operator*(Phasor a, double b) { + return a *= b; +} + +Phasor operator*(double a, const Phasor& b) { + return Phasor(a, 0, AlgTag()) * b; +} + +Phasor operator/(Phasor a, double b) { + return a /= b; +} + +Phasor operator/(double a, const Phasor& b) { + return Phasor(a, 0, AlgTag()) / b; +} + + +// Методы создания (реализация) + +Phasor MakePhasorCartesian(double re, double im) { + return Phasor(re, im, AlgTag()); +} + +Phasor MakePhasorPolar(double amplitude, double phase) { + return Phasor(amplitude, phase, ExpTag()); +} + +Phasor MakePhasorPolarDeg(double amplitude, double phase) { + return Phasor(amplitude, phase, DegTag()); +} + + +// Вывод в поток (реализация) + +std::ostream& operator<<(std::ostream& os, const Phasor& p) { + std::streamsize default_precision = os.precision(); + std::ios_base::fmtflags default_flags = os.flags(); + + os << std::fixed << std::setprecision(3); + os << p.Magnitude() << "*e(j*" << p.PhaseDeg() << ") [" << p.Real() << " + j*" << p.Imag() << "]"; + + os.precision(default_precision); + os.flags(default_flags); + return os; +} \ No newline at end of file diff --git a/04_week/tasks/queue/queue.cpp b/04_week/tasks/queue/queue.cpp index 2a9f8493..8352746c 100644 --- a/04_week/tasks/queue/queue.cpp +++ b/04_week/tasks/queue/queue.cpp @@ -1,6 +1,143 @@ +#include +#include #include +#include class Queue { +public: + // Конструкторы (объявления) + Queue(); + Queue(std::stack st); + Queue(std::vector vec); + Queue(std::initializer_list list); + Queue(int n); + // Методы (объявления) + size_t Size() const; + bool Empty() const; + void Push(int val); + bool Pop(); + int& Front(); + const int& Front() const; + int& Back(); + const int& Back() const; + void Clear(); + void Swap(Queue& other); + + // Операторы (объявления) + bool operator==(const Queue& other) const; + bool operator!=(const Queue& other) const; + +private: + mutable std::vector in; + mutable std::vector out; + + // Подготовка стека на вывод + void prepareOut() const { + if (out.empty()) { + while (!in.empty()) { + out.push_back(in.back()); + in.pop_back(); + } + } + } }; + + +// Конструкторы (реализации) + +Queue::Queue() {}; +Queue::Queue(std::stack st) { + while (!st.empty()) { + this->Push(st.top()); + st.pop(); + } + std::reverse(in.begin(), in.end()); +} +Queue::Queue(std::vector vec) : in(vec) {} +Queue::Queue(std::initializer_list list) : in(list) {} +Queue::Queue(int n) { + in.reserve(n); + out.reserve(n); +} + +//Методы (реализации) + +size_t Queue::Size() const { + return in.size() + out.size(); +} + +bool Queue::Empty() const { + return in.empty() && out.empty(); +} + +void Queue::Push(int val) { + in.push_back(val); +} + +bool Queue::Pop() { + prepareOut(); + if (out.empty()) + return false; + out.pop_back(); + return true; +} + +int& Queue::Front() { + prepareOut(); + return out.back(); +} + +const int& Queue::Front() const { + prepareOut(); + return out.back(); +} + +int& Queue::Back() { + if (!in.empty()) + return in.back(); + return out.front(); +} + +const int& Queue::Back() const { + if (!in.empty()) + return in.back(); + return out.front(); +} + +void Queue::Clear() { + if (!in.empty()) + in.clear(); + if (!out.empty()) + out.clear(); +} + +void Queue::Swap(Queue& other) { + std::swap(in, other.in); + std::swap(out, other.out); +} + +// Операторы (реализации) + +bool Queue::operator==(const Queue& other) const { + if ((this->in.size() + this->out.size()) != (other.in.size() + other.out.size())) + return false; + + auto getVal = [](const Queue& q, size_t index) { + if (index < q.out.size()) + return q.out[q.out.size() - 1 - index]; + return q.in[index - q.out.size()]; + }; + + size_t total_size = this->in.size() + this->out.size(); + for (size_t i = 0; i < total_size; ++i) + if (getVal(*this, i) != getVal(other, i)) + return false; + + return true; +} + +bool Queue::operator!=(const Queue& other) const { + return !(*this == other); +} \ No newline at end of file diff --git a/04_week/tasks/ring_buffer/ring_buffer.cpp b/04_week/tasks/ring_buffer/ring_buffer.cpp index e2b57ba2..8a6274aa 100644 --- a/04_week/tasks/ring_buffer/ring_buffer.cpp +++ b/04_week/tasks/ring_buffer/ring_buffer.cpp @@ -1,6 +1,157 @@ -#include - + #include +#include +#include class RingBuffer { +public: + // Конструкторы (объявления) + RingBuffer(size_t n); + RingBuffer(size_t n, int val); + RingBuffer(std::initializer_list list); + + // Методы (объявления) + bool Empty() const; + bool Full() const; + size_t Size() const; + size_t Capacity() const; + void Push(int val); + bool TryPush(int val); + void Pop(); + bool TryPop(int& val); + int& Front(); + const int& Front() const; + int& Back(); + const int& Back() const; + void Clear(); + void Resize(size_t n); + std::vector Vector() const; + // Операторы (объявсления) + int& operator[](size_t ind); + const int& operator[](size_t ind) const; + +private: + std::vector buf; + size_t begin = 0; + size_t curSize = 0; }; + +// Конструкторы (реализации) + +RingBuffer::RingBuffer(size_t n) : buf(std::max(1, n)), begin(0), curSize(0) {} + +RingBuffer::RingBuffer(size_t n, int val) : buf(std::max(1, n), val) { + curSize = buf.size(); + begin = 0; +} + +RingBuffer::RingBuffer(std::initializer_list list) : buf(list), begin(0), curSize(list.size()) { + if (buf.empty()) + buf.resize(1); +} + +// Методы (реализации) + +size_t RingBuffer::Size() const { + return curSize; +} + +size_t RingBuffer::Capacity() const { + return buf.size(); +} + +bool RingBuffer::Empty() const { + return curSize == 0; +} + +bool RingBuffer::Full() const { + return curSize == buf.size(); +} + +void RingBuffer::Push(int val) { + if (Full()) { + buf[begin] = val; + begin = (begin + 1) % buf.size(); + } else { + buf[(begin + curSize) % buf.size()] = val; + curSize++; + } +} + +bool RingBuffer::TryPush(int val) { + if (Full()) + return false; + Push(val); + return true; +} + +void RingBuffer::Pop() { + if (!Empty()) { + begin = (begin + 1) % buf.size(); + curSize--; + } +} + +bool RingBuffer::TryPop(int& val) { + if (Empty()) + return false; + val = Back(); + Pop(); + return true; +} + +int& RingBuffer::Front() { + return buf[(begin + curSize - 1) % buf.size()]; +} + +const int& RingBuffer::Front() const { + return buf[(begin + curSize - 1) % buf.size()]; +} + +int& RingBuffer::Back() { + return buf[begin]; +} + +const int& RingBuffer::Back() const { + return buf[begin]; +} + +void RingBuffer::Clear() { + begin = 0; + curSize = 0; +} + +void RingBuffer::Resize(size_t n) { + if (n == 0) + n = 1; + + std::vector newBuf(n); + size_t cntCopy = std::min(curSize, n); + size_t start = curSize > n ? (curSize - n) : 0; + + for (size_t i = 0; i < cntCopy; ++i) + newBuf[i] = (*this)[start + i]; + + buf = std::move(newBuf); + begin = 0; + curSize = cntCopy; +} + +std::vector RingBuffer::Vector() const { + std::vector res(curSize); + + for (size_t i = 0; i < curSize; ++i) + res[i] = (*this)[i]; + + return res; +} + +// Операторы (реализации) + +int& RingBuffer::operator[](size_t ind) { + return buf[(begin + ind) % buf.size()]; +} + +const int& RingBuffer::operator[](size_t ind) const { + return buf[(begin + ind) % buf.size()]; +} \ No newline at end of file diff --git a/04_week/tasks/stack/stack.cpp b/04_week/tasks/stack/stack.cpp index 222e4ffc..9687e02c 100644 --- a/04_week/tasks/stack/stack.cpp +++ b/04_week/tasks/stack/stack.cpp @@ -1,6 +1,98 @@ +#include +#include #include +#include class Stack { +public: + // Конструкторы (объявления) + Stack(); + Stack(const Stack& other); + // Методы (объявления) + size_t Size() const; + bool Empty() const; + void Push(int val); + bool Pop(); + int& Top(); + const int& Top() const; + void Clear(); + void Swap(Stack& other); + + // Операторы (объявления) + Stack& operator=(const Stack& other); + bool operator==(const Stack& other) const; + bool operator!=(const Stack& other) const; + +private: + std::vector st; }; + + +// Конструкторы (реализации) + +Stack::Stack() {}; +Stack::Stack(const Stack& other) : st(other.st) {} + +//Методы (реализации) + +size_t Stack::Size() const { + return st.size(); +} + +bool Stack::Empty() const { + return st.empty(); +} + +void Stack::Push(int val) { + st.push_back(val); +} + +bool Stack::Pop() { + if (st.empty()) + return false; + st.pop_back(); + return true; +} + +int& Stack::Top() { + return st.back(); +} + +const int& Stack::Top() const { + return st.back(); +} + +void Stack::Clear() { + if (!st.empty()) + st.clear(); +} + +void Stack::Swap(Stack& other) { + std::swap(st, other.st); +} + +// Операторы (реализации) + +Stack& Stack::operator=(const Stack& other) { + if (this == &other) + return *this; + this->st = other.st; + return *this; +} + +bool Stack::operator==(const Stack& other) const { + if (this->st.size() != other.st.size()) + return false; + + for (size_t i = 0; i < this->Size(); ++i) + if (this->st[i] != other.st[i]) + return false; + + return true; +} + +bool Stack::operator!=(const Stack& other) const { + return !(*this == other); +} \ No newline at end of file diff --git a/main.cpp b/main.cpp index 9dad8356..0274e011 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,32 @@ +#include +#include +#include #include +std::vector Range(int from, int to, int step = 1) { + if ((from <= to && step < 0) || (from >= to && step > 0) || step == 0) + return {}; -int main() { - std::cout << "I wait your unbelievable code here" << std::endl; - return 0; + int count = (to - from) / step; + if ((to - from) % step != 0) + ++count; + + std::vector result; + result.reserve(count); + + for (int el = from; (step > 0 ? el < to : el > to); el += step) + result.push_back(el); + + return result; } + + +int main(void) { + std::vector res = Range(20, 20, 3); + + for (size_t i = 0; i < res.size(); ++i) { + std::cout << res[i] << ' '; + } + std::cout << '\n'; + return 0; +} \ No newline at end of file