From 2c9056a23e1a55fd21a8e314c903d9325bffd62e Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Sat, 6 Sep 2025 09:37:14 +0200 Subject: Move CPP code here Signed-off-by: Carlos Maiolino --- CPP/cpp_book/chap5/split_str/split_str | Bin 0 -> 86760 bytes CPP/cpp_book/chap5/split_str/split_str.cpp | 147 +++++++++++++++++++++++++++++ CPP/cpp_book/chap5/split_str/text.txt | 1 + 3 files changed, 148 insertions(+) create mode 100755 CPP/cpp_book/chap5/split_str/split_str create mode 100644 CPP/cpp_book/chap5/split_str/split_str.cpp create mode 100644 CPP/cpp_book/chap5/split_str/text.txt (limited to 'CPP/cpp_book/chap5/split_str') diff --git a/CPP/cpp_book/chap5/split_str/split_str b/CPP/cpp_book/chap5/split_str/split_str new file mode 100755 index 0000000..06f91bd Binary files /dev/null and b/CPP/cpp_book/chap5/split_str/split_str differ diff --git a/CPP/cpp_book/chap5/split_str/split_str.cpp b/CPP/cpp_book/chap5/split_str/split_str.cpp new file mode 100644 index 0000000..3bb843e --- /dev/null +++ b/CPP/cpp_book/chap5/split_str/split_str.cpp @@ -0,0 +1,147 @@ +#include +#include +#include + +std::vector +split(const std::string& s) { + std::vector ret; + std::string::size_type i = 0; + + /* Invariant: we have processed characters [start i, i) */ + while (i != s.size()) { + + + /* + * Walk to the first non-space character + * + * Invariant: characters between [start i, current i) are all + * spaces + */ + while (i != s.size() && isspace(s[i])) + i++; + + /* + * Find the end of current word + * + * Invariant: None of the characters between [i, j) are spaces + */ + std::string::size_type j = i; + while (j != s.size() && !isspace(s[j])) + j++; + + /* If we found non-whitespace chars, push to the vector */ + if (i != j) { + ret.push_back(s.substr(i, j - i)); + i = j; + } + } + + return ret; +} + +std::string::size_type +width(const std::vector& v) +{ + std::string::size_type ret = 0; + + for (std::vector::const_iterator i = v.begin(); + i != v.end(); + i++) + ret = std::max(ret, i->size()); + + return ret; +} + +std::vector +frame(const std::vector&v) { + + std::vector ret; + std::string::size_type w = width(v); + std::string border(w + 4, '*'); + + ret.push_back(border); + + for (std::string::size_type i = 0; + i != v.size(); + i++) + ret.push_back("* " + v[i] + std::string(w - v[i].size(), ' ') + " *"); + + ret.push_back(border); + + return ret; +} + +std::vector +vcat( + const std::vector& v1, + const std::vector& v2) { + + std::vector ret = v1; + + for (std::vector::const_iterator i = v2.begin(); + i < v2.end(); + i++) + ret.push_back(*i); + + return ret; +} + +std::vector +hcat( + const std::vector& v1, + const std::vector& v2) { + + std::vector ret; + + std::string::size_type w1 = width(v1) + 1; + + + std::vector::size_type i = 0, j = 0; + + while (i != v1.size() || j != v2.size()) { + std::string s; + + if (i != v1.size()) + s = v1[i++]; + + s += std::string(w1 - s.size(), ' '); + + if (j != v2.size()) + s += v2[j++]; + + ret.push_back(s); + } + + return ret; +} + +int main(void) { + + std::string s; + while (getline(std::cin, s)) { + + std::vector vec = split(s); + + vec = frame(vec); + for (std::vector::const_iterator i = vec.begin(); + i < vec.end(); + i++) { + std::cout << *i << std::endl; + } + } + + std::string a = "Vai curintia fela da puta"; + std::vector v1 = split(a); + v1 = frame(v1); + std::string b = "Curintiano sao tudo paus do caralho porra locas"; + std::vector v2 = split(b); + + std::vector v3 = hcat(v1, v2); + for (std::vector::const_iterator i = v3.begin(); + i < v3.end(); + i++) { + std::cout << *i << std::endl; + } + + return 0; +} diff --git a/CPP/cpp_book/chap5/split_str/text.txt b/CPP/cpp_book/chap5/split_str/text.txt new file mode 100644 index 0000000..a6436ff --- /dev/null +++ b/CPP/cpp_book/chap5/split_str/text.txt @@ -0,0 +1 @@ +seu corno puto manso -- cgit v1.2.3