diff options
| author | Carlos Maiolino <[email protected]> | 2025-09-06 09:37:14 +0200 |
|---|---|---|
| committer | Carlos Maiolino <[email protected]> | 2025-09-06 09:37:14 +0200 |
| commit | 2c9056a23e1a55fd21a8e314c903d9325bffd62e (patch) | |
| tree | a73ba1c7bb60dd89c9e70769dd510e59a49a56a1 | |
| parent | ee8a08bfa24d9a3d09273fdebe8d73ac38c545f9 (diff) | |
Move CPP code here
Signed-off-by: Carlos Maiolino <[email protected]>
40 files changed, 812 insertions, 0 deletions
diff --git a/CPP/cpp_book/.1_1.cpp.un~ b/CPP/cpp_book/.1_1.cpp.un~ Binary files differnew file mode 100644 index 0000000..3400a2d --- /dev/null +++ b/CPP/cpp_book/.1_1.cpp.un~ diff --git a/CPP/cpp_book/.1_2.cpp.un~ b/CPP/cpp_book/.1_2.cpp.un~ Binary files differnew file mode 100644 index 0000000..3a1c9b3 --- /dev/null +++ b/CPP/cpp_book/.1_2.cpp.un~ diff --git a/CPP/cpp_book/.exe.cpp.un~ b/CPP/cpp_book/.exe.cpp.un~ Binary files differnew file mode 100644 index 0000000..23003a7 --- /dev/null +++ b/CPP/cpp_book/.exe.cpp.un~ diff --git a/CPP/cpp_book/.greeting.cpp.un~ b/CPP/cpp_book/.greeting.cpp.un~ Binary files differnew file mode 100644 index 0000000..7c476f3 --- /dev/null +++ b/CPP/cpp_book/.greeting.cpp.un~ diff --git a/CPP/cpp_book/chap1/1_1 b/CPP/cpp_book/chap1/1_1 Binary files differnew file mode 100755 index 0000000..1d73f5e --- /dev/null +++ b/CPP/cpp_book/chap1/1_1 diff --git a/CPP/cpp_book/chap1/1_1.cpp b/CPP/cpp_book/chap1/1_1.cpp new file mode 100644 index 0000000..7a6cd2c --- /dev/null +++ b/CPP/cpp_book/chap1/1_1.cpp @@ -0,0 +1,15 @@ +#include <iostream> +#include <string> + +int +main() +{ + std::cout << "Please enter your first name: "; + + std::string name; + std::cin >> name; + + std::cout << "Hello, " << name << ". Welcome!" << std::endl; + + return 0; +} diff --git a/CPP/cpp_book/chap1/1_1.cpp~ b/CPP/cpp_book/chap1/1_1.cpp~ new file mode 100644 index 0000000..c7cc953 --- /dev/null +++ b/CPP/cpp_book/chap1/1_1.cpp~ @@ -0,0 +1,15 @@ +#include <iostream> +#include <string> + +int +main() +{ + std::cout << "Please enter your first name: "; + + std::string name; + std::in >> name; + + std::cout << "Hello, " << name << ". Welcome!" << std::endl; + + return 0; +} diff --git a/CPP/cpp_book/chap1/1_2 b/CPP/cpp_book/chap1/1_2 Binary files differnew file mode 100755 index 0000000..05ac539 --- /dev/null +++ b/CPP/cpp_book/chap1/1_2 diff --git a/CPP/cpp_book/chap1/1_2.cpp b/CPP/cpp_book/chap1/1_2.cpp new file mode 100644 index 0000000..b914271 --- /dev/null +++ b/CPP/cpp_book/chap1/1_2.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <string> + +int +main() +{ + std::cout << "Enter your first name: "; + std::string name; + std::cin >> name; + + const std::string greeting = "Hello, " + name + "!"; + + const std::string spaces(greeting.size(), ' '); + const std::string second = "* " + spaces + " *"; + const std::string first(second.size(), '*'); + + std::cout << msg << std::endl; + + std::cout << std::endl; // Flush stdou buffer + + std::cout << first << std::endl; + std::cout << second << std::endl; + std::cout <<"* " + greeting + " *" << std::endl; + std::cout << second << std::endl; + std::cout << first << std::endl; +} diff --git a/CPP/cpp_book/chap1/1_2.cpp~ b/CPP/cpp_book/chap1/1_2.cpp~ new file mode 100644 index 0000000..9e42110 --- /dev/null +++ b/CPP/cpp_book/chap1/1_2.cpp~ @@ -0,0 +1,29 @@ +#include <iostream> +#include <string> + +int +main() +{ + std::cout << "Enter your first name: "; + std::string name; + std::cin >> name; + + const std::string greeting = "Hello, " + name + "!"; + + const std::string spaces(greeting.size(), ' '); + const std::string second = "* " + spaces + " *"; + const std::string first(second.size(), '*'); + + const std::string exclam = "!"; + const std::string msg = "Hello" + ", world" + exclam; + + std::cout << msg << std::endl; + + std::cout << std::endl; // Flush stdou buffer + + std::cout << first << std::endl; + std::cout << second << std::endl; + std::cout <<"* " + greeting + " *" << std::endl; + std::cout << second << std::endl; + std::cout << first << std::endl; +} diff --git a/CPP/cpp_book/chap1/exe.cpp b/CPP/cpp_book/chap1/exe.cpp new file mode 100644 index 0000000..537d955 --- /dev/null +++ b/CPP/cpp_book/chap1/exe.cpp @@ -0,0 +1,23 @@ +#include <iostream> +#include <string> + +int main(void) { + + std::cout << "Whats us your name? "; + std::string name; + + std::cin >> name; + + std::cout << "Hello, " << name + << std::endl + << "And what is yours? "; + + std::cin >> name; + + std::cout << "Hello, " << name + << "; nice to meet you too!" + << std::endl; + + + return 0; +} diff --git a/CPP/cpp_book/chap2/greeting.cpp b/CPP/cpp_book/chap2/greeting.cpp new file mode 100644 index 0000000..516d36a --- /dev/null +++ b/CPP/cpp_book/chap2/greeting.cpp @@ -0,0 +1,54 @@ +#include <iostream> +#include <string> + +#define ROW_PAD 5 +#define COL_PAD 8 + +int main(void) +{ + using std::cout; using std::endl; + using std::cin; using std::string; + + const int rows = ROW_PAD * 2 + 3; // empty + first/last line + greeting + string name; + + cout << "Enter your name: "; + cin >> name; + + const string greeting = "Hello " + name + "!"; + const string::size_type cols = COL_PAD * 2 + greeting.size() + 2; + + cout << endl; + + /* + * Loop invariant: + * -We've written 'r' rows so far + */ + const string col_blank(COL_PAD, ' '); + const string row_blank(cols - 2, ' '); + for (int r = 0; r != rows; r++) { + + string::size_type c = 0; + /* + * Loop invariant: + * - We've written 'c' columns so far + */ + while (c != cols) { + if (r == ROW_PAD + 1 && c == 1) { + cout << col_blank << greeting << col_blank; + c += greeting.size() + (col_blank.size() * 2); + } else { + if (r == 0 || r == rows - 1 || + c == 0 || c == cols - 1) { + cout << "*"; + c++; + } else { + cout << row_blank; + c += row_blank.size(); + } + } + } + cout << endl; + } + return 0; +} diff --git a/CPP/cpp_book/chap2/greeting.cpp~ b/CPP/cpp_book/chap2/greeting.cpp~ new file mode 100644 index 0000000..c2a3b8f --- /dev/null +++ b/CPP/cpp_book/chap2/greeting.cpp~ @@ -0,0 +1,54 @@ +#include <iostream> +#include <string> + +#define ROW_PAD 5 +#define COL_PAD 8 + +int main(void) +{ + using std::cout; using std::endl; + using std::cin; using std::string; + + const int rows = ROW_PAD * 2 + 3; // empty + first/last line + greeting + string name; + + cout << "Enter your name: "; + cin >> name; + + const string greeting = "Hello " + name + "!"; + const string::size_type cols = COL_PAD * 2 + greeting.size() + 2; + + cout << endl; + + /* + * Loop invariant: + * -We've written 'r' rows so far + */ + const string col_blank(COL_PAD, ' '); + const string row_blank(cols - 2, ' '); + for (int r = 0; r != rows; r++) { + + string::size_type c = 0; + /* + * Loop invariant: + * - We've written 'c' columns so far + */ + while (c != cols) { + if (r == ROW_PAD + 1 && c == 1) { + cout << col_blank << greeting << col_blank; + c += greeting.size() + (blank.size() * 2); + } else { + if (r == 0 || r == rows - 1 || + c == 0 || c == cols - 1) { + cout << "*"; + c++; + } else { + cout << row_blank; + c += row_blank.size(); + } + } + } + cout << endl; + } + return 0; +} diff --git a/CPP/cpp_book/chap3/.grades.cpp.un~ b/CPP/cpp_book/chap3/.grades.cpp.un~ Binary files differnew file mode 100644 index 0000000..4d93a46 --- /dev/null +++ b/CPP/cpp_book/chap3/.grades.cpp.un~ diff --git a/CPP/cpp_book/chap3/grade.cpp b/CPP/cpp_book/chap3/grade.cpp new file mode 100644 index 0000000..0a08217 --- /dev/null +++ b/CPP/cpp_book/chap3/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/chap3/grade.h b/CPP/cpp_book/chap3/grade.h new file mode 100644 index 0000000..7cd73d1 --- /dev/null +++ b/CPP/cpp_book/chap3/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/chap3/grades b/CPP/cpp_book/chap3/grades Binary files differnew file mode 100755 index 0000000..f086657 --- /dev/null +++ b/CPP/cpp_book/chap3/grades diff --git a/CPP/cpp_book/chap3/grades.cpp b/CPP/cpp_book/chap3/grades.cpp new file mode 100644 index 0000000..3c8f19a --- /dev/null +++ b/CPP/cpp_book/chap3/grades.cpp @@ -0,0 +1,48 @@ +#include <iomanip> // IO Manipulators +#include <ios> +#include <iostream> +#include <string> +#include <vector> +#include <algorithm> +#include <stdexcept> +#include "grade.h" +#include "student_info.h" + +using std::cin; using std::string; +using std::cout; using std::endl; + +int +main(void) +{ + std::vector<StudentInfo> students; + StudentInfo record; + string::size_type maxlen = 0; + + while (read_student(cin, record)) { + maxlen = std::max(maxlen, record.name.size()); + students.push_back(record); + } + + sort(students.begin(), students.end(), compare_students); + + for (std::vector<StudentInfo>::size_type i = 0; + i != students.size(); + i++) { + + cout << students[i].name + << string(maxlen + 1 - students[i].name.size(), ' '); + + try { + double final_grade = grade(students[i]); + + std::streamsize prec = cout.precision(); + cout << std::setprecision(3) << final_grade + << std::setprecision(prec); + } catch (std::domain_error& e){ + cout << e.what(); + } + + cout << endl; + } + return 0; +} diff --git a/CPP/cpp_book/chap3/median.cpp b/CPP/cpp_book/chap3/median.cpp new file mode 100644 index 0000000..e9932e8 --- /dev/null +++ b/CPP/cpp_book/chap3/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/chap3/median.h b/CPP/cpp_book/chap3/median.h new file mode 100644 index 0000000..f4e6580 --- /dev/null +++ b/CPP/cpp_book/chap3/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/chap3/student_info.cpp b/CPP/cpp_book/chap3/student_info.cpp new file mode 100644 index 0000000..a55338b --- /dev/null +++ b/CPP/cpp_book/chap3/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/chap3/student_info.h b/CPP/cpp_book/chap3/student_info.h new file mode 100644 index 0000000..697f3c0 --- /dev/null +++ b/CPP/cpp_book/chap3/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/CPP/cpp_book/chap3/tags b/CPP/cpp_book/chap3/tags new file mode 100644 index 0000000..acf7309 --- /dev/null +++ b/CPP/cpp_book/chap3/tags @@ -0,0 +1,4 @@ +Mgrades.cpp grades.cpp /^main(void)$/ +grade grades.cpp /^double grade(double midterm, double final, double / +median grades.cpp /^double median(std::vector<double> vec)$/ +read_homework grades.cpp /^istream& read_homework(istream& in, std::vector<do/ diff --git a/CPP/cpp_book/chap5/approval.cpp b/CPP/cpp_book/chap5/approval.cpp new file mode 100644 index 0000000..c31c7b5 --- /dev/null +++ b/CPP/cpp_book/chap5/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/chap5/approval.h b/CPP/cpp_book/chap5/approval.h new file mode 100644 index 0000000..6db01e3 --- /dev/null +++ b/CPP/cpp_book/chap5/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/chap5/grade.cpp b/CPP/cpp_book/chap5/grade.cpp new file mode 100644 index 0000000..0a08217 --- /dev/null +++ b/CPP/cpp_book/chap5/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/chap5/grade.h b/CPP/cpp_book/chap5/grade.h new file mode 100644 index 0000000..7cd73d1 --- /dev/null +++ b/CPP/cpp_book/chap5/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/chap5/grades b/CPP/cpp_book/chap5/grades Binary files differnew file mode 100755 index 0000000..638d2ff --- /dev/null +++ b/CPP/cpp_book/chap5/grades diff --git a/CPP/cpp_book/chap5/grades.core b/CPP/cpp_book/chap5/grades.core Binary files differnew file mode 100644 index 0000000..fc50544 --- /dev/null +++ b/CPP/cpp_book/chap5/grades.core diff --git a/CPP/cpp_book/chap5/grades.cpp b/CPP/cpp_book/chap5/grades.cpp new file mode 100644 index 0000000..acbc1e0 --- /dev/null +++ b/CPP/cpp_book/chap5/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/chap5/median.cpp b/CPP/cpp_book/chap5/median.cpp new file mode 100644 index 0000000..e9932e8 --- /dev/null +++ b/CPP/cpp_book/chap5/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/chap5/median.h b/CPP/cpp_book/chap5/median.h new file mode 100644 index 0000000..f4e6580 --- /dev/null +++ b/CPP/cpp_book/chap5/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/chap5/split_str/split_str b/CPP/cpp_book/chap5/split_str/split_str Binary files differnew file mode 100755 index 0000000..06f91bd --- /dev/null +++ b/CPP/cpp_book/chap5/split_str/split_str 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 <iostream> +#include <vector> +#include <cctype> + +std::vector<std::string> +split(const std::string& s) { + std::vector<std::string> 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<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 fela da puta"; + std::vector<std::string> v1 = split(a); + v1 = frame(v1); + std::string b = "Curintiano sao tudo paus do caralho porra locas"; + 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/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 diff --git a/CPP/cpp_book/chap5/student_info.cpp b/CPP/cpp_book/chap5/student_info.cpp new file mode 100644 index 0000000..a55338b --- /dev/null +++ b/CPP/cpp_book/chap5/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/chap5/student_info.h b/CPP/cpp_book/chap5/student_info.h new file mode 100644 index 0000000..697f3c0 --- /dev/null +++ b/CPP/cpp_book/chap5/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/CPP/cpp_book/test.cpp b/CPP/cpp_book/test.cpp new file mode 100644 index 0000000..28186d3 --- /dev/null +++ b/CPP/cpp_book/test.cpp @@ -0,0 +1,39 @@ +#include <iostream> +#include <string> + +struct name { + std::string name; + int num[5]; +}; + +int main(void) { + + int x; + int i = 0; + struct name foo; + struct name bar; + std::cin >> foo.name; + while (std::cin >> x) { + foo.num[i] = x; + i++; + + if (i >= 5) + break; + } + i = 0; + + if (std::cin) + std::cout << "We have data" << std::endl; + std::cin.clear(); + std::cin >> bar.name; + while (std::cin >> x) { + bar.num[i] = x; + i++; + + if (i >= 5) + break; + } + + std::cout << foo.name.size() << " " << bar.name.size() << std::endl; + return 0; +} diff --git a/CPP/cpp_book/test2 b/CPP/cpp_book/test2 Binary files differnew file mode 100755 index 0000000..65e91b3 --- /dev/null +++ b/CPP/cpp_book/test2 diff --git a/CPP/cpp_book/test2.cpp b/CPP/cpp_book/test2.cpp new file mode 100644 index 0000000..54ae462 --- /dev/null +++ b/CPP/cpp_book/test2.cpp @@ -0,0 +1,14 @@ +#include <iostream> +#include <string> + +int main(void) { + + std::string name; + int a, b, c; + while (std::cin >> name) { + std::cin >> a >> b; + std::cout << name << a << b << std::endl; + } + + return 0; +} |
