Loading lang_cpp_ex_0203...
struct Exam { std::string title; std::vector<double> marks; };
/* ...good marks and bad marks... */ split_marks( /* ...an Exam to be consulted... */ , double limit);
void test_Exam_split_marks() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; const sharing::Exam e= {"maths", {8.0, 15.0, 4.0, 13.0, 11.0, 9.0, 17.0, 10.0, 12.0, 7.0}}; // ...obtain the good marks and the bad marks of e... // ...display the good marks... // ...display the bad marks... }
struct Sensor { std::string device; std::vector<double> values; };
/* ...squares and square-roots... */ quadratic_values( /* ...a Sensor to be consulted... */ );
void test_Sensor_quadratic_values() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; const sharing::Sensor s= {"pressure", {0.8, 1.5, 0.4, 1.3, 1.1, 0.9, 1.7, 1.0, 1.2, 0.7}}; // ...obtain the squares and the square-roots of the values in s... // ...display the squares... // ...display the square-roots... }
struct Album { std::string artist; std::vector<std::string> songs; };
/* ...short song titles and long song titles... */ split_songs( /* ...an Album to be consulted... */ , int limit);
void test_Album_split_songs() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; const sharing::Album a= {"David Bowie", {"Space Oddity", "Life On Mars", "Ziggy Stardust", "The Jean Genie", "Ashes To Ashes", "Heroes"}}; // ...obtain the short song titles and the long song titles of a... // ...display the short song titles... // ...display the long song titles... }
//----------------------------------------------------------------------------
#ifndef SHARING_RESULTS_HPP #define SHARING_RESULTS_HPP
#include <string> #include <vector> #include <tuple>
namespace sharing {
struct Exam { std::string title; std::vector<double> marks; };
std::tuple<std::vector<double>, // good marks std::vector<double>> // bad marks split_marks(const Exam &e, double limit);
struct Sensor { std::string device; std::vector<double> values; };
std::tuple<std::vector<double>, // squares std::vector<double>> // square-roots quadratic_values(const Sensor &s);
struct Album { std::string artist; std::vector<std::string> songs; };
std::tuple<std::vector<std::string>, // short song titles std::vector<std::string>> // long song titles split_songs(const Album &a, int limit);
} // namespace sharing
#endif // SHARING_RESULTS_HPP
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "results.hpp"
#include <cmath>
namespace sharing {
std::tuple<std::vector<double>, // good marks std::vector<double>> // bad marks split_marks(const Exam &e, double limit) { std::vector<double> good; std::vector<double> bad; for(const auto &elem: e.marks) { (elem>=limit ? good : bad).emplace_back(elem); } return {std::move(good), std::move(bad)}; }
std::tuple<std::vector<double>, // squares std::vector<double>> // square-roots quadratic_values(const Sensor &s) { std::vector<double> squares; std::vector<double> roots; for(const auto &elem: s.values) { squares.emplace_back(elem*elem); roots.emplace_back(std::sqrt(elem)); } return {std::move(squares), std::move(roots)}; }
std::tuple<std::vector<std::string>, // short song titles std::vector<std::string>> // long song titles split_songs(const Album &a, int limit) { std::vector<std::string> short_titles; std::vector<std::string> long_titles; for(const auto &elem: a.songs) { (int(size(elem))>limit ? long_titles : short_titles).emplace_back(elem); } return {std::move(short_titles), std::move(long_titles)}; }
} // namespace sharing
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "results.hpp"
#include <iostream>
void test_Exam_split_marks() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; const sharing::Exam e= {"maths", {8.0, 15.0, 4.0, 13.0, 11.0, 9.0, 17.0, 10.0, 12.0, 7.0}}; const auto [good, bad]=split_marks(e, 10.0); std::cout << "good marks:"; for(const auto &elem: good) { std::cout << ' ' << elem; } std::cout << '\n'; std::cout << "bad marks:"; for(const auto &elem: bad) { std::cout << ' ' << elem; } std::cout << '\n'; }
void test_Sensor_quadratic_values() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; const sharing::Sensor s= {"pressure", {0.8, 1.5, 0.4, 1.3, 1.1, 0.9, 1.7, 1.0, 1.2, 0.7}}; const auto [squares, roots]=quadratic_values(s); std::cout << "squares:"; for(const auto &elem: squares) { std::cout << ' ' << elem; } std::cout << '\n'; std::cout << "square roots:"; for(const auto &elem: roots) { std::cout << ' ' << elem; } std::cout << '\n'; }
void test_Album_split_songs() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; const sharing::Album a= {"David Bowie", {"Space Oddity", "Life On Mars", "Ziggy Stardust", "The Jean Genie", "Ashes To Ashes", "Heroes"}}; const auto [short_titles, long_titles]=split_songs(a, 13); std::cout << "short titles:"; for(const auto &elem: short_titles) { std::cout << " '" << elem << '\''; } std::cout << '\n'; std::cout << "long titles:"; for(const auto &elem: long_titles) { std::cout << " '" << elem << '\''; } std::cout << '\n'; }
int main() { test_Exam_split_marks(); test_Sensor_quadratic_values(); test_Album_split_songs(); return 0; }
//----------------------------------------------------------------------------