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
|
// temp_stats.cpp : Defines the entry point for the console application.
// Osman Zakir
// 2 / 12 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 10 Exercise 3
// Exercise Specifications:
/**
* Write a program that reads the data from raw_temps.txt created in exercise
* 2 into a vector and then calculates the mean and median temperatures
* in your data set. Call this program temp_stats.cpp.
*/
#include "../../std_lib_facilities.h"
struct Reading
{
int hour;
double temperature;
bool operator < (const Reading &reading) const;
bool operator > (const Reading &reading) const;
Reading &operator=(const Reading &reading);
friend bool operator==(const Reading &r1, const Reading &r2);
friend bool operator!=(const Reading &r1, const Reading &r2);
};
void fill_vector(ifstream &ist, vector<Reading> &temps);
double calc_mean(const vector<Reading> &temps);
double calc_median(const vector<Reading> &temps);
void print_data(const double mean, const double median);
int main()
{
cout << "Please enter name of input file: ";
string iname;
cin >> iname;
ifstream ist{ iname };
try
{
if (!ist)
{
error("can't open input file ", iname);
}
}
catch (const runtime_error &e)
{
cerr << "error: " << e.what() << '\n';
}
vector<Reading> temps;
fill_vector(ist, temps);
double mean = calc_mean(temps);
double median = calc_median(temps);
print_data(mean, median);
keep_window_open();
}
void fill_vector(ifstream &ist, vector<Reading> &temps)
{
int hour;
double temperature;
while (ist >> hour >> temperature)
{
try
{
if (hour < 0 || hour > 23)
{
error("hour out of range");
}
temps.push_back(Reading{ hour, temperature });
}
catch (const runtime_error &e)
{
cerr << "error: " << e.what() << '\n';
}
}
}
double calc_mean(const vector<Reading> &temps)
{
double sum = 0;
for (const auto &temp : temps)
{
sum += temp.temperature;
}
double mean = sum / temps.size();
return mean;
}
double calc_median(const vector<Reading> &temps)
{
double median = 0;
sort(temps.begin(), temps.end());
if (temps.size() % 2 != 0)
{
median = temps[temps.size() / 2].temperature;
}
else
{
long double middle = temps.size() / 2;
median = double(temps[middle + 1].temperature);
}
return median;
}
void print_data(const double mean, const double median)
{
cout << "The mean is " << mean << " and the median is " << median << '\n';
}
bool Reading::operator < (const Reading &reading) const
{
return reading.temperature < temperature;
}
bool Reading::operator > (const Reading &reading) const
{
return reading.temperature > temperature;
}
bool operator==(const Reading &r1, const Reading &r2)
{
return r1.hour == r2.hour &&
r1.temperature == r2.temperature;
}
bool operator!=(const Reading &r1, const Reading &r2)
{
return !(r1 == r2);
}
Reading &Reading::operator=(const Reading &reading)
{
hour = reading.hour;
temperature = reading.temperature;
return *this;
}
|