1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// Osman Zakir
// 6 / 22 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 15 Example code
// Japanese Ages Graphing Example
#include "../../Graph.h"
#include "../../Window.h"
#include "../../Simple_window.h"
#include <iostream>
#include <fstream>
struct Distribution
{
int year, young, middle, old;
};
class Scale
// data value to coordinate conversion
{
int cbase; // coordinate base
int vbase; // base of values
double scale;
public:
Scale(const int b, const int vb, const double s) : cbase{ b }, vbase{ vb }, scale{ s } { }
int operator()(int v) const { return cbase + (v - vbase) * scale; }
};
std::istream &operator>>(std::istream &is, Distribution &d);
int main()
{
using namespace std;
using namespace Graph_lib;
try
{
constexpr int xmax = 600; // window size
constexpr int ymax = 400;
constexpr int xoffset = 100; // distance from left-hand side of window to y axis
constexpr int yoffset = 60; // distance from bottom of window to x axis
constexpr int xspace = 40; // space beyond axis
constexpr int yspace = 40;
constexpr int xlength = xmax - xoffset - xspace; // length of axes
constexpr int ylength = ymax - yoffset - yspace;
constexpr int base_year = 1960;
constexpr int end_year = 2040;
constexpr double xscale = double(xlength) / (end_year - base_year);
constexpr double yscale = double(ylength) / 100;
Scale xs{ xoffset, base_year, xscale };
Scale ys{ ymax - yoffset, 0, -yscale };
Graph_lib::Window win{ Point{100, 100}, xmax, ymax, "Aging Japan" };
Axis x{ Axis::x, Point{xoffset, ymax - yoffset}, xlength,
(end_year - base_year) / 10, "year 1960 1970 1980 1990 "
"2000 2010 2020 2030 2040" };
x.label.move(-100, 0);
Axis y{ Axis::y, Point{xoffset, ymax - yoffset}, ylength, 10, "% of population" };
Line current_year{ Point{xs(2008), ys(0)}, Point{xs(2008), ys(100)} };
current_year.set_style(Line_style::dash);
string file_name = "japanese-age-data.txt";
ifstream ifs{ file_name };
if (!ifs)
{
error("can't open ", file_name);
}
Open_polyline children;
Open_polyline adults;
Open_polyline aged;
for (Distribution d; ifs >> d;)
{
if (d.year < base_year || d.year > end_year)
{
error("year out of range");
}
if (d.young + d.middle + d.old != 100)
{
error("percentages don't add up");
}
const int x = xs(d.year);
children.add(Point{ x, ys(d.young) });
adults.add(Point{ x, ys(d.middle) });
aged.add(Point{ x, ys(d.old) });
}
Text children_label{ Point{20, children.point(0).y}, "age 0-14" };
children.set_color(Color::red);
children_label.set_color(Color::red);
Text adults_label{ Point{20, adults.point(0).y}, "age 15-64" };
adults.set_color(Color::blue);
adults_label.set_color(Color::blue);
Text aged_label{ Point{20, aged.point(0).y}, "age 65+" };
aged.set_color(Color::dark_green);
aged_label.set_color(Color::dark_green);
win.attach(children);
win.attach(adults);
win.attach(aged);
win.attach(children_label);
win.attach(adults_label);
win.attach(aged_label);
win.attach(x);
win.attach(y);
win.attach(current_year);
gui_main();
}
catch (const runtime_error &rte)
{
constexpr int xmax = 600; // window size
constexpr int ymax = 400;
Graph_lib::Window win{ Point{ 100, 100 }, xmax, ymax, "Aging Japan" };
Text err_msg_start{ Point{(xmax / 2) - 100, (ymax / 2)}, "Runtime error: " };
std::size_t err_msg_start_length = err_msg_start.label().length();
Text err_msg{ Point{(xmax / 2) + err_msg_start_length - 10, (ymax / 2)}, rte.what() };
err_msg_start.set_color(Color::black);
err_msg.set_color(Color::black);
win.attach(err_msg_start);
win.attach(err_msg);
gui_main();
}
}
std::istream &operator>>(std::istream &is, Distribution &d)
// assume format: (year: young middle old)
{
char ch1 = 0;
char ch2 = 0;
char ch3 = 0;
Distribution dd;
if (is >> ch1 >> dd.year >> ch2 >> dd.young >> dd.middle >> dd.old >> ch3)
{
if (ch1 != '(' || ch2 != ':' || ch3 != ')')
{
is.clear(std::ios_base::failbit);
return is;
}
}
else
{
return is;
}
d = dd;
return is;
}
|