diff options
| author | Carlos Maiolino <[email protected]> | 2026-02-20 16:17:14 +0100 |
|---|---|---|
| committer | Carlos Maiolino <[email protected]> | 2026-02-20 16:17:14 +0100 |
| commit | 4ff0e42f65d8bba3d21bed53bfe1251d8db5c13f (patch) | |
| tree | 9ac61873e2676c3f392bd26063afeb16897c3902 | |
| parent | fd313dd5ad9ac067a31f2b1760b85bd305567131 (diff) | |
23 files changed, 2306 insertions, 0 deletions
diff --git a/CPP/cpp_book/chap6/approval.cpp b/CPP/cpp_book/chap6/approval.cpp new file mode 100644 index 0000000..c31c7b5 --- /dev/null +++ b/CPP/cpp_book/chap6/approval.cpp @@ -0,0 +1,28 @@ +#include <vector> +#include <iostream> +#include "student_info.h" +#include "grade.h" +#include "approval.h" + +bool fgrade(const StudentInfo& s) { + return grade(s) < 60; +} + +std::list<StudentInfo> +extract_fails( + std::list<StudentInfo>& students) +{ + std::list<StudentInfo> fail; + std::list<StudentInfo>::iterator iter = students.begin(); + + while (iter != students.end()) { + if (fgrade(*iter)) { + fail.push_back(*iter); + iter = students.erase(iter); + } else { + iter++; + } + } + + return fail; +} diff --git a/CPP/cpp_book/chap6/approval.h b/CPP/cpp_book/chap6/approval.h new file mode 100644 index 0000000..6db01e3 --- /dev/null +++ b/CPP/cpp_book/chap6/approval.h @@ -0,0 +1,10 @@ +#ifndef APPROVAL_H +#define APPROVAL_H + +#include <list> +#include "student_info.h" + +bool fgrade(const StudentInfo& s); +std::list<StudentInfo> extract_fails(std::list<StudentInfo>& students); + +#endif /* APPROVAL_H */ diff --git a/CPP/cpp_book/chap6/cscope.out b/CPP/cpp_book/chap6/cscope.out new file mode 100644 index 0000000..e59c74b --- /dev/null +++ b/CPP/cpp_book/chap6/cscope.out @@ -0,0 +1,1764 @@ +cscope 15 $HOME/Source/PERSONAL/playground/CPP/cpp_book/chap6 0000007269 + @approval.cpp + +1 + ~<ve�� +> + +2 + ~<io��am +> + +3 + ~"�ud�t_�fo.h +" + +4 + ~"g�de.h +" + +5 + ~"��ov�.h +" + +7 +bo� + + $fg�de +(cڡ +Stud�tInfo +& +s +) { + +8 + `g�de +( +s +) < 60; + +9 + } +} + +11 + g�d +:: +li� +< +Stud�tInfo +> + +12 +ex�a�_�s +( + +13 +�d +:: +li� +< +Stud�tInfo +>& +�ud�ts +) + +15 +�d +:: +li� +< +Stud�tInfo +> +� +; + +16 + g�d +:: +li� +< +Stud�tInfo +>:: +���� + +�� + = +�ud�ts +. +beg� +(); + +18 + g�� + !� +�ud�ts +. +�d +()) { + +19 i�( +fg�de +(* +�� +)) { + +20 +� +. +push_back +(* +�� +); + +21 + g�� + = +�ud�ts +. +�a� +( +�� +); + +23 + g�� +++; + +27 + g� +; + + @approval.h + +1 #i�de� +APPROVAL_H + + +2 + #APPROVAL_H + + + ) + +4 + ~<li� +> + +5 + ~"�ud�t_�fo.h +" + +7 +bo� + +fg�de +(cڡ +Stud�tInfo +& +s +); + +8 + g�d +:: +li� +< +Stud�tInfo +> +ex�a�_�s +( +�d +::li�<Stud�tInfo>& +�ud�ts +); + + @grade.cpp + +1 + ~<ve�� +> + +2 + ~"�ud�t_�fo.h +" + +3 + ~"med�n.h +" + +4 + ~"g�de.h +" + +6 + $g�de +( +mid�rm +, +f�� +, +homew�k +) + +8 ( +mid�rm + * 0.2 + + +9 +f�� + * 0.4 + + +10 +homew�k + * 0.4); + +11 + } +} + +14 +g�de +( +mid�rm +, +f�� +, cڡ +�d +:: +ve�� +<>& +hw +) + +16 i�( +hw +. +size +() == 0) + +17 +throw + +�d +:: +doma�_�r� +("Student has�o homework"); + +19 +g�de +( +mid�rm +, +f�� +, +med�n +( +hw +)); + +22 + $g�de +(cڡ +Stud�tInfo +& +s +) + +24 + `g�de +( +s +. +mid�rm +, s. +f�� +, s. +homew�k +); + +25 + } +} + + @grade.h + +1 #i�de� +GRADE_H + + +2 + #GRADE_H + + + ) + +4 + ~<ve�� +> + +5 + ~"�ud�t_�fo.h +" + +7 +g�de +( +mid�rm +, +f�� +, +homew�k +); + +8 +g�de +( +mid�rm +, +f�� +, cڡ +�d +:: +ve�� +<>& +hw +); + +9 +g�de +(cڡ +Stud�tInfo +& +s +); + + @grades.cpp + +1 + ~<iom�� +> + +2 + ~<ios +> + +3 + ~<io��am +> + +4 + ~<�r�g +> + +5 + ~<li� +> + +6 + ~<�gܙhm +> + +7 + ~<�dex�� +> + +8 + ~"g�de.h +" + +9 + ~"�ud�t_�fo.h +" + +10 + ~"��ov�.h +" + +12 +us�g + + g�d +:: +c� +; us�g std:: +�r�g +; + +13 +us�g + + g�d +:: +cout +; us�g std:: +�dl +; + +16 + $ma� +() + +18 +�d +:: +li� +< +Stud�tInfo +> +�ud�ts +, +� +; + +19 +Stud�tInfo + +�c�d +; + +20 +�r�g +:: +size_ty� + +max�n + = 0; + +22 + `�ad_�ud�t +( +c� +, +�c�d +)) { + +23 +max�n + = +�d +:: + `max +(max�n, +�c�d +. +�me +. + `size +()); + +24 +�ud�ts +. + `push_back +( +�c�d +); + +27 +�ud�ts +. + `s�t +( +com��_�ud�ts +); + +29 +�d +:: +li� +< +Stud�tInfo +>:: +���� + +i + = +�ud�ts +. + `beg� +(); + +30 +i + !� +�ud�ts +. + `�d +(); + +31 +i +++) { + +33 +cout + << +i +-> +�me + + +34 << + `�r�g +( +max�n + + 1 - +i +-> +�me +. + `size +(), ' '); + +36 +�y + { + +37 +f��_g�de + = + `g�de +(* +i +); + +39 +�d +:: +��amsize + +�ec + = +cout +. + `�ecisi� +(); + +40 +cout + << +�d +:: + `���cisi� +(3�<< +f��_g�de + + +41 << +�d +:: + `���cisi� +( +�ec +); + +42 +cout + << " - Student " + +43 << ( + `fg�de +(* +i +) ? "Failed" : "Approved"); + +45 } + `�tch + ( +�d +:: +doma�_�r� +& +e +){ + +46 +cout + << +e +. + `wh� +(); + +49 +cout + << +�dl +; + +52 +� + = + `ex�a�_�s +( +�ud�ts +); + +54 +�d +:: +cout + << "Fa�ed stud�ts: " << std:: +�dl +; + +56 +�d +:: +li� +< +Stud�tInfo +>:: +���� + +i + = +� +. + `beg� +(); + +57 +i + !� +� +. + `�d +(); + +58 +i +++) { + +59 +�d +:: +cout + << +i +-> +�me + << std:: +�dl +; + +63 + } +} + + @median.cpp + +1 + ~<ve�� +> + +2 + ~<�gܙhm +> + +3 + ~<�dex�� +> + +5 +med�n +( +�d +:: +ve�� +<> +vec +) + +7 +�d +:: +ve�� +<>:: +size_ty� + +mid +, + gsize +; + +9 + gsize + = +vec +. +size +(); + +11 i�( + gsize + == 0) + +12 +throw + +�d +:: +doma�_�r� +("median of�n�mpty vector"); + +14 +s�t +( +vec +. +beg� +(), vec. +�d +()); + +16 + gmid + = +size + / 2; + +18 + gsize + % 2 =�0 ? ( +vec +[ +mid +] + vec[mid - 1]) / 2 + +19 : +vec +[ +mid +]; + + @median.h + +1 #i�de� +MEDIAN_H + + +2 + #MEDIAN_H + + + ) + +4 + ~<ve�� +> + +6 +med�n +( +�d +:: +ve�� +<> +vec +); + + @split_str/split_str.cpp + +1 + ~<io��am +> + +2 + ~<ve�� +> + +3 + ~<c�y� +> + +5 + g�d +:: +ve�� +< +�d +:: +�r�g +> + +6 + $�l� +(cڡ +�d +:: +�r�g +& +s +) { + +7 +�d +:: +ve�� +<�d:: +�r�g +> +�t +; + +8 +�d +:: +�r�g +:: +size_ty� + +i + = 0; + +11 +i + !� +s +. + `size +()) { + +20 +i + !� +s +. + `size +(�&& + `is�a� +(s[i])) + +21 +i +++; + +28 +�d +:: +�r�g +:: +size_ty� + +j + = +i +; + +29 +j + !� +s +. + `size +(�&& ! + `is�a� +(s[j])) + +30 +j +++; + +33 i�( +i + !� +j +) { + +34 +�t +. + `push_back +( +s +. + `sub�r +( +i +, +j + - i)); + +35 +i + = +j +; + +39 +�t +; + +40 + } +} + +42 + g�d +:: +�r�g +:: +size_ty� + + +43 +width +(cڡ +�d +:: +ve�� +<�d:: +�r�g +>& +v +) + +45 +�d +:: +�r�g +:: +size_ty� + +�t + = 0; + +47 + g�d +:: +ve�� +< +�d +:: +�r�g +>:: +cڡ_���� + +i + = +v +. +beg� +(); + +48 + gi + !� +v +. +�d +(); + +49 + gi +++) + +50 + g�t + = +�d +:: +max +( +�t +, +i +-> +size +()); + +52 + g�t +; + +55 + g�d +:: +ve�� +< +�d +:: +�r�g +> + +56 +�ame +(cڡ +�d +:: +ve�� +<�d:: +�r�g +>& +v +) { + +58 +�d +:: +ve�� +<�d:: +�r�g +> +�t +; + +59 + g�d +:: +�r�g +:: +size_ty� + +w + = +width +( +v +); + +60 + g�d +:: +�r�g + +b�d� +( +w + + 4, '*'); + +62 + g�t +. +push_back +( +b�d� +); + +64 + g�d +:: +�r�g +:: +size_ty� + +i + = 0; + +65 + gi + !� +v +. +size +(); + +66 + gi +++) + +67 + g�t +. +push_back +("* " + +v +[ +i +] + +�d +:: +�r�g +( +w + - v[i]. +size +(), ' ') + " *"); + +69 + g�t +. +push_back +( +b�d� +); + +71 + g�t +; + +74 + g�d +:: +ve�� +< +�d +:: +�r�g +> + +75 +v�t +( + +76 cڡ +�d +:: +ve�� +<�d:: +�r�g +>& +v1 +, + +77 cڡ +�d +:: +ve�� +<�d:: +�r�g +>& +v2 +) { + +79 +�d +:: +ve�� +<�d:: +�r�g +> +�t + = +v1 +; + +81 + g�d +:: +ve�� +< +�d +:: +�r�g +>:: +cڡ_���� + +i + = +v2 +. +beg� +(); + +82 + gi + < + gv2 +. +�d +(); + +83 + gi +++) + +84 + g�t +. +push_back +(* +i +); + +86 + g�t +; + +89 + g�d +:: +ve�� +< +�d +:: +�r�g +> + +90 +h�t +( + +91 cڡ +�d +:: +ve�� +<�d:: +�r�g +>& +v1 +, + +92 cڡ +�d +:: +ve�� +<�d:: +�r�g +>& +v2 +) { + +94 +�d +:: +ve�� +<�d:: +�r�g +> +�t +; + +96 + g�d +:: +�r�g +:: +size_ty� + +w1 + = +width +( +v1 +) + 1; + +99 + g�d +:: +ve�� +< +�d +:: +�r�g +>:: +size_ty� + +i + = 0, + gj + = 0; + +101 + gi + !� +v1 +. +size +(�|| +j + !� +v2 +.size()) { + +102 +�d +:: +�r�g + +s +; + +104 i�( + gi + !� +v1 +. +size +()) + +105 +s + = +v1 +[ +i +++]; + +107 + gs + +� +�d +:: +�r�g +( +w1 + - +s +. +size +(), ' '); + +109 i�( + gj + !� +v2 +. +size +()) + +110 +s + +� +v2 +[ +j +++]; + +112 + g�t +. +push_back +( +s +); + +115 + g�t +; + +118 + $ma� +() { + +120 +�d +:: +�r�g + +s +; + +121 + `g�l�e +( +�d +:: +c� +, +s +)) { + +123 +�d +:: +ve�� +<�d:: +�r�g +> +vec + = + `�l� +( +s +); + +125 +vec + = + `�ame +(vec); + +126 +�d +:: +ve�� +<�d:: +�r�g +>:: +cڡ_���� + +i + = +vec +. + `beg� +(); + +127 +i + < +vec +. + `�d +(); + +128 +i +++) { + +129 +�d +:: +cout + << * +i + << std:: +�dl +; + +133 +�d +:: +�r�g + +a + = "Vai curintia fela da�uta"; + +134 +�d +:: +ve�� +<�d:: +�r�g +> +v1 + = + `�l� +( +a +); + +135 +v1 + = + `�ame +(v1); + +136 +�d +:: +�r�g + +b + = "Curintiano sao�udo�aus do caralho�orra�ocas"; + +137 +�d +:: +ve�� +<�d:: +�r�g +> +v2 + = + `�l� +( +b +); + +139 +�d +:: +ve�� +<�d:: +�r�g +> +v3 + = + `h�t +( +v1 +, +v2 +); + +140 +�d +:: +ve�� +<�d:: +�r�g +>:: +cڡ_���� + +i + = +v3 +. + `beg� +(); + +141 +i + < +v3 +. + `�d +(); + +142 +i +++) { + +143 +�d +:: +cout + << * +i + << std:: +�dl +; + +147 + } +} + + @student_info.cpp + +1 + ~"�ud�t_�fo.h +" + +3 +bo� + + $com��_�ud�ts +(cڡ +Stud�tInfo +& +a +, cڡ Stud�tInfo& +b +) + +5 +a +. +�me + < +b +.name; + +6 + } +} + +8 + g�d +:: +i��am +& +�ad_homew�k +( +�d +::i��am& +� +, std:: +ve�� +<>& +hw +) + +10 i�( +� +) { + +11 +hw +. +��r +(); + +13 + gx +; + +18 + g� + >> + gx +) + +19 + ghw +. +push_back +( +x +); + +21 + g� +. +��r +(); + +24 + g� +; + +27 + g�d +:: +i��am +& + $�ad_�ud�t +( +�d +:: +i��am +& +is +, +Stud�tInfo +& +s +) + +30 +is + >> +s +. +�me + >> s. +mid�rm + >> s. +f�� +; + +32 + `�ad_homew�k +( +is +, +s +. +homew�k +); + +33 +is +; + +34 + } +} + + @student_info.h + +1 #i�de� +STUDENT_INFO_H + + +2 + #STUDENT_INFO_H + + + ) + +4 + ~<io��am +> + +5 + ~<�r�g +> + +6 + ~<ve�� +> + +8 + sStud�tInfo + { + +9 + m�d +:: +�r�g + +�me +; + +10 + mmid�rm +; + +11 + mf�� +; + +12 + m�d +:: +ve�� +<> +homew�k +; + +16 +bo� + +com��_�ud�ts +(cڡ +Stud�tInfo +& +a +, cڡ Stud�tInfo& +b +); + +17 + g�d +:: +i��am +& +�ad_homew�k +( +�d +::i��am& +� +, std:: +ve�� +<>& +hw +); + +18 + g�d +:: +i��am +& +�ad_�ud�t +( +�d +::i��am& +is +, +Stud�tInfo +& +s +); + + @ +1 +. +1 +/usr/include +10 +129 +approval.cpp +approval.h +grade.cpp +grade.h +grades.cpp +median.cpp +median.h +split_str/split_str.cpp +student_info.cpp +student_info.h diff --git a/CPP/cpp_book/chap6/find_url/find_url b/CPP/cpp_book/chap6/find_url/find_url Binary files differnew file mode 100755 index 0000000..4d7ac32 --- /dev/null +++ b/CPP/cpp_book/chap6/find_url/find_url diff --git a/CPP/cpp_book/chap6/find_url/furl.cpp b/CPP/cpp_book/chap6/find_url/furl.cpp new file mode 100644 index 0000000..17e9822 --- /dev/null +++ b/CPP/cpp_book/chap6/find_url/furl.cpp @@ -0,0 +1,88 @@ +#include <iostream> +#include <vector> +#include <algorithm> + +static bool +not_url_char(char c) +{ + static const std::string url_ch = "~;/?:@=&$-_.+!*`(),"; + + return !(isalnum(c) || + find(url_ch.begin(), url_ch.end(), c) != url_ch.end()); +} + +static std::string::const_iterator +url_beg( + std::string::const_iterator b, + std::string::const_iterator e) +{ + static const std::string sep = "://"; + + std::string::const_iterator i = b; + + while ((i = search(i, e, sep.begin(), sep.end())) != e) { + + // make sure the separator isn't at the beginning of the line + if (i != b && i + sep.size() != e) { + + // beg marks the beginning of the protocol name + std::string::const_iterator beg = i; + while (beg != b && isalpha(beg[-1])) + beg--; + + // Is there at least one char before and after the separator? + if (beg != i && !not_url_char(i[sep.size()])) { + return beg; + } + } + + // The separator we found wasn't part of a URL; + // advance i past the separator + i += sep.size(); + } + + return e; + +} + +static std::string::const_iterator +url_end( + std::string::const_iterator b, + std::string::const_iterator e) +{ + return find_if(b, e, not_url_char); +} + +/* + * Find URLs in the string 's' and return + * a vector containing all strings found. + * We ignore the protocol and just get the url + */ +std::vector<std::string> +find_urls(const std::string& s) +{ + std::vector<std::string> ret; + std::string::const_iterator b = s.begin(); + std::string::const_iterator e = s.end(); + + while (b != e) { + + /* Look for one or more chars followed by '://' */ + b = url_beg(b, e); + + /* Found it? */ + if (b != e) { + + /* Get the end of the URL after '://' */ + std::string::const_iterator after = url_end(b, e); + + /* Save the url */ + ret.push_back(std::string(b, after)); + + /* Advance b and check if file has more URLs */ + b = after; + } + } + + return ret; +} diff --git a/CPP/cpp_book/chap6/find_url/main.cpp b/CPP/cpp_book/chap6/find_url/main.cpp new file mode 100644 index 0000000..8bd9416 --- /dev/null +++ b/CPP/cpp_book/chap6/find_url/main.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <vector> +#include <algorithm> + +std::vector<std::string> find_urls(const std::string& s); + +int +main(void) +{ + std::string s; + + while(std::getline(std::cin, s)) { + std::vector<std::string> urls; + urls = find_urls(s); + + std::vector<std::string>::const_iterator i = urls.begin(); + + while (i != urls.end()) { + std::cout << *i << std::endl; + i++; + } + } + + return 0; +} + diff --git a/CPP/cpp_book/chap6/find_url/text.txt b/CPP/cpp_book/chap6/find_url/text.txt new file mode 100644 index 0000000..b068661 --- /dev/null +++ b/CPP/cpp_book/chap6/find_url/text.txt @@ -0,0 +1,3 @@ +http://www.ronaldo.com +parangaricotirimirruaru +https://www.toxiclabs.cc diff --git a/CPP/cpp_book/chap6/grade.cpp b/CPP/cpp_book/chap6/grade.cpp new file mode 100644 index 0000000..0a08217 --- /dev/null +++ b/CPP/cpp_book/chap6/grade.cpp @@ -0,0 +1,27 @@ +#include <vector> +#include "student_info.h" +#include "median.h" +#include "grade.h" + +double grade(double midterm, double final, double homework) +{ + return (midterm * 0.2 + + final * 0.4 + + homework * 0.4); +} + +/* Overloads above grade() */ +double grade(double midterm, double final, const std::vector<double>& hw) +{ + if (hw.size() == 0) + throw std::domain_error("Student has no homework"); + + return grade(midterm, final, median(hw)); +} + +double grade(const StudentInfo& s) +{ + return grade(s.midterm, s.final, s.homework); +} + + diff --git a/CPP/cpp_book/chap6/grade.h b/CPP/cpp_book/chap6/grade.h new file mode 100644 index 0000000..7cd73d1 --- /dev/null +++ b/CPP/cpp_book/chap6/grade.h @@ -0,0 +1,11 @@ +#ifndef GRADE_H +#define GRADE_H + +#include <vector> +#include "student_info.h" + +double grade(double midterm, double final, double homework); +double grade(double midterm, double final, const std::vector<double>& hw); +double grade(const StudentInfo& s); + +#endif /* GRADE_H */ diff --git a/CPP/cpp_book/chap6/grades b/CPP/cpp_book/chap6/grades Binary files differnew file mode 100755 index 0000000..f9c52ad --- /dev/null +++ b/CPP/cpp_book/chap6/grades diff --git a/CPP/cpp_book/chap6/grades.cpp b/CPP/cpp_book/chap6/grades.cpp new file mode 100644 index 0000000..acbc1e0 --- /dev/null +++ b/CPP/cpp_book/chap6/grades.cpp @@ -0,0 +1,63 @@ +#include <iomanip> // IO Manipulators +#include <ios> +#include <iostream> +#include <string> +#include <list> +#include <algorithm> +#include <stdexcept> +#include "grade.h" +#include "student_info.h" +#include "approval.h" + +using std::cin; using std::string; +using std::cout; using std::endl; + +int +main(void) +{ + std::list<StudentInfo> students, fail; + StudentInfo record; + string::size_type maxlen = 0; + + while (read_student(cin, record)) { + maxlen = std::max(maxlen, record.name.size()); + students.push_back(record); + } + + students.sort(compare_students); + + for (std::list<StudentInfo>::iterator i = students.begin(); + i != students.end(); + i++) { + + cout << i->name + << string(maxlen + 1 - i->name.size(), ' '); + + try { + double final_grade = grade(*i); + + std::streamsize prec = cout.precision(); + cout << std::setprecision(3) << final_grade + << std::setprecision(prec); + cout << " - Student " + << (fgrade(*i) ? "Failed" : "Approved"); + + } catch (std::domain_error& e){ + cout << e.what(); + } + + cout << endl; + } + + fail = extract_fails(students); + + std::cout << "Failed students: " << std::endl; + + for (std::list<StudentInfo>::iterator i = fail.begin(); + i != fail.end(); + i++) { + std::cout << i->name << std::endl; + } + + return 0; +} diff --git a/CPP/cpp_book/chap6/median.cpp b/CPP/cpp_book/chap6/median.cpp new file mode 100644 index 0000000..e9932e8 --- /dev/null +++ b/CPP/cpp_book/chap6/median.cpp @@ -0,0 +1,21 @@ +#include <vector> +#include <algorithm> +#include <stdexcept> + +double median(std::vector<double> vec) +{ + std::vector<double>::size_type mid, size; + + size = vec.size(); + + if (size == 0) + throw std::domain_error("median of an empty vector"); + + sort(vec.begin(), vec.end()); + + mid = size / 2; + + return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 + : vec[mid]; + +} diff --git a/CPP/cpp_book/chap6/median.h b/CPP/cpp_book/chap6/median.h new file mode 100644 index 0000000..f4e6580 --- /dev/null +++ b/CPP/cpp_book/chap6/median.h @@ -0,0 +1,8 @@ +#ifndef MEDIAN_H +#define MEDIAN_H + +#include <vector> + +double median(std::vector<double> vec); + +#endif /* MEDIAN_H */ diff --git a/CPP/cpp_book/chap6/split_str/split_str b/CPP/cpp_book/chap6/split_str/split_str Binary files differnew file mode 100755 index 0000000..ca1e6aa --- /dev/null +++ b/CPP/cpp_book/chap6/split_str/split_str diff --git a/CPP/cpp_book/chap6/split_str/split_str.cpp b/CPP/cpp_book/chap6/split_str/split_str.cpp new file mode 100644 index 0000000..1c03bb2 --- /dev/null +++ b/CPP/cpp_book/chap6/split_str/split_str.cpp @@ -0,0 +1,152 @@ +#include <iostream> +#include <vector> +#include <cctype> +#include <algorithm> + +bool +space(char c) +{ + return isspace(c); +} + +bool +not_space(char c) +{ + return !isspace(c); +} + +std::vector<std::string> +split(const std::string& s) +{ + std::vector<std::string> ret; + std::string::const_iterator i = s.begin(); + + /* Invariant: we have processed characters [start i, i) */ + while (i != s.end()) { + + /* Find the first non_space char */ + i = find_if(i, s.end(), not_space); + + /* + * Find the end of current word + * + * Invariant: None of the characters between [i, j) are spaces + */ + std::string::const_iterator j = find_if(i, s.end(), space); + + /* If we found non-whitespace chars, push to the vector */ + if (i != s.end()) + ret.push_back(std::string(i, j)); + + i = j; + } + + return ret; +} + +std::string::size_type +width(const std::vector<std::string>& v) +{ + std::string::size_type ret = 0; + + for (std::vector<std::string>::const_iterator i = v.begin(); + i != v.end(); + i++) + ret = std::max(ret, i->size()); + + return ret; +} + +std::vector<std::string> +frame(const std::vector<std::string>&v) { + + std::vector<std::string> 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<std::string> +vcat( + const std::vector<std::string>& v1, + const std::vector<std::string>& v2) { + + std::vector<std::string> ret = v1; + + for (std::vector<std::string>::const_iterator i = v2.begin(); + i < v2.end(); + i++) + ret.push_back(*i); + + return ret; +} + +std::vector<std::string> +hcat( + const std::vector<std::string>& v1, + const std::vector<std::string>& v2) { + + std::vector<std::string> ret; + + std::string::size_type w1 = width(v1) + 1; + + + std::vector<std::string>::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<std::string> vec = split(s); + + vec = frame(vec); + for (std::vector<std::string>::const_iterator i = vec.begin(); + i < vec.end(); + i++) { + std::cout << *i << std::endl; + } + } + + std::string a = "Vai curintia eh nois"; + std::vector<std::string> v1 = split(a); + v1 = frame(v1); + std::string b = "Another string test ronaldo"; + std::vector<std::string> v2 = split(b); + + std::vector<std::string> v3 = hcat(v1, v2); + for (std::vector<std::string>::const_iterator i = v3.begin(); + i < v3.end(); + i++) { + std::cout << *i << std::endl; + } + + return 0; +} diff --git a/CPP/cpp_book/chap6/split_str/text.txt b/CPP/cpp_book/chap6/split_str/text.txt new file mode 100644 index 0000000..a6436ff --- /dev/null +++ b/CPP/cpp_book/chap6/split_str/text.txt @@ -0,0 +1 @@ +seu corno puto manso diff --git a/CPP/cpp_book/chap6/student_info.cpp b/CPP/cpp_book/chap6/student_info.cpp new file mode 100644 index 0000000..a55338b --- /dev/null +++ b/CPP/cpp_book/chap6/student_info.cpp @@ -0,0 +1,34 @@ +#include "student_info.h" + +bool compare_students(const StudentInfo& a, const StudentInfo& b) +{ + return a.name < b.name; +} + +std::istream& read_homework(std::istream& in, std::vector<double>& hw) +{ + if (in) { + hw.clear(); // Empty the vector. + + double x; + /* + * Loop invariant: + * - 'homework' contains all the homework grades read so far + */ + while (in >> x) + hw.push_back(x); + + in.clear(); // Clear the stream in case + // it will be used again. + } + return in; +} + +std::istream& read_student(std::istream& is, StudentInfo& s) +{ + /* Read and store student's name, midterm and final */ + is >> s.name >> s.midterm >> s.final; + + read_homework(is, s.homework); + return is; +} diff --git a/CPP/cpp_book/chap6/student_info.h b/CPP/cpp_book/chap6/student_info.h new file mode 100644 index 0000000..697f3c0 --- /dev/null +++ b/CPP/cpp_book/chap6/student_info.h @@ -0,0 +1,20 @@ +#ifndef STUDENT_INFO_H +#define STUDENT_INFO_H + +#include <iostream> +#include <string> +#include <vector> + +struct StudentInfo { + std::string name; + double midterm; + double final; + std::vector<double> homework; +}; + + +bool compare_students(const StudentInfo& a, const StudentInfo& b); +std::istream& read_homework(std::istream& in, std::vector<double>& hw); +std::istream& read_student(std::istream& is, StudentInfo& s); + +#endif /* STUDENT_INFO_H */ diff --git a/rust/findurl/Cargo.lock b/rust/findurl/Cargo.lock new file mode 100644 index 0000000..e4c345f --- /dev/null +++ b/rust/findurl/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "findurl" +version = "0.1.0" diff --git a/rust/findurl/Cargo.toml b/rust/findurl/Cargo.toml new file mode 100644 index 0000000..7a3bc23 --- /dev/null +++ b/rust/findurl/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "findurl" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/rust/findurl/src/furl.rs b/rust/findurl/src/furl.rs new file mode 100644 index 0000000..5ce60c1 --- /dev/null +++ b/rust/findurl/src/furl.rs @@ -0,0 +1,7 @@ +pub fn find_urls(urls: &mut Vec<String>) -> () { + + urls.push("Ronaldo".to_string()); + for i in urls { + println!("{i}"); + } +} diff --git a/rust/findurl/src/main.rs b/rust/findurl/src/main.rs new file mode 100644 index 0000000..1723df4 --- /dev/null +++ b/rust/findurl/src/main.rs @@ -0,0 +1,27 @@ +mod furl; + +fn main() { + let mut s = String::new(); + + loop { + + match std::io::stdin().read_line(&mut s) { + Ok(n) => { + if n == 0 { + break; + } + } + Err(_) => panic!(), + } + } + + let mut urls: Vec<String> = Vec::new(); + + furl::find_urls(&mut urls); + + for i in &urls { + println!("{i}"); + } + + println!("{}", s); +} diff --git a/rust/findurl/text.txt b/rust/findurl/text.txt new file mode 100644 index 0000000..1c44e9c --- /dev/null +++ b/rust/findurl/text.txt @@ -0,0 +1,3 @@ +https://www.ronaldo.com +http://www.xproblems.cc +gopher://gopher.kaboom |
