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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
// temp_readings.cpp : Defines the entry point for the console application.
// Osman Zakir
// 2 / 7 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 10 Sections 10.1.1 to 10.1.3
// This program takes temperature readings from an input file and prints it to
// output file. The readings are represented in the form of readings for the
// year, month and hour. Structs are used to represent Readings, Days, and Months.
#include "../../std_lib_facilities.h"
// not_a_reading == less than absolute zero
const int not_a_reading = -7777;
const int not_a_month = -1;
struct Day
{
vector<double> hour{ vector<double>(24, not_a_reading) };
};
// a month of temperature readings
struct Month
{
// [0:11] January is 0
int month{ not_a_month };
// [1:31] one vector of readings per day
vector<Day> day{ 32 };
};
// a year of temperature readings, organized by month
struct Year
{
// positive == A.D.
int year;
// [0:11] January is 0
vector<Month> month{ 12 };
};
struct Reading
{
int day;
int hour;
double temperature;
};
istream& operator >> (istream &is, Reading &r);
istream& operator >> (istream &is, Month &m);
istream& operator >> (istream &is, Year &y);
string int_to_month(int i);
int month_to_int(const string &s);
bool is_valid(const Reading &r);
void end_of_loop(istream &is, char term, const string &message);
void print_year(ofstream &ofs, const Year &y);
int main()
{
try
{
// open an input file:
cout << "Please enter input file name\n";
string iname;
cin >> iname;
ifstream ist{ iname };
if (!ist)
{
error("can't open input file ", iname);
}
ist.exceptions(ist.exceptions() | ios_base::badbit);
// open an output file:
cout << "Please enter output file name\n";
string oname;
cin >> oname;
ofstream ost{ oname };
if (!ost)
{
error("can't open output file ", oname);
}
// read an arbitrary number of years:
vector<Year> ys;
while (true)
{
// get a freshly initialized Year each time around
Year y;
if (!(ist >> y))
{
break;
}
ys.push_back(y);
}
cout << "read " << ys.size() << " years of readings\n";
for (const Year &y : ys)
{
print_year(ost, y);
}
}
catch (const runtime_error &e)
{
cerr << "error: " << e.what() << '\n';
keep_window_open();
return 1;
}
catch (const exception &e)
{
cerr << "error: " << e.what() << '\n';
keep_window_open();
return 2;
}
keep_window_open();
return 0;
}
istream& operator>>(istream &is, Reading &r)
// read a temperature reading from is into r
// format: ( 3 4 9.7 )
// check format, but don’t bother with data validity
{
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 != ')')
{
// messed-up reading
error("bad reading");
}
r.day = d;
r.hour = h;
r.temperature = t;
return is;
}
istream& operator>>(istream &is, Month &m)
// read a month from is into m
// format: { month feb . . . }
{
char ch = 0;
if (is >> ch && ch != '{')
{
is.unget();
// we failed to read a Month
is.clear(ios_base::failbit);
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;
}
istream& operator>>(istream &is, Year &y)
// read a year from is into y
// format: { year 1972 . . . }
{
char ch;
is >> ch;
if (ch != '{')
{
is.unget();
is.clear(ios_base::failbit);
return is;
}
string year_marker;
int yy;
is >> year_marker >> yy;
if (!is || year_marker != "year")
{
error("bad start of year");
}
y.year = yy;
while (true)
{
// get a clean m each time around
Month m;
if (!(is >> m))
{
break;
}
y.month[m.month] = m;
}
end_of_loop(is, '}', "bad end of year");
return is;
}
int month_to_int(const string &s)
// is s the name of a month? If so return its index [0:11] otherwise – 1
{
vector<string> month_input_tbl =
{"jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec"};
for (int i = 0; i < 12; ++i)
{
if (month_input_tbl[i] == s)
{
return i;
}
}
return -1;
}
string int_to_month(int i)
// months [0:11]
{
vector<string> month_print_tbl =
{ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
if (i < 0 || i >= 12)
{
error("bad month index");
}
return month_print_tbl[i];
}
bool is_valid(const Reading &r)
// a rough test
{
constexpr int implausible_min = -200;
constexpr int implausible_max = 200;
if (r.day < 1 || r.day > 31)
{
return false;
}
if (r.hour < 0 || r.hour > 23)
{
return false;
}
if (r.temperature < implausible_min || r.temperature > implausible_max)
{
return false;
}
return true;
}
void end_of_loop(istream &ist, char term, const string &message)
{
// use term as terminator and/or separator
if (ist.fail())
{
ist.clear();
char ch;
if (ist >> ch && ch == term)
{
// all is fine
return;
}
error(message);
}
}
void print_year(ofstream &ofs, const Year &y)
{
for (unsigned i = 0; i < y.month.size(); ++i)
{
ofs << y.year << ": " << y.month[i].day[i].hour[i] << '\n';
}
}
|