half of 4th seminar

master
nihonium 2 years ago
parent ca1a5e91b4
commit 52ca816253
No known key found for this signature in database
GPG Key ID: 0251623741027CFC

@ -0,0 +1,18 @@
#include <iostream>
#include <vector>
using std::cout, std::endl;
int sumEven(const std::vector<int>& 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<int> v {4, 8, 15, 16, 23, 42};
cout << sumEven(v) << endl;
}

@ -0,0 +1,58 @@
#include <iostream>
#include <vector>
#include <span>
using std::cout, std::endl, std::vector, std::span;
vector<int> lastDigits1(const vector<int>& v) {
vector<int> result;
size_t size = v.size();
for(int i = 0; i < size; ++i) {
result.push_back(v[i] % 10);
}
return result;
}
void lastDigits2(vector<int>& v) {
size_t size = v.size();
for(int i = 0; i < size; ++i) {
v[i] %= 10;
}
}
void lastDigits3(vector<int>* pv) {
size_t size = pv->size();
for(int i = 0; i < size; ++i)
(*pv)[i] %= 10;
}
void lastDigits4(span<int> sp) {
size_t size = sp.size();
for(int i = 0; i < size; ++i)
sp[i] %= 10;
}
std::ostream& operator<<(std::ostream& out, const vector<int>& 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<int> meow{1, 2, 3, 12, 45, 32,313123,3245};
vector<int> result1 = lastDigits1(meow);
cout << result1 << endl;
vector<int> result2 = meow;
lastDigits2(result2);
cout << result2 << endl;
vector<int> result3 = meow;
lastDigits3(&result3);
cout << result3 << endl;
span<int> sp = meow;
lastDigits4(sp);
cout << meow << endl;
}

@ -0,0 +1,74 @@
#include <iostream>
#include <utility>
#include <vector>
#include <cmath>
using std::cout, std::endl, std::pair, std::vector;
int next_prime(int n);
vector<pair<int, int>> factorization(int n) {
if (n == 1) {
return vector<pair<int,int>>{{1, 1}};
}
int d = 2;
int c;
vector<pair<int, int>> 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<int>(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<int,int> p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
std::ostream& operator<<(std::ostream& out, vector<pair<int,int>> 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<pair<int, int>> res = factorization(60);
cout << res << endl;
res = factorization(626215995);
cout << res << endl;
res = factorization(107);
cout << res << endl;
res = factorization(1);
cout << res << endl;
}

@ -0,0 +1,27 @@
#include <iostream>
#include "time.h"
#include <string_view>
#include <string>
#include <vector>
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;
}

@ -0,0 +1,81 @@
#include "time.h"
#include <string>
#include <iomanip>
#include <vector>
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;
}

@ -0,0 +1,21 @@
#pragma once
#include <string_view>
#include <iostream>
#include <vector>
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<Time>& v);
vector<Time> getTimesFromString(string_view s);

@ -0,0 +1,55 @@
#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <utility>
using std::cout, std::endl;
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& v)
{
out << "[";
for (size_t i = 0; i < v.size() - 1; ++i)
out << v[i] << ", ";
if (!v.empty())
{
out << v.back();
}
out << "]";
return out;
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& out, const std::pair<T, U>& p)
{
out << "(" << p.first << ", " << p.second << ")";
return out;
}
// Нужно написать функцию pairing
int main()
{
std::vector v {10, 20, 30, 40, 50, 60};
cout << pairing(v) << endl;
std::array<std::string, 7> a {"cat", "dog", "mouse", "elephant", "tiget", "axolotl", "wolf"};
cout << pairing(a) << endl;
std::string s {"Cats and dogs!"};
cout << pairing(s) << endl;
}
/*
Программа должна напечатать:
[(10, 20), (30, 40), (50, 60)]
[(cat, dog), (mouse, elephant), (tiget, axolotl), (wolf, )]
[(C, a), (t, s), ( , a), (n, d), ( , d), (o, g), (s, !)]
*/