Loading lang_cpp_ex_0202...
struct Exam { std::string title; std::vector<double> marks; };
double mean( /* ...an Exam to be consulted... */ );
void adjust( /* ...an Exam to be altered... */ , double delta);
void test_Exam_mean_adjust() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; sharing::Exam e={"maths", {8.0, 15.0, 4.0, 13.0, 11.0, 9.0, 17.0, 10.0, 12.0, 7.0}}; // ...use mean() on e and display result... // ...use adjust() on e with delta=1.0... // ...use mean() on e and display result... }
struct Sensor { std::string device; std::vector<double> values; };
void scale( /* ...a Sensor to be altered... */ , double factor);
bool saturated( /* ...a Sensor to be consulted... */ , double limit);
void test_Sensor_scale_saturated() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; sharing::Sensor s={"pressure", {0.8, 1.5, 0.4, 1.3, 1.1, 0.9, 1.7, 1.0, 1.2, 0.7}}; // ...use saturated() on s with limit=1.5 and display result... // ...use scale() on s with factor=0.8... // ...use saturated() on s with limit=1.5 and display result... }
struct Album { std::string artist; std::vector<std::string> songs; };
void add_prefix(/* ...an Album to be altered... */);
bool prefixed(/* ...an Album to be consulted... */);
if(song.compare(0, size(artist), artist)!=0) { /* prefix not detected! */ })
void test_Album_prefix() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; sharing::Album a={"David Bowie", {"Space Oddity", "Life On Mars", "Ziggy Stardust", "The Jean Genie", "Ashes To Ashes", "Heroes"}}; // ...use prefixed() on a and display result... // ...use add_prefix() on a... // ...use prefixed() on a and display result... }
//----------------------------------------------------------------------------
#ifndef SHARING_ACCESS_HPP #define SHARING_ACCESS_HPP
#include <string> #include <vector>
namespace sharing {
struct Exam { std::string title; std::vector<double> marks; };
double mean(const Exam &e);
void adjust(Exam &e, double delta);
struct Sensor { std::string device; std::vector<double> values; };
void scale(Sensor &s, double factor);
bool saturated(const Sensor &s, double limit);
struct Album { std::string artist; std::vector<std::string> songs; };
bool prefixed(const Album &a);
void add_prefix(Album &a);
} // namespace sharing
#endif // SHARING_ACCESS_HPP
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "access.hpp"
namespace sharing {
double mean(const Exam &e) { double sum=0.0; for(const auto &elem: e.marks) { sum+=elem; } return sum/double(size(e.marks)); }
void adjust(Exam &e, double delta) { for(auto &elem: e.marks) { elem+=delta; } }
void scale(Sensor &s, double factor) { for(auto &elem: s.values) { elem*=factor; } }
bool saturated(const Sensor &s, double limit) { for(const auto &elem: s.values) { if(elem>limit) { return true; } } return false; }
bool prefixed(const Album &a) { for(const auto &elem: a.songs) { if(elem.compare(0, size(a.artist), a.artist)!=0) { return false; } } return true; }
void add_prefix(Album &a) { for(auto &elem: a.songs) { elem=a.artist+" - "+elem; } }
} // namespace sharing
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "access.hpp"
#include <iostream>
void test_Exam_mean_adjust() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; sharing::Exam e={"maths", {8.0, 15.0, 4.0, 13.0, 11.0, 9.0, 17.0, 10.0, 12.0, 7.0}}; std::cout << "before: mean of " << e.title << " is " << mean(e) << '\n'; adjust(e, 1.0); std::cout << "after: mean of " << e.title << " is " << mean(e) << '\n'; for(const auto &elem: e.marks) { std::cout << ' ' << elem; } std::cout << '\n'; }
void test_Sensor_scale_saturated() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; sharing::Sensor s={"pressure", {0.8, 1.5, 0.4, 1.3, 1.1, 0.9, 1.7, 1.0, 1.2, 0.7}}; std::cout << "before: " << s.device << " over 1.5? " << saturated(s, 1.5) << '\n'; scale(s, 0.8); std::cout << "after: " << s.device << " over 1.5? " << saturated(s, 1.5) << '\n'; for(const auto &elem: s.values) { std::cout << ' ' << elem; } std::cout << '\n'; }
void test_Album_prefix() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; sharing::Album a={"David Bowie", {"Space Oddity", "Life On Mars", "Ziggy Stardust", "The Jean Genie", "Ashes To Ashes", "Heroes"}}; std::cout << "before: " << a.artist << " album prefixed? " << prefixed(a) << '\n'; add_prefix(a); std::cout << "after: " << a.artist << " album prefixed? " << prefixed(a) << '\n'; for(const auto &elem: a.songs) { std::cout << " '" << elem << '\''; } std::cout << '\n'; }
int main() { test_Exam_mean_adjust(); test_Sensor_scale_saturated(); test_Album_prefix(); return 0; }
//----------------------------------------------------------------------------