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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | // Code derived from Stroustrup's PPP2 book
// § 10.11.2 Reading structured values
// -and beginning on p 370
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
void error(const string s) { throw runtime_error(s); }
void error(const string s1, const string s2) { error(s1 + s2); }
void error(const string& s, int i)
{
ostringstream os;
os << s << ": " << i;
error(os.str());
}
//------------------------------------------------------------------------------
struct Reading {
int day;
int hour;
double temperature;
};
// read a temperature reading from is into r
// format: ( 3 4 9.7 )
// check format, but don’t bother with data validity
istream& operator>>(istream& is, Reading& r)
{
char ch1;
if (is >> ch1 && ch1 != '(') { // could it be a Reading?
is.unget();
is.clear(ios_base::failbit);
return is;
}
char ch2;
int d;
int h;
double t;
is >> d >> h >> t >> ch2;
if (! is || ch2 != ')')
error("bad reading"); // messed-up reading
r.day = d;
r.hour = h;
r.temperature = t;
return is;
}
constexpr int implausible_min = -200;
constexpr int implausible_max = 200;
// a rough test
bool is_valid(const Reading& r)
{
if (r.day < 1 || 31 < r.day)
return false;
if (r.hour < 0 || 23 < r.hour)
return false;
if (r.temperature < implausible_min || implausible_max < r.temperature)
return false;
return true;
}
//------------------------------------------------------------------------------
const int not_a_reading = -7777; // less than absolute zero
const int not_a_month = -1;
// a day of temperature readings, organized by hour
struct Day {
vector<double> hour{vector<double>(24, not_a_reading)};
};
// a month of temperature readings, organized by day
struct Month {
int month{not_a_month}; // [0:11] January is 0
vector<Day> day{32}; // [1:31] one vector of readings per day
};
// a year of temperature readings, organized by month
struct Year {
int year; // positive == A.D.
vector<Month> month{12}; // [0:11] January is 0
};
//------------------------------------------------------------------------------
int month_to_int(string s);
void end_of_loop(istream& ist, char term, const string& message);
//------------------------------------------------------------------------------
// read a month from is into m
// format: { month feb . . . }
istream& operator>>(istream& is, Month& m)
{
char ch = 0;
if (is >> ch && ch != '{') {
is.unget();
is.clear(ios_base::failbit); // we failed to read a Month
return is;
}
string month_marker;
string mm;
is >> month_marker >> mm;
if (! is || month_marker != "month")
error("bad start of month");
m.month = month_to_int(mm);
int duplicates = 0;
int invalids = 0;
for (Reading r; is >> r;) {
if (is_valid(r)) {
if (m.day[r.day].hour[r.hour] != not_a_reading)
++duplicates;
m.day[r.day].hour[r.hour] = r.temperature;
} else
++invalids;
}
if (invalids)
error("invalid readings in month ", invalids);
if (duplicates)
error("duplicate readings in month ", duplicates);
end_of_loop(is, '}', "bad end of month");
return is;
}
//------------------------------------------------------------------------------
// read a year from is into y
// format: { year 1972 . . . }
istream& operator>>(istream& is, Year& y)
{
char ch;
is >> ch;
if (ch != '{') {
is.unget();
is.clear(ios::failbit);
return is;
}
string year_marker;
int yy;
is >> year_marker >> yy;
if (! is || year_marker != "year") {
if (is.eof())
return is; // all data is parsed (eof)
else
error("bad start of year");
}
y.year = yy;
while (true) {
Month m; // get a clean m each time around
if (! (is >> m))
break;
y.month[m.month] = m;
}
end_of_loop(is, '}', "bad end of year");
return is;
}
//------------------------------------------------------------------------------
void end_of_loop(istream& ist, char term, const string& message)
{
if (ist.fail()) { // use term as terminator and/or separator
ist.clear();
char ch;
if (ist >> ch && ch == term)
return; // all is fine
error(message);
}
}
//------------------------------------------------------------------------------
vector<string> month_input_tbl = {"jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec"};
// is s the name of a month? If so return its index [0:11] otherwise –1
int month_to_int(string s)
{
for (int i = 0; i < 12; ++i)
if (month_input_tbl[i] == s)
return i;
return -1;
}
vector<string> month_print_tbl = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
// months [0:11]
string int_to_month(int i)
{
if (i < 0 || 12 <= i)
error("bad month index");
return month_print_tbl[i];
}
//------------------------------------------------------------------------------
void print_year(ostream& ost, const Year& y)
{
ost << "\nYEAR: " << y.year << '\n';
for (const auto& m : y.month) {
if (m.month != not_a_month) {
ost << " MONTH: " << int_to_month(m.month) << '\n';
for (int day = 1; day < (int)m.day.size(); ++day) {
for (int hour = 0; hour < (int)m.day[day].hour.size(); ++hour) {
auto temp = m.day[day].hour[hour];
if (temp != not_a_reading)
ost << " (" << day //
<< ", " << hour //
<< ", " << temp //
<< ")\n";
}
}
}
}
}
//------------------------------------------------------------------------------
int main()
try {
// open an input file:
cout << "Please enter input file name (eg, odd_format.txt ): ";
string iname;
cin >> iname;
ifstream ist{iname};
if (! ist)
error("can't open input file ", iname);
ist.exceptions(ist.exceptions() | ios_base::badbit); // throw for bad()
// open an output file:
cout << "Please enter output file name: ";
string oname;
cin >> oname;
ofstream ost{oname};
if (! ost)
error("can't open output file ", oname);
// read an arbitrary number of years from input stream:
vector<Year> ys;
while (true) {
Year y; // get a freshly initialized Year each time around
if (! (ist >> y))
break;
ys.push_back(y);
}
// write formatted data to output stream:
cout << "read " << ys.size() << " years of readings\n";
for (Year& y : ys)
print_year(ost, y);
} catch (exception& e) {
cerr << "error: " << e.what() << '\n';
return 1;
} catch (...) {
cerr << "Oops: unknown exception!\n";
return 2;
}
|
build & run:
g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p370.cpp && ./a.out
Prev • Up • Next
Bonus: version 2