#include "WeatherDataStatistics.h"
int main()
{
WeatherDataStatistics weatherDataStatistics;
weatherDataStatistics.ReadDataFromUser();
weatherDataStatistics.Calculate();
weatherDataStatistics.Print();
return 0;
}
#pragma once
#include "Months.h"
#include "WeatherDataSummary.h"
#include <string>
class WeatherDataStatistics
{
public:
WeatherDataStatistics();
void ReadDataFromUser();
void Calculate();
void Print() const;
private:
WeatherData monthlyData[nMonths];
WeatherData monthlyAverage;
WeatherDataSummary yearlySummary;
double ReadValidated(const std::string & message, double min, double max) const;
void Print(const WeatherData & data) const;
};
#include "WeatherDataStatistics.h"
#include <iostream>
WeatherDataStatistics::WeatherDataStatistics() :
monthlyData(),
monthlyAverage(),
yearlySummary()
{
}
double WeatherDataStatistics::ReadValidated(const std::string & message, double min, double max) const
{
double value;
do
{
std::cout << message << " ([" << min << ", " << max << "]): ";
std::cin >> value;
} while (value < min || value > max);
return value;
}
void WeatherDataStatistics::ReadDataFromUser()
{
for (int i = 0; i < nMonths; i++)
{
std::cout << "Enter the data for " << monthNames[i] << "." << std::endl;
monthlyData[i].total_rainfall = ReadValidated("Enter the total rainfall", 0, 100000); // TODO: you can come up with a reasonable number here. Google search puts the highest average rainfall around 12000 m
monthlyData[i].high_temp = ReadValidated("Enter the highest temperature", -140, 140);
monthlyData[i].low_temp = ReadValidated("Enter the lowest temperature", -140, 140);
monthlyData[i].avg_temp = ReadValidated("Enter the average temperature", -140, 140);
std::cout << std::endl;
}
}
void WeatherDataStatistics::Calculate()
{
monthlyAverage = monthlyData[0];
yearlySummary = monthlyData[0];
for (int i = 1; i < nMonths; i++) // we've already "processed" the 1st month
{
yearlySummary.total_rainfall += monthlyData[i].total_rainfall;
if (yearlySummary.high_temp < monthlyData[i].high_temp)
{
yearlySummary.high_temp = monthlyData[i].high_temp;
yearlySummary.high_temp_month = i;
}
if (yearlySummary.low_temp > monthlyData[i].low_temp)
{
yearlySummary.low_temp = monthlyData[i].low_temp;
yearlySummary.low_temp_month = i;
}
yearlySummary.avg_temp += monthlyData[i].avg_temp;
monthlyAverage.total_rainfall += monthlyData[i].total_rainfall;
monthlyAverage.high_temp += monthlyData[i].high_temp;
monthlyAverage.low_temp += monthlyData[i].low_temp;
monthlyAverage.avg_temp += monthlyData[i].avg_temp;
}
yearlySummary.avg_temp /= 12;
monthlyAverage.total_rainfall /= 12;
monthlyAverage.high_temp /= 12;
monthlyAverage.low_temp /= 12;
monthlyAverage.avg_temp /= 12;
}
void WeatherDataStatistics::Print() const
{
std::cout << "Montly average (for a full year):" << std::endl;
Print(monthlyAverage);
std::cout << std::endl << "Yearly summary:" << std::endl;
Print(yearlySummary);
}
void WeatherDataStatistics::Print(const WeatherData & data) const
{
std::cout << "Total rainfall: " << data.total_rainfall << " mm" << std::endl;
std::cout << "Highest temperature: " << data.high_temp << " C" << std::endl;
std::cout << "Lowest temperature: " << data.low_temp << " C" << std::endl;
std::cout << "Average temperature: " << data.avg_temp << " C" << std::endl;
}
#pragma once
struct WeatherData
{
double total_rainfall;
double high_temp;
double low_temp;
double avg_temp;
WeatherData();
virtual void Print() const;
};
#include "WeatherData.h"
#include <iostream>
WeatherData::WeatherData() :
total_rainfall(0.0),
high_temp(0.0),
low_temp(0.0),
avg_temp(0.0)
{
}
void WeatherData::Print() const
{
std::cout << "Total rainfall: " << total_rainfall << " mm" << std::endl;
std::cout << "Highest temperature: " << high_temp << " C" << std::endl;
std::cout << "Lowest temperature: " << low_temp << " C" << std::endl;
std::cout << "Average temperature: " << avg_temp << " C" << std::endl;
}
#pragma once
#include "WeatherData.h"
struct WeatherDataSummary : public WeatherData
{
int high_temp_month;
int low_temp_month;
WeatherDataSummary();
WeatherDataSummary(const WeatherData & weatherData);
void Print() const override;
};
#include "WeatherDataSummary.h"
#include "Months.h"
#include <iostream>
WeatherDataSummary::WeatherDataSummary() :
WeatherData(),
high_temp_month(0),
low_temp_month(0)
{
}
WeatherDataSummary::WeatherDataSummary(const WeatherData & weatherData)
{
total_rainfall = weatherData.total_rainfall;
high_temp = weatherData.high_temp;
low_temp = weatherData.low_temp;
avg_temp = weatherData.avg_temp;
}
void WeatherDataSummary::Print() const
{
std::cout << "Total rainfall: " << total_rainfall << " mm" << std::endl;
std::cout << "Highest temperature: " << high_temp << " C [" << monthNames[high_temp_month] << "]" << std::endl;
std::cout << "Lowest temperature: " << low_temp << " C [" << monthNames[low_temp_month] << "]" << std::endl;
std::cout << "Average temperature: " << avg_temp << " C" << std::endl;
}
#pragma once
#include <string>
const int nMonths = 12; // Number of Months
const std::string monthNames[nMonths] = { "January", "Februray", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };