From a756ccb5a4d22ccb25f0e35bb13b72cfed40d00c Mon Sep 17 00:00:00 2001 From: Tibor Bizjak Date: Thu, 27 Jul 2023 10:23:42 +0200 Subject: [PATCH] chapter 05 exercise solution --- udemy-cpp/exc05/main.cc | 81 +++++++++++++++++++++++++++ udemy-cpp/exc05/utils.h | 119 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 200 insertions(+) create mode 100644 udemy-cpp/exc05/main.cc create mode 100644 udemy-cpp/exc05/utils.h diff --git a/udemy-cpp/exc05/main.cc b/udemy-cpp/exc05/main.cc new file mode 100644 index 0000000..c8b2b85 --- /dev/null +++ b/udemy-cpp/exc05/main.cc @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "utils.h" + +namespace fs = std::filesystem; + +using WordVector = std::vector; +using WordCountPair = std::pair; +using CountedWordsMap = std::map; + +WordVector split_text(std::string_view text, const std::set &dels); +CountedWordsMap count_words(const WordVector &words); + +int main() +{ + auto current_path = fs::current_path(); + current_path /= "text.txt"; + + /* get text */ + auto text = readFile(current_path.string()); + std::cout << "---------- text\n"; + std::cout << text; + + /* make text lowercase */ + std::transform(text.begin(), text.end(), + text.begin(), ::tolower); + + /* split text */ + std::cout << "\n---------- splitted text\n"; + auto splitted_text = split_text(text, {' ', '\n', '.'}); + print_vector(splitted_text); + + /* count word occurences */ + std::cout << "\n---------- word count\n"; + auto counted_words = count_words(splitted_text); + print_map(counted_words); + + return 0; +} + +CountedWordsMap count_words(const WordVector &words) +{ + auto result = CountedWordsMap{}; + for (auto word : words) { + ++result[word]; + } + return result; +} + + +#define AVG_W_LEN 4 +WordVector split_text(std::string_view text, const std::set &dels) +{ + size_t start, count; + start = count = 0; + auto result = WordVector{}; + /* pre allocate space */ + result.reserve(text.length() / AVG_W_LEN); + + for (auto c : text) { + if (dels.find(c) != dels.end()) { + if (count) + result.push_back(static_cast(text.substr(start, count))); + start += count + 1; + count = 0; + } else + ++count; + } + result.shrink_to_fit(); + return result; +} + + diff --git a/udemy-cpp/exc05/utils.h b/udemy-cpp/exc05/utils.h new file mode 100644 index 0000000..d11a349 --- /dev/null +++ b/udemy-cpp/exc05/utils.h @@ -0,0 +1,119 @@ +#ifndef UTILS_H +#define UTILS_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +constexpr double pi = 3.14159265358979311600; + +template +void print_array(const T *array, const std::size_t length) +{ + for (std::size_t i = 0; i < length - 1; i++) + { + std::cout << array[i] << ", "; + } + + std::cout << array[length - 1] << '\n'; +} + +template +void print_array(const std::array array) +{ + for (std::size_t i = 0; i < array.size() - 1; i++) + { + std::cout << array[i] << ", "; + } + + std::cout << array[array.size() - 1] << '\n'; +} + +template +void print_vector(const std::vector &vector) +{ + for (std::size_t i = 0; i < vector.size() - 1; i++) + { + std::cout << vector[i] << ", "; + } + if (vector.size()) + std::cout << vector[vector.size() - 1] << '\n'; +} + +template <> +void print_vector( + const std::vector> &vector) +{ + for (std::size_t i = 0; i < vector.size() - 1; i++) + { + std::cout << vector[i].first << ": " << vector[i].second << ", "; + } + + std::cout << vector[vector.size() - 1].first << ": " + << vector[vector.size() - 1].second << '\n'; +} + +template +void print_map(const std::map Map) +{ + for (const auto &[Key, value] : Map) + { + std::cout << Key << ": " << value << '\n'; + } +} + +std::string readFile(std::string_view file_path) +{ + auto str = std::string{}; + auto text = std::string{}; + + auto iffile = std::ifstream{}; + iffile.open(file_path.data()); + + if (iffile.is_open()) + { + while (std::getline(iffile, str)) + { + text += str + '\n'; + } + } + + iffile.close(); + + return text; +} + + +template +void random_vector(std::vector &vec) +{ + std::mt19937 random_generator(22); + std::uniform_int_distribution random_distribution(-10, 10); + + for (auto &val : vec) + { + val = random_distribution(random_generator); + } +} + +void clear_console() +{ +#if defined _WIN32 + system("cls"); +#elif defined(__LINUX__) || defined(__gnu_linux__) || defined(__linux__) + system("clear"); +#elif defined(__APPLE__) + system("clear"); +#else + system("clear"); +#endif +} + +#endif /* UTILS_H */