Write in C++ The National Weather Service has come to you to write a program tha
ID: 3715899 • Letter: W
Question
Write in C++
The National Weather Service has come to you to write a program that analyzes their weather data stored in large files. Your program will input a file, analyze the weather data, and output the data in human-readable format.
This is a great opportunity for you to put together your 8.5" x 11" cheat sheet, and before you ask, no, "See Answer" will not be enabled. Instead, it is a good idea to look up the solutions to ensure you actually understand the material.
Step 1:
Write your include files here.
You need to support:
cout and cin
String Streams
File Streams
Strings
Vectors
I/O manipulators (#include<iomanip>)
Finally, write your using statement (using namespace std;)
Step 2:
Write a structure named WeatherData that contains the following fields (in this order):
Integer day
Integer month
Integer year
Integer clouds
Double temp
Double wind
String watch
Step 3:
Write a class named WeatherStation that contains the following members:
A private vector of WeatherData named m_data
A public 0-argument constructor
A public member function called getNumData that returns an integer and takes no arguments
A public member function called getData that returns a WeatherData structure and takes an integer argument
A public member function called sortData that returns nothing and takes nothing
A public member function called addData that returns nothing and takes a constant by-reference WeatherDatastructure.
Code has been written:
int main()
{
string line;
string file_name;
cin >> file_name;
//Weather variables
int day, month, year, clouds;
double wind, temp;
string watch;
Step 4:
Declare a WeatherStation called station
Step 5:
Declare an empty, input string stream named sin
Step 6:
Declare an input file stream named fin.
Step 7:
Open the file given by the string named file_name declared above using fin
Step 8:
If the file was not opened successfully, output the text "Could not open file. " and return the error code 1 (one).
Step 9:
Now you will need to read the file line-by-line. Using the string line above, write a while loop that grabs an entire line from fin and loops until the input is exhausted.
NOTE You will be finished with this block when you type your opening brace '{'
Step 10:
You are inside of the while loop and you have put a line of data into the string line. Now, put this line into the string stream sin, but remember that this is inside of a loop and sin will be changing strings every iteration.
Step 11:
Using the string stream sin, extract the fields needed to fill the WeatherData structure. The file is in the same order as the structure (i.e. day is first, month is second, and watch is the very last field).
Note: watch may have multiple words, so you cannot simply use the extractors here >>
Note: Use the variables day, month, year, etc that we declared for you, above.
Step 12:
Now, you should have good data to fill a WeatherData structure. Declare a new WeatherData structure called data and set its fields to what you just extracted above.
Step 13:
Using the appropriate member function of your variable station, add the weather data to the weather station.
written code:
Step 14:
Using the appropriate member function, sort the weather station's data.
Step 15:
Step 16:
Write the 0-argument constructor for WeatherStation. Simply leave the body empty.
Step 17:
Write the definition for the member function getNumData. This will return the size of the vector m_data.
Step 18:
Write the definition for the member function getNumData. This will return the size of the vector m_data.
Step 19:
Write the definition for the member function addData. This will simply add the parameter to the m_datavector.
Step 20:
Write the member function definition for the function sortData. As the name implies, this will sort the m_datavector date (year, month, and day) in descending order. Therefore, the most recent WeatherData structure should be at the very top of the vector.
You may use any sorting algorithm
Explanation / Answer
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <algorithm>
using namespace std;
struct WeatherData{
int day;
int month;
int year;
int clouds;
double temp;
double wind;
string watch;
};
bool compareDay(struct WeatherData w1, struct WeatherData w2) {
return w1.year > w2.year ||
w1.year == w2.year && w1.month > w2.month ||
w1.year == w2.year && w1.month > w2.month && w1.day > w2.day;
}
class WeatherStation {
public: vector<WeatherData> m_data;
public: WeatherStation() { }
public: int getNumData() {
return m_data.size();
}
public: WeatherData getData(int data) {
return m_data[data];
}
public: void sortData() {
sort(m_data.begin(), m_data.end(), compareDay);
}
public: void addData(struct WeatherData data) {
m_data.push_back(data);
}
};
int main(int argc, char const *argv[]) {
string line;
string file_name;
int day, month, year, clouds;
double temp, wind;
string watch;
cin >> file_name;
WeatherStation station;
ifstream fin;
fin.open(file_name);
if (!fin.is_open()) {
cout << "Could not open file. ";
return(1);
}
while(getline(fin, line)) {
stringstream ss(line);
ss >> day >> month >> year >> clouds >> temp >> wind >> watch;
struct WeatherData data;
data.day = day;
data.month = month;
data.year = year;
data.clouds = clouds;
data.temp = temp;
data.wind = wind;
data.watch = watch;
station.addData(data);
station.sortData();
}
for (int i = 0;i < station.getNumData();i++) {
WeatherData data_n = station.getData(i);
cout << "Weather data for " << data_n.month << "/" << data_n.day << "/" << data_n.year << ": Temp: " << fixed << setprecision(2) << data_n.temp << ", Wind: " << setprecision(3) << data_n.wind << ", Clouds: " << data_n.clouds << endl;
cout << "Notes: " << setw(28) << right << data_n.watch << endl << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.