Question: A temperature file (e.g., temps.dat) A query file (e.g., queries.dat) This file contains a list of queries (i.e., requests for information) that your program should
A temperature file (e.g., temps.dat)
A query file (e.g., queries.dat) This file contains a list of queries (i.e., requests for information) that your program should calculate based on the data in file temps.dat. Each line in the file contains one query. There are two types of queries:
AVG: given a location (specified as a station id) and a range of years (specified as two integers, for example, 2000 2006), it computes the average temperature for the location and period. If no data is available for the location/period, the answer will be the string unknown.
MODE: given a location and a range of years, it identifies the most common rounded temperature value in the period.
If no data is available for the period, the expected result is the string unknown.
If more than one mode, you should return the one with the highest value.
Notice that the temperature values in file temps.dat are double numbers. You should round these values to the nearest integer before computing the mode. For example, 8.03, 8.1, and 8.5 are all rounded to 8; 8.51 and 8.8 are rounded to 9. So with this set of data, there are three eights and two nines. And the mode would be 8.
Your program should read these two files and generate a file result.dat with the queries and their results. Regardless,
temps.dat might look like this:
411048 2015 1 9.58 411048 2015 3 14.82 411048 2016 4 20.51 411048 2016 1 10.99 411000 1973 1 0.54 411048 2016 3 18.40 411048 2016 5 -99.99
queries.dat might look like this:
411048 AVG 2015 2016 411138 AVG 1995 1995 411048 MODE 2015 2016
Then your program is expected to produce a file named result.dat with the following content:
411048 2015 2016 AVG 14.86 411138 1995 1995 AVG unknown 411048 2015 2016 MODE 21
Requirements
Start with the code you completed for Part 1.
Your program should receive the names of the input files as command line arguments. The first argument will be the temperature file, the second the queries file.
If the input files contain a line with an invalid format, you should output on the console an error message and finish executing the program.
Do not throw an exception.
Output the message in the console beginning Error: followed by the description shown below followed by the invalid value. For example:
Data File
Error: Invalid temperature -1221.11
Error: Invalid year 2020
Error: Invalid month 0
Error: Unable to open input.dat
Error: Other invalid input
Query File
Error: Invalid range 1996-1995
Error: Unsupported query MAX
Error: Other invalid query
Note: You can have an invalid year in the query so that error can occur here too.
Invalid years will be part of an invalid range.
Error: Invalid range 1776-2025
Examples of lines with invalid format for queries.dat: AVG 2015 2016 411138 AVG 1996 1995 411048 MODE 1500 2016 411048 MAX 2015 2016
The output file created by your program should be named result.dat.
Here is my code thus far after have working on it for a while:
TemperatureDatabase.h
#ifndef TEMP_DB
#define TEMP_DB
#include
#include "LinkedList.h"
class TemperatureDatabase {
public:
TemperatureDatabase();
~TemperatureDatabase();
// The two functions below are required
// Read the temperature records from the data file and populate the linked list
// Implement for Part 1
void loadData(const std::string& data_file);
// Read the queries from the query file and perform a series of queries
// Implement for Part 2
//void performQuery(const std::string& query_filename);
double getAVG(LinkedList&, int, int);
double getMODE(LinkedList&, int, int);
private:
// Linked list to store the temperature records. You need to properly populate
// this link list.
LinkedList records;
// You can add any private member variables/functions you feel useful in this class.
};
#endif // TEMP_DB
TemperatureDatabase.cpp
#include "TemperatureDatabase.h"
#include
#include
#include
#include
#include
#include
#include
using namespace std;
// Default constructor/destructor. Modify them if you need to.
TemperatureDatabase::TemperatureDatabase() {}
TemperatureDatabase::~TemperatureDatabase() {}
void TemperatureDatabase::loadData(const string& filename) {
ifstream inFS("temps.dat");
if (!inFS.is_open()) {
cout << "Could not open file." << endl;
}
else {
while (!inFS.eof()) {
try {
int year;
int month;
int location;
double temp;
inFS >> location >> year >> month >> temp;
if (location > 99999 && location < 1000000) {
if (year > 1799 && year < 2018) {
if (month > 0 && month < 13) {
if (temp > -51 && temp < 51) {
records.insert(location, year, month, temp);
}
else
throw runtime_error("Temp out of range");
}
else
throw runtime_error("Month of of range");
}
else
throw runtime_error("Year out of range");
}
}
catch (const runtime_error& e) {
cout << e.what() << endl;
}
}
}
inFS.close();
return;
}
}
void TemperatureDatabase::performQuery(const string& filename) {
ifstream inFS("queries.dat");
while (!inS.eof()) {
try {
string mode;
int location, first_year, second_year;
inS >> location >> mode >> first_year >> secod_year;
if (!(location > 99999 && location < 999999)) {
throw runtime_error("Location out of range");
}
if ( !(second_year > 1799 && second_year < 2019) || !(first_year > 1799 && start < 2018) || (second_year < start)) {
throw runtime_error("years out of range");
}
if ((mode == "AVG" || mode == "MODE") && check) {
if (mode == "AVG") {
findAVG(loca, start, end);
}
if (mode == "MODE") {
findMode(location, first_year, second_year);
}
}
bool check = records.checkData(location, first_year, second_year);
if (!check) {
if (mode == "AVG") {
queries.insert_Q(location, first_year, second_year, 1, -999);
}
else {
queries.insert_Q(location, first_year, second_year, 2, -999);
}
}
}
catch (const runtime_error& e) {
cout << e.what() << endl;
}
}
}
ofstream outS("results.dat");
outS << this-> queries;
return;
}
TemperatureData.cpp
#include "TemperatureData.h"
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace std;
TemperatureData::TemperatureData() {} //initialize everything
TemperatureData::TemperatureData(std::string id, int year, int month, double temperature) {} //initialize everything
TemperatureData::~TemperatureData() {} // You should not need to implement this
bool TemperatureData::operator<(const TemperatureData& b) {
// Implement this
}
TemperatureData.h
/*
* TemperatureData.h
*
* Created on: Jul 16, 2018
* Author: student
*/
#ifndef TEMPERATUREDATA
#define TEMPERATUREDATA
struct TemperatureData {
// Put data members here
TemperatureData();
TemperatureData(std::string id, int year, int month, double temperature);
virtual ~TemperatureData();
bool operator<(const TemperatureData& b);
};
#endif /* TEMPERATUREDATA */
Node.h
#ifndef NODE
#define NODE
#include "TemperatureData.h"
struct Node {
TemperatureData data;
Node* next;
std::string id;
int month;
int year;
double temperature;
// Default constructor
Node(); // remember to initialize next to nullptr
Node(std::string id, int year, int month, double temperature); // remember to initialize next to nullptr
// This operator will allow you to just ask if a node is smaller than another
// rather than looking at all of the location, temperature and date information
bool operator<(const Node& b);
// The function below is written. Do not modify it
virtual ~Node() {}
};
#endif
Node.cpp
#include
#include "Node.h"
using namespace std;
// Default constructor
Node::Node():next(nullptr) {} // remember to initialize next to nullptr
// Parameterized constructor
Node::Node(string id, int year, int month, double temperature): id(id), year(year), month(month), temperature(temperature), next(nullptr) {}
// remember to initialize next to nullptr
bool Node::operator<(const Node& b) {
return this->data < b.data;
if (this->id > b.id) { return false; }
else if (this->id < b.id) {
return true;
else {
if (this->year < b.year) {
return true;
}
else if (this->year > b.year) {
return false;
}
else {
if (this->month > b.month) {
return false;
}
else if (this->month < b.month) {
return true;
}
else {
return false;
}
}
}
}
LinkedList.h
#ifndef LINKEDLIST
#define LINKEDLIST
#include
#include
#include "Node.h"
class LinkedList {
private:
Node* head;
Node* tail;
public:
// Default constructor
LinkedList();
// Destructor
~LinkedList();
// Copy constructor
LinkedList(const LinkedList& other);
// Copy assignment
LinkedList& operator=(const LinkedList& other);
// Insert a record to the linked list
void insert(std::string location, int year, int month, double temperature);
// Clear the content of this linked list
void clear();
// The functions below are written already. Do not modify them.
std::string print() const;
Node* getHead() const;
};
std::ostream& operator<<(std::ostream& os, const LinkedList& ll);
#endif
LinkedList.cpp
#include
#include
#include "LinkedList.h"
#include "Node.h"
using namespace std;
LinkedList::LinkedList() {
head = nullptr;
tail = nullptr;
// Implement this function
}
LinkedList::~LinkedList() {
// Implement this function
clear();
}
LinkedList::LinkedList(const LinkedList& source) {
Node* previous = source.head;
this->head = nullptr;
if (previous != nullptr)
{
Node* node = new Node;
node->location = previous->location;
node->year = previous->year;
node->month = previous->month;
node->temperature = previous->temperature;
node->query = previous->query;
node->next = previous->next;
this->head = node;
Node* current = this->head;
previous = previous->next;
while (previous != nullptr)
{
node = new Node;
node->location = previous->location;
node->year = previous->year;
node->month = previous->month;
node->temperature = previous->temperature;
node->query = previous->query;
node->next = previous->next;
current->next = node;
current = current->next;
previous = previous->next;
}
}
}
LinkedList& LinkedList::operator=(const LinkedList& source) {
{
clear();
Node* previous = source.head;
if (previous != nullptr){
newNode = new Node;
newNode->location = oldNode->location;
newNode->year = oldNode->year;
newNode->month = oldNode->month;
newNode->temp = oldNode->temp;
newNode->query = oldNode->query;
newNode->next = oldNode->next;
this->head = newNode;
Node* curr = this->head;
oldNode = oldNode->next;
while (oldNode != NULL) {
newNode = new Node;
newNode->location = oldNode->location;
newNode->year = oldNode->year;
newNode->month = oldNode->month;
newNode->temp = oldNode->temp;
newNode->query = oldNode->query;
newNode->next = oldNode->next;
curr->next = newNode;
curr = curr->next;
oldNode = oldNode->next;
}
}
return *this;
}
void LinkedList::insert(string location, int year, int month, double temperature) {
Node* node = new Node(location, year, month, temperature);
Node* current = head;
Node* previous = nullptr;
if (this->head == nullptr && this->tail == nullptr)
{
head = node;
tail = node;
return;
}
else {
current->next = temp;
tail = temp;
temp->next = nullptr;
}
}
void LinkedList::clear() {
Node* current = this->head;
while (current != NULL) {
Node* deleteNode = current;
current = current->next;
delete deleteNode;
}
return;
}
Node* LinkedList::getHead() const {
return this-> head;
}
string LinkedList::print() const {
string outputString;
cout << outputString;
return outputString;
}
ostream& operator<<(ostream& os, const LinkedList& ll) {
/* Do not modify this function */
os << ll.print();
return os;
}
main.cpp
#include "TemperatureDatabase.h"
#include
using namespace std;
int main(int argc, char** argv) {
if (argc < 3) {
cout << "Usage: " << argv[0] << " data_file query_file" << endl;
return 1;
} else {
TemperatureDatabase database;
database.loadData(argv[1]);
database.performQuery(argv[2]);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
