#include #include #include #include #include #include #include class Data { public: Data(std::string const& caption) : caption{caption} {} virtual ~Data() = default; virtual int get_point(int N) const = 0; virtual int size() const = 0; std::string get_caption() const { return caption; } protected: std::string caption; }; class File_Table : public Data { public: File_Table(std::string const& caption, std::string const& filename) : Data {caption}, coordinates {} { std::ifstream ifs {filename}; int x {}; int y {}; while(ifs >> x >> y) { coordinates.push_back({x,y}); } } int get_point(int N) const override { for (auto [x,y] : coordinates) { if (x == N) { return y; } } return 0; } int size() const override { return std::max_element(begin(coordinates), end(coordinates))->first; } private: std::vector> coordinates; }; class Sinus_Function : public Data { public: Sinus_Function(std::string const& caption, int x_start, int x_end) : Data{caption}, start{x_start}, end{x_end} {} int get_point(int N) const override { return sin_function(start + N); } int size() const override { return end - start + 1; } private: int sin_function(int x) const { return static_cast(sin(x) * 3 + 10); } int start; int end; }; void plot(Data const& data, char mark = '+', char fill = ' ') { std::cout << "=== Plotting " << data.get_caption() << " ===" << std::endl; for (int i {0}; i <= data.size(); ++i) { int value { data.get_point(i) }; std::cout << std::setfill(fill) << std::setw(value) << mark << std::endl; } } class Plot { public: Plot(char m, char f) : mark{m}, fill{f} {} void operator<<(Data const& d) { plot(d, mark, fill); } private: char mark; char fill; }; int main() { File_Table pd {"Measurements", "data.txt"}; plot(pd, '|', '#'); Sinus_Function sd {"Sinus", -5, 13}; plot(sd); /* Plot barplot{'|', '#'}; barplot << pd; barplot << sd; Plot mathplot{'+', ' '}; mathplot << pd; mathplot << sd; */ }