From 52ca81625371431d729ad1063aca05f9a8f02826 Mon Sep 17 00:00:00 2001 From: nihonium Date: Wed, 2 Nov 2022 22:41:16 +0300 Subject: [PATCH] half of 4th seminar --- seminar04_templates/01_sum_even/main.cpp | 18 ++++ seminar04_templates/02_last_digits/main.cpp | 58 +++++++++++++ seminar04_templates/03_factorization/main.cpp | 74 ++++++++++++++++ seminar04_templates/04_time/main.cpp | 27 ++++++ seminar04_templates/04_time/time.cpp | 81 ++++++++++++++++++ seminar04_templates/04_time/time.h | 21 +++++ seminar04_templates/homework_templates.pdf | Bin 0 -> 130512 bytes seminar04_templates/pairing_test.cpp | 55 ++++++++++++ 8 files changed, 334 insertions(+) create mode 100644 seminar04_templates/01_sum_even/main.cpp create mode 100644 seminar04_templates/02_last_digits/main.cpp create mode 100644 seminar04_templates/03_factorization/main.cpp create mode 100644 seminar04_templates/04_time/main.cpp create mode 100644 seminar04_templates/04_time/time.cpp create mode 100644 seminar04_templates/04_time/time.h create mode 100644 seminar04_templates/homework_templates.pdf create mode 100644 seminar04_templates/pairing_test.cpp diff --git a/seminar04_templates/01_sum_even/main.cpp b/seminar04_templates/01_sum_even/main.cpp new file mode 100644 index 0000000..badaefd --- /dev/null +++ b/seminar04_templates/01_sum_even/main.cpp @@ -0,0 +1,18 @@ +#include +#include + +using std::cout, std::endl; + +int sumEven(const std::vector& v) { + int sum = 0; + size_t s = v.size(); + for (int i = 0; i < s; ++i) + if (v[i] % 2 == 0) + sum += v[i]; + return sum; +} + +int main() { + std::vector v {4, 8, 15, 16, 23, 42}; + cout << sumEven(v) << endl; +} diff --git a/seminar04_templates/02_last_digits/main.cpp b/seminar04_templates/02_last_digits/main.cpp new file mode 100644 index 0000000..87cd622 --- /dev/null +++ b/seminar04_templates/02_last_digits/main.cpp @@ -0,0 +1,58 @@ +#include +#include +#include + +using std::cout, std::endl, std::vector, std::span; + +vector lastDigits1(const vector& v) { + vector result; + size_t size = v.size(); + for(int i = 0; i < size; ++i) { + result.push_back(v[i] % 10); + } + return result; +} +void lastDigits2(vector& v) { + size_t size = v.size(); + for(int i = 0; i < size; ++i) { + v[i] %= 10; + } +} + +void lastDigits3(vector* pv) { + size_t size = pv->size(); + for(int i = 0; i < size; ++i) + (*pv)[i] %= 10; +} + +void lastDigits4(span sp) { + size_t size = sp.size(); + for(int i = 0; i < size; ++i) + sp[i] %= 10; +} + +std::ostream& operator<<(std::ostream& out, const vector& v) { + cout << "["; + size_t size = v.size(); + for(int i = 0; i < size; ++i) + cout << v[i] << (i == size - 1 ? "]" : " "); + return out; +} + +int main() { + vector meow{1, 2, 3, 12, 45, 32,313123,3245}; + vector result1 = lastDigits1(meow); + cout << result1 << endl; + + vector result2 = meow; + lastDigits2(result2); + cout << result2 << endl; + + vector result3 = meow; + lastDigits3(&result3); + cout << result3 << endl; + + span sp = meow; + lastDigits4(sp); + cout << meow << endl; +} diff --git a/seminar04_templates/03_factorization/main.cpp b/seminar04_templates/03_factorization/main.cpp new file mode 100644 index 0000000..fde4487 --- /dev/null +++ b/seminar04_templates/03_factorization/main.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +using std::cout, std::endl, std::pair, std::vector; + +int next_prime(int n); + +vector> factorization(int n) { + if (n == 1) { + return vector>{{1, 1}}; + } + int d = 2; + int c; + vector> result; + + while(n != 1) { + c = 0; + while(n % d == 0) { + c++; + n /= d; + } + if (c) + result.push_back(pair{d, c}); + d = next_prime(d); + } + return result; +} + +bool is_prime(int n) { + bool prime = true; + for(int i = 2; i <= static_cast(std::sqrt(n)); i++) { + if (n % i == 0) { + prime = false; + break; + } + } + return prime; +} + +int next_prime(int n) { + ++n; + while (true) { + if (is_prime(n)) + return n; + ++n; + } +} + +std::ostream& operator<<(std::ostream& out, pair p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} + +std::ostream& operator<<(std::ostream& out, vector> v) { + out << "{"; + size_t size = v.size(); + for(int i = 0; i < size; ++i) { + out << v[i] << (i == size - 1 ? "}" : ", "); + } + return out; +} + +int main() { + vector> res = factorization(60); + cout << res << endl; + res = factorization(626215995); + cout << res << endl; + res = factorization(107); + cout << res << endl; + res = factorization(1); + cout << res << endl; +} diff --git a/seminar04_templates/04_time/main.cpp b/seminar04_templates/04_time/main.cpp new file mode 100644 index 0000000..6f99c57 --- /dev/null +++ b/seminar04_templates/04_time/main.cpp @@ -0,0 +1,27 @@ +#include +#include "time.h" +#include +#include +#include + +using std::cout, std::endl, std::string, std::string_view, std::vector; + +int main() +{ + string t1s = "23:59:59"; + string_view t1sv = t1s; + string t2s = "23:59:59"; + string_view t2sv = t2s; + + Time t1 = t1sv; + Time t2 = t2sv; + cout << t1 + t2 << endl; + + string tss = "23:59:59 23:59:59 23:59:59"; + string_view tssv = tss; + vector < Time > tsv = getTimesFromString(tss); + for (int i = 0, size = tsv.size(); i < size; ++i) { + cout << tsv[i] << endl; + } + cout << sumTimes(tsv) << endl; +} diff --git a/seminar04_templates/04_time/time.cpp b/seminar04_templates/04_time/time.cpp new file mode 100644 index 0000000..afc7c1d --- /dev/null +++ b/seminar04_templates/04_time/time.cpp @@ -0,0 +1,81 @@ +#include "time.h" +#include +#include +#include + +using std::cout, std::endl, std::vector, std::string_view, std::string; + +Time::Time(int hours, int minutes, int seconds) +{ + mHours = hours; + mMinutes = minutes; + mSeconds = seconds; +} + +Time::Time(string_view s) +{ + string buf; + buf[0] = s[0]; + buf[1] = s[1]; + mHours = stoi(buf); + buf[0] = s[3]; + buf[1] = s[4]; + mMinutes = stoi(buf); + buf[0] = s[6]; + buf[1] = s[7]; + mSeconds = stoi(buf); +} + +Time::Time():Time(0, 0, 0) +{ +}; + +Time Time::operator+(Time b) const +{ + return Time((mHours + b.mHours + (mMinutes + b.mMinutes) / 60) % 24, + (mMinutes + b.mMinutes + + (mSeconds + b.mSeconds) / 60) % 60, + (mSeconds + b.mSeconds) % 60); +} + +int Time::hours() const +{ + return mHours; +} + +int Time::minutes() const +{ + return mMinutes; +} + +int Time::seconds() const +{ + return mSeconds; +} + +vector < Time > getTimesFromString(string_view s) +{ + vector < Time > res; + for (int i = 0, size = s.size(); i < size; i += 9) { + res.push_back(Time(s.substr(i, 9))); + } + return res; +} + +Time sumTimes(vector < Time > &v) +{ + Time res; + for (int i = 0, size = v.size(); i < size; ++i) { + res = res + v[i]; + } + return res; +} + +std::ostream & operator<<(std::ostream & out, Time t) +{ + out << std::setw(2) << std::setfill('0') << t.mHours + << ":" + << std::setw(2) << std::setfill('0') << t.mMinutes + << ":" << std::setw(2) << std::setfill('0') << t.mSeconds; + return out; +} diff --git a/seminar04_templates/04_time/time.h b/seminar04_templates/04_time/time.h new file mode 100644 index 0000000..68a4d92 --- /dev/null +++ b/seminar04_templates/04_time/time.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include +#include + +using std::string_view, std::vector; + +class Time { +private: + int mHours = 0, mMinutes = 0, mSeconds = 0; +public: + Time(int hours, int minutes, int seconds); + Time(); + Time(string_view s); + Time operator+(Time b) const; + int hours() const; int minutes() const; int seconds() const; + friend std::ostream& operator<<(std::ostream& out, Time t); +}; + +Time sumTimes(vector