From 4ff0e42f65d8bba3d21bed53bfe1251d8db5c13f Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Fri, 20 Feb 2026 16:17:14 +0100 Subject: extra code --- CPP/cpp_book/chap6/approval.cpp | 28 + CPP/cpp_book/chap6/approval.h | 10 + CPP/cpp_book/chap6/cscope.out | 1764 ++++++++++++++++++++++++++++ CPP/cpp_book/chap6/find_url/find_url | Bin 0 -> 51128 bytes CPP/cpp_book/chap6/find_url/furl.cpp | 88 ++ CPP/cpp_book/chap6/find_url/main.cpp | 26 + CPP/cpp_book/chap6/find_url/text.txt | 3 + CPP/cpp_book/chap6/grade.cpp | 27 + CPP/cpp_book/chap6/grade.h | 11 + CPP/cpp_book/chap6/grades | Bin 0 -> 68104 bytes CPP/cpp_book/chap6/grades.cpp | 63 + CPP/cpp_book/chap6/median.cpp | 21 + CPP/cpp_book/chap6/median.h | 8 + CPP/cpp_book/chap6/split_str/split_str | Bin 0 -> 66576 bytes CPP/cpp_book/chap6/split_str/split_str.cpp | 152 +++ CPP/cpp_book/chap6/split_str/text.txt | 1 + CPP/cpp_book/chap6/student_info.cpp | 34 + CPP/cpp_book/chap6/student_info.h | 20 + 18 files changed, 2256 insertions(+) create mode 100644 CPP/cpp_book/chap6/approval.cpp create mode 100644 CPP/cpp_book/chap6/approval.h create mode 100644 CPP/cpp_book/chap6/cscope.out create mode 100755 CPP/cpp_book/chap6/find_url/find_url create mode 100644 CPP/cpp_book/chap6/find_url/furl.cpp create mode 100644 CPP/cpp_book/chap6/find_url/main.cpp create mode 100644 CPP/cpp_book/chap6/find_url/text.txt create mode 100644 CPP/cpp_book/chap6/grade.cpp create mode 100644 CPP/cpp_book/chap6/grade.h create mode 100755 CPP/cpp_book/chap6/grades create mode 100644 CPP/cpp_book/chap6/grades.cpp create mode 100644 CPP/cpp_book/chap6/median.cpp create mode 100644 CPP/cpp_book/chap6/median.h create mode 100755 CPP/cpp_book/chap6/split_str/split_str create mode 100644 CPP/cpp_book/chap6/split_str/split_str.cpp create mode 100644 CPP/cpp_book/chap6/split_str/text.txt create mode 100644 CPP/cpp_book/chap6/student_info.cpp create mode 100644 CPP/cpp_book/chap6/student_info.h (limited to 'CPP') 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 +#include +#include "student_info.h" +#include "grade.h" +#include "approval.h" + +bool fgrade(const StudentInfo& s) { + return grade(s) < 60; +} + +std::list +extract_fails( + std::list& students) +{ + std::list fail; + std::list::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 +#include "student_info.h" + +bool fgrade(const StudentInfo& s); +std::list extract_fails(std::list& 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  + ~ + +2  + ~ + +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  + ~ + +5  + ~"¡ud’t_šfo.h +" + +7 +boŞ + +fg¿de +(cÚ¡ +Stud’tInfo +& +s +); + +8 + g¡d +:: +li¡ +< +Stud’tInfo +> +exŒaù_çs +( +¡d +::li¡& +¡ud’ts +); + + @grade.cpp + +1  + ~ + +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  + ~ + +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  + ~ + +2  + ~ + +3  + ~ + +4  + ~<¡ršg +> + +5  + ~ + +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 + << "Faed 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  + ~ + +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  + ~ + +6  +medŸn +( +¡d +:: +veùÜ +<> +vec +); + + @split_str/split_str.cpp + +1  + ~ + +2  + ~ + +3  + ~ + +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 saoudo…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  + ~ + +5  + ~<¡ršg +> + +6  + ~ + +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 new file mode 100755 index 0000000..4d7ac32 Binary files /dev/null and b/CPP/cpp_book/chap6/find_url/find_url differ 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 +#include +#include + +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 +find_urls(const std::string& s) +{ + std::vector 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 +#include +#include + +std::vector find_urls(const std::string& s); + +int +main(void) +{ + std::string s; + + while(std::getline(std::cin, s)) { + std::vector urls; + urls = find_urls(s); + + std::vector::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 +#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& 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 +#include "student_info.h" + +double grade(double midterm, double final, double homework); +double grade(double midterm, double final, const std::vector& hw); +double grade(const StudentInfo& s); + +#endif /* GRADE_H */ diff --git a/CPP/cpp_book/chap6/grades b/CPP/cpp_book/chap6/grades new file mode 100755 index 0000000..f9c52ad Binary files /dev/null and b/CPP/cpp_book/chap6/grades differ 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 // IO Manipulators +#include +#include +#include +#include +#include +#include +#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 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::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::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 +#include +#include + +double median(std::vector vec) +{ + std::vector::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 + +double median(std::vector vec); + +#endif /* MEDIAN_H */ diff --git a/CPP/cpp_book/chap6/split_str/split_str b/CPP/cpp_book/chap6/split_str/split_str new file mode 100755 index 0000000..ca1e6aa Binary files /dev/null and b/CPP/cpp_book/chap6/split_str/split_str differ 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 +#include +#include +#include + +bool +space(char c) +{ + return isspace(c); +} + +bool +not_space(char c) +{ + return !isspace(c); +} + +std::vector +split(const std::string& s) +{ + std::vector 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& 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 eh nois"; + std::vector v1 = split(a); + v1 = frame(v1); + std::string b = "Another string test ronaldo"; + 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/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& 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 +#include +#include + +struct StudentInfo { + std::string name; + double midterm; + double final; + std::vector homework; +}; + + +bool compare_students(const StudentInfo& a, const StudentInfo& b); +std::istream& read_homework(std::istream& in, std::vector& hw); +std::istream& read_student(std::istream& is, StudentInfo& s); + +#endif /* STUDENT_INFO_H */ -- cgit v1.2.3