#include "date.h" #include "time.h" #include "vector.h" #include "weather.h" #include #include #include #include using namespace std; string FileName = "MetData_Mar01-2014-Mar01-2015-ALL.csv"; // enter the file name of the file here to read the file string OutFileName = "WindTempSolar.csv"; // enter the file name here to write to the file void ReadAllDataFromFile(Vector &WeatherVec,istream &infile); // used to read the record from the file and store in class Vector obj void PrintMenu(); // print the menu options float CalAvgWindSpeed(Vector & Vec,unsigned month, unsigned year); // calculatate the average windspeed of the month and year float CalAvgTemp(Vector & Vec,unsigned month, unsigned year); // calculate the average temperature of the month and year float CalSolarRad(Vector &WeatherVecObj, unsigned Month, unsigned Year); // calculate the solar radiation of the month and year string GetMonthName(unsigned month); // get the string name of the month void ReadVecInputOption1(Vector & WeatherVecObj,Vector &WeatherVec, unsigned Month,unsigned Year); // read the input from the Vector and take relevant record to another Vector for option 1 void ReadVecInputOption2 (Vector &WeatherVec,Vector & WeatherVecObj, unsigned Year); // read the input from the Vector and take relevant record to another Vector for option 2 void ReadVecInputWSolarOption3( Vector & WeatherVec,Vector&WeatherVecObj,unsigned Year);// read the input from the Vector and take relevant record to another Vector for option 3 bool CheckChoice(int &I_Choice, string S_choice); // error checking of user input of the Choice bool CheckYear(unsigned &Year, string S_Year);// error checking of user input of the year bool CheckMonth(unsigned &Month, string S_Month);// error checking of user input of the month void PrintYearRequest(); // prompt for the year void PrintMonthRequest(); // prompt for the month void PrintOption1Output(unsigned Month,unsigned Year,float AverageTemperature,float AverageWindSpeed); // print option 1 to screen void PrintOption2OutPut(unsigned Month, float AverageTemperature, float AverageWindSpeed); // print option 2 output to screen void PrintOption3Output(unsigned Month,float Solar_Kwh_Msqr); // print option 3 output to screen void PrintOption4ToCsv(unsigned Month,float Solar_Kwh_Msqr,float AvgWindSpeed,float AvgTemperature,unsigned Year,ofstream &Outfile); // print option 4 to screen int main () {//----------------------------Initialising and declaration of variable-------------------------- unsigned C_Year=0,C_Month=0; float AvgWindSpeed=0,AvgTemperature=0, Solar_Kwh_Msqr=0; int Choice=0; string S_Choice ="",S_Month ="", S_Year="",IgnoreLine=""; //----------------------------------------------------------------------------------------------- Vector WeatherVec; // create a Vector object of type Weather and name it WeatherVec ifstream infile; // instantiate ifstream to read file infile.open(FileName); // open file through ifstream if(infile.is_open()) { // check if the file is open getline(infile,IgnoreLine,'\n'); // read in the file line of the file which contains the titles ReadAllDataFromFile(WeatherVec,infile); // call the function to store all the record in Vector obj } else { // if file is not opened print error cout<<"Error reading file"<>S_Choice;//Get S_Choice while(CheckChoice(Choice,S_Choice)==false) { // if incorrect user input prompt and get again cout<<"Please enter your choice again 1 - 5"<>S_Choice; } cout< WeatherVecObj; // create a Vector object with Weather Data type and name it WeatherVecObj PrintMonthRequest(); // prompt S_Month cin>>S_Month; //get S_Month while (CheckMonth(C_Month, S_Month)==false) { // if the C_Month is incorrect enter prompt and get again PrintMonthRequest(); cin>>S_Month; } PrintYearRequest(); // prompt for S_Year cin>>S_Year; // get S_Year while (CheckYear(C_Year,S_Year)==false) { // if the C_Year is incorrect enter prompt and get again PrintYearRequest(); cin>>S_Year; } ReadVecInputOption1(WeatherVecObj,WeatherVec, C_Month,C_Year); // get the record from the Vector with conditions and placed into WeatherVecObj AvgWindSpeed= CalAvgWindSpeed(WeatherVecObj,C_Month, C_Year); // calculate average windspeed AvgTemperature= CalAvgTemp(WeatherVecObj,C_Month,C_Year); // calculate average temperature PrintOption1Output(C_Month,C_Year,AvgTemperature,AvgWindSpeed); // print the output to screen WeatherVecObj.~Vector(); // destruct the WeatherVecObj } break; case 2: { Vector WeatherVecObj;// create a Vector object with Weather Data type and name it WeatherVecObj PrintYearRequest();// prompt for S_Year cin>>S_Year;// get S_Year while (CheckYear(C_Year,S_Year)==false) {// if the C_Year is incorrect enter prompt and get again PrintYearRequest(); cin>>S_Year; } ReadVecInputOption2(WeatherVec, WeatherVecObj, C_Year);// get the record from the Vector with conditions and placed into WeatherVecObj for (unsigned i = 0; i<12; i++) // loop 12 times { AvgWindSpeed = CalAvgWindSpeed(WeatherVecObj,i+1, C_Year); // calculate the average windspeed of each month AvgTemperature = CalAvgTemp(WeatherVecObj,i+1, C_Year); // caclualte the average temperature of each month PrintOption2OutPut(i+1,AvgTemperature,AvgWindSpeed); // print the out put for each month } WeatherVecObj.~Vector(); // destruct the weatherVecObj } break; case 3: { Vector WeatherVecObj; // create a Vector object with Weather Data type and name it WeatherVecObj PrintYearRequest();// prompt for S_Year cin>>S_Year;// get S_Year while (CheckYear(C_Year,S_Year)==false) {// if the C_Year is incorrect enter prompt and get again PrintYearRequest(); cin>>S_Year; } ReadVecInputWSolarOption3(WeatherVec,WeatherVecObj,C_Year);// get the record from the Vector with conditions and placed into WeatherVecObj for(unsigned i = 0; i<12; i++) {// loop 12 times Solar_Kwh_Msqr = CalSolarRad(WeatherVecObj, i+1, C_Year); // calculate the solar radiation kwh/m2 for each month of the year PrintOption3Output(i+1,Solar_Kwh_Msqr); // print the output of option 3 } WeatherVecObj.~Vector(); // destruct the WeatherVecObj } break; case 4: { ofstream Outfile; // instantiate the ofstream object to write file Outfile.open(OutFileName); // open WindTempSolar.csv if not found create one Vector WeatherVecObj; // create a Vector object with Weather Data type and name it WeatherVecObj PrintYearRequest();// prompt for S_Year cin>>S_Year;// get S_Year while (CheckYear(C_Year,S_Year)==false) {// if the C_Year is incorrect enter prompt and get again PrintYearRequest(); cin>>S_Year; } ReadVecInputWSolarOption3(WeatherVec,WeatherVecObj,C_Year);// get the record from the Vector with conditions and placed into WeatherVecObj Outfile<>S_Choice; // get S_Choice cout<>S_Choice; } if(Choice==5) {// if the user input is = 5 break from the loop break; } } cout<<"Program ended"< & WeatherVecObj,unsigned month, unsigned year) { // calculating average windspeed for the months of the year float AverageWindSpeed=0.0,ConvertingSpeed=0.0; unsigned I_Month=0,counter=0; for(int i = 0; i>I_Month; // convert string month to unsigned U_Month if(WeatherVecObj[i].GetDate().GetYear()== year && I_Month==month) // if the element year and the month is the same as the specific year and month go in if statement { ConvertingSpeed = WeatherVecObj[i].GetSpeed()*3.6; // the windspeed of the element times 3.6 and assigned to Converting Speed AverageWindSpeed += ConvertingSpeed; // AverageWindSpeed plus adding ConvertingSpeed in the loop counter++; // increment counter by 1 } } if(AverageWindSpeed!=0) { // if avaerage wind speed not equals to 0 AverageWindSpeed = AverageWindSpeed/counter; //AverageWindSpeed is divided by counter AverageWindSpeed = round(AverageWindSpeed*10)/10; // round up to the first decimal point } return AverageWindSpeed;// return the average wind speed of the month of the year } float CalSolarRad(Vector &WeatherVecObj, unsigned Month, unsigned Year) { // calculate solar radiation float Total_Solar_Rad_kWh_Msqr=0.0,Get_Solar_Rad_W_Msqr=0.0,Cal_Solar_Rad_W_Msqr=0.0; unsigned I_Month=0; for(int i = 0; i>I_Month;// converting string month to unsigned I_Month if(WeatherVecObj[i].GetDate().GetYear() == Year && I_Month==Month && WeatherVecObj[i].GetSolarMSqr()>=100) { // if year and month = to specific year AND solar radiation value of the element is more or equal to 100 enter if statement Get_Solar_Rad_W_Msqr = WeatherVecObj[i].GetSolarMSqr();// retrieve solar radiation of the element Cal_Solar_Rad_W_Msqr = Get_Solar_Rad_W_Msqr*1/6; // converting the solar radiation Cal_Solar_Rad_W_Msqr = Cal_Solar_Rad_W_Msqr/1000; // calculating the solar radiation Total_Solar_Rad_kWh_Msqr += Cal_Solar_Rad_W_Msqr; // plus adding the solar radiation to Total_Solar_Rad_kWh_Msqr } } Total_Solar_Rad_kWh_Msqr = round(Total_Solar_Rad_kWh_Msqr*10)/10; // round up the total solar radiation of the month of the year to first decimal place return Total_Solar_Rad_kWh_Msqr; // return the total solar radiation } float CalAvgTemp(Vector & WeatherVecObj,unsigned month, unsigned year) { float ConvertingTemp=0.0,AverageTemperature=0.0; unsigned I_Month,counter=0; for(int i = 0; i>I_Month; // converting string month to unsigned I_Month if(WeatherVecObj[i].GetDate().GetYear() == year && I_Month==month) {// if year and month = to specific year AND Month ConvertingTemp += WeatherVecObj[i].GetTemperature(); // adding to the ConvertingTemp of the temperature of each of the element counter++; // increment counter by 1 every loop } } if(ConvertingTemp!=0) { // if ConvertingTemp not equals to 0 AverageTemperature = ConvertingTemp/counter; // ConvertingTemp divided by counter to get average AverageTemperature = round(AverageTemperature*10)/10; // round up to the nearing tenth } return AverageTemperature; } string GetMonthName(unsigned month) { // return the string name of the month entered in the parameter string Month = ""; if(month == 1) { Month = "January"; return Month; } else if (month == 2) { Month = "February"; return Month; } else if (month == 3) { Month = "March"; return Month; } else if (month == 4) { Month = "April"; return Month; } else if (month == 5) { Month = "May"; return Month; } else if (month == 6) { Month = "June"; return Month; } else if (month == 7) { Month = "July"; return Month; } else if (month == 8) { Month = "August"; return Month; } else if (month == 9) { Month = "September"; return Month; } else if (month == 10) { Month = "October"; return Month; } else if (month == 11) { Month = "November"; return Month; } else if (month == 12) { Month = "December"; return Month; } return Month; } void ReadVecInputOption1(Vector & AddWeatherVec,Vector &WeatherVec, unsigned Month,unsigned Year) { // read the Vector input into new Vector in option 1 unsigned U_Month; float I_Speed,F_Temperature; Weather T_WObj; Date T_DateObj; Time T_TimeObj; for(int i = 0; i>U_Month; // converting string month into unsigned U_Month if(U_Month == Month && WeatherVec[i].GetDate().GetYear() == Year) // if month and year of the element is equal to specific month and year { T_DateObj = WeatherVec[i].GetDate(); //retrieve date of the element T_TimeObj = WeatherVec[i].GetTime(); // retrieve the time of the element I_Speed = WeatherVec[i].GetSpeed(); // retrieve the wind speed of the element F_Temperature = WeatherVec[i].GetTemperature();// retrieve the temperature of the element T_WObj.SetSpeed(I_Speed); // set windspeed to weather obj T_WObj.SetDate(T_DateObj);// set date to weather obj T_WObj.SetTime(T_TimeObj);// set time to weather obj T_WObj.SetTemperature(F_Temperature);// set temeperature to weather obj AddWeatherVec.PushBack(T_WObj); //push weather object into new Vector } } } void ReadVecInputOption2 (Vector &WeatherVec,Vector & AddWeatherVec, unsigned Year) {// read the Vector input into new Vector in option 2 float I_Speed,F_Temperature; Weather T_WObj; Date T_DateObj; Time T_TimeObj; for(int i = 0; i> U_Month; // convert string month to unsigned U_Month if(U_Month>0 && U_Month<13) { // if U_Month is more than 0 and less than 13 set Month and return true Month = U_Month; return true; } else { // else return false return false; } } bool CheckYear(unsigned &Year, string S_Year) {// method to error check the year input unsigned U_Year; istringstream(S_Year)>>U_Year; // convert string month to unsigned U_Year if(U_Year>1000 && U_Year<2021) {// if U_Year is more than 1000 and less than 2021 set Year and return true Year = U_Year; // return true; } else { // if condition not met return false return false; } } void ReadVecInputWSolarOption3( Vector & WeatherVec,Vector &WeatherVecObj,unsigned Year) {// read the Vector input into new Vector in option 3 float I_Speed,F_Temperature,F_Solar_M_Sqr; Weather T_WObj; Date T_DateObj; Time T_TimeObj; for(int i = 0; i &WeatherVec,istream &infile) { //----------------------------------Variable and objects declaration and initialisation----------------------------------- string IgnoreColumn,T_speed,getLine,T_Temperature="",S_Day,S_Month,S_Year,S_Hrs,S_Mins,IgnoreLine,S_Solar_M_Sqr; unsigned Temp_Day=0,Temp_Year=0; float I_Speed=0,F_Temperature=0,F_Solar_M_Sqr=0; int Temp_Hrs=0,Temp_Mins=0; Date T_DateObj; Time T_TimeObj; Weather T_WObj; //---------------------------------------------------------------------------------------------------------------------------- while(!infile.eof()) { // loop till the end of file if(infile.eof()) {// if end of file reach break from loop break; } getline(infile,S_Day,'/');// read data till the delimiter / istringstream(S_Day)>>Temp_Day; // convert string day to unsigned day getline(infile,S_Month,'/');// read data till the delimiter / getline(infile,S_Year,' ');// read data till the delimiter ' ' istringstream (S_Year)>>Temp_Year;// convert string year to unsigned year getline(infile,S_Hrs,':');// read data till the delimiter : istringstream(S_Hrs)>>Temp_Hrs;// convert string hours to int hours getline(infile,S_Mins,',');// read data till the delimiter , istringstream(S_Mins)>>Temp_Mins;// convert string minutes to int minutes T_DateObj.SetDay(Temp_Day);//Set the day to date object T_DateObj T_DateObj.SetMonth(S_Month); // set the month to date object T_DateObj T_DateObj.SetYear(Temp_Year); // set the year to date object T_DateObj T_TimeObj.SetHours(Temp_Hrs); // set the hours to time object T_TimeObj T_TimeObj.SetMins(Temp_Mins); // set the minutes to time object T_TimeObj T_WObj.SetDate(T_DateObj); //set the date T_WObj weather object T_WObj.SetTime(T_TimeObj); // set the time T_WObj weather object for(int i =0; i<9; i++) { // loop 9 times to ignore 9 columns getline(infile,IgnoreColumn,','); } getline(infile,T_speed,','); // read data till the delimiter , istringstream(T_speed)>>I_Speed; // convert string windspeed to float windspeed T_WObj.SetSpeed(I_Speed); getline(infile,S_Solar_M_Sqr,',');// read data till the delimiter , istringstream(S_Solar_M_Sqr)>>F_Solar_M_Sqr; // convert string solar radiation to float solar radiation T_WObj.SetSolarMSqr(F_Solar_M_Sqr); // set the solar radiation to T_WObj weather object for(int i =0; i<5; i++) { // loop 5 times to ignore 5 columns getline(infile,IgnoreColumn,',');// read data till the delimiter , } getline(infile,T_Temperature,'\n');// read data till the delimiter \n if(!T_Temperature.empty()) // check if the temperature string is not empty { istringstream(T_Temperature)>>F_Temperature; // convert the string temperature to float temperature T_WObj.SetTemperature(F_Temperature); // set temperature to T_WObj weather object WeatherVec.PushBack(T_WObj); // push the T_WObj weather object to Vector } T_Temperature = ""; // erase the value of temperature } } bool CheckChoice(int &I_Choice,string S_choice) { // error checking the choice user input int T_Choice; istringstream(S_choice)>>T_Choice;// convert string choice to int choice if(T_Choice>0 && T_Choice <6) { // int choice is more than 0 AND less than 5 I_Choice = T_Choice; // set I_Choice to int T_Choice return true;// return true } else {//if condition not met return false return false; } } #include "date.h" Date::Date() { m_day = 0; // initialise the m_day m_month="Invalid Month";// initialise the m_month m_year = 0; // initialise m_year } Date::~Date() { m_day = 0; // overwrite the m_day m_month="Invalid Month";// overwrite the m_month m_year = 0; // overwrite the m_year } bool Date::SetDay(unsigned day ) { if(day > 0 && day <= 31) { // if day is more than 0 and less or equal to 31 m_day = day; // set m_day to day return true; // return true } else { // condition not met return false return false; } } unsigned Date::GetDay() const { // return value of m_day return m_day; } bool Date::SetMonth(string month) { unsigned U_Month; istringstream(month)>>U_Month; // convert string month to unsigned U_Month if(U_Month>0 && U_Month<13) { // if I_Month more than 0 and less than 13 m_month=month; // set m_month = month return true; } else { // if condition not met return false return false; } } string Date::GetMonth() const { return m_month; // return the value of month of the class } bool Date::SetYear(unsigned year) { if(year>1000 && year<=2020) { // if year more than 1000 AND year less or equal to 2020 m_year = year; // set m_year = year and return true return true; } else {// if condition not met return false return false; } } unsigned Date:: GetYear() const { //return the year of the class return m_year; } #include "weather.h" Weather::Weather() {// initialise the value of members m_Speed = 0; m_Temperature = 0; m_Solar_M_Sqr = 0; } Weather::~Weather() { //// erase the value or overwritting the value of members m_Speed = 0; m_Temperature = 0; m_Solar_M_Sqr = 0; } bool Weather:: SetSpeed( float T_Speed) { // check if T_Speed is more or equal to 0 if(T_Speed >=0 ) { // if yes set member speed to T_speed m_Speed = T_Speed; return true; } else {// if not return false return false; } } void Weather:: SetTemperature( float T_Temperature) { // set m_Temperature to T_Temperature m_Temperature = T_Temperature; } void Weather::SetDate(Date T_DateObj) { // set m_DateObj = Date T_DateObj m_DateObj = T_DateObj; } void Weather::SetTime(Time T_TimeObj) { m_TimeObj = T_TimeObj; } // set m_TimeObj to T_TimeObj float Weather::GetSpeed() const {// return the value of windspeed of the class return m_Speed; } float Weather::GetTemperature() const { return m_Temperature; } void Weather::SetSolarMSqr(float SolarMSqr) { // set m_Solar_M_Sqr = SolarMSqr m_Solar_M_Sqr = SolarMSqr; } float Weather::GetSolarMSqr() { // return the value of m_Solar_M_Sqr return m_Solar_M_Sqr; } Date Weather::GetDate() const { // return the of the Date of the class return m_DateObj; } Time Weather::GetTime() const { // return the m_TimeObj value return m_TimeObj; } #include "time.h" Time::Time() { // inititalise the members of the class m_Hours = 0; m_Mins = 0; } Time::~Time() { // erase or overwrite the value of instantiated Time class m_Hours = 0; m_Mins = 0; } bool Time::SetHours(int Hours) { // set Hours equals to m_Hours if Hour is more or equal to zero AND less than 24 if(Hours >=0 && Hours<24) { m_Hours = Hours; return true; } else {// if condition not met then return false return false; } } bool Time::SetMins(int minutes) { // if minutes more to equal to zero AND minutes less than 60 if (minutes >=0 && minutes <60) {// set member m_Mins to minutes m_Mins = minutes; return true; } else {// return false if condition not met return false; } } int Time::GetHours() const { // return the value of hours of the class return m_Hours; } int Time::GetMins() const {// return the value of minutes of the class return m_Mins; } #ifndef VECTOR_H #define VECTOR_H #include #define DefaultSize 10 //--------------------------------------------------------------- /** *@class Vector *@brief The Vector template that holds the inputted data * * *The Vector Template can be created in with any data type *it is used to hold the inputted data from the file *and store it. * * * * *@author Tan Jun Qi *@version 10 *@date 19/June/2020 Singapore,Singapore * * * */ //--------------------------------------------------------------------------- using namespace std; template class Vector { public: /** *@brief The Default Vector constructor * *This initialise the member or attributes of time class and *allocate memory space in the heap memory for the dynamic array * * *@return void * */ Vector(); /** * *@fn GetSize() * *@brief Function to return the total size of the array * * *return the total size of the array to check if the array *is expanding * * *@return int * */ int GetSize(); /** * *@fn IsEmpty() * *@brief Function to check if the array is empty * * *check if there are any elements inside of the array *return false if there is elements return true if its *empty * *@return bool * */ bool IsEmpty(); /** * *@fn PushBack(T value) * *@brief Function to add in new data into array * * *Add the data into the last element of the array * * * *@return void * */ void PushBack(T value); /** * *@fn PopBack() * *@brief Erase the last element of the array * * *Dis count the last element of the array and enable *it to be overwritten by new data * *return true if there is element in array and discounted * *return false if there is no element in the array * *@pre The array must have elements inside *@return bool * */ bool PopBack(); /** * *@fn Resize() * *@brief Resize the total size of the array * * *Expand the array if the element in the array is more *than half of the total size by 1.5 times * * *@return void * */ void Resize(); /** * *@fn GetLength() * *@brief Resize the length of the array * * *return the total number of elements residing inside *of the array. * *@return int * */ int GetLength(); /** * *@overload T& operator[](int index) const * *@brief Overloading of [] operator * *overload the [] to return the element of the array *passed in to the paramemter * *@param index - int type *@return T& * */ T& operator[](int index) const; /** * *@fn Vector(); * *@brief The destructor of Vector * *Release the memory back to the OS and set the *memory location of the array to null. * * *@return void * */ ~Vector(); private: /// Holds the number of element in the array int m_length; /// The dynamic array that holds the records T *m_array; /// Hold the total size of the array int m_Tsize; }; template Vector::Vector() { // initiate member variables to m_length = 0; m_array = new T[DefaultSize]; // allocate new memory location on heaps for the dynamic array m_Tsize = DefaultSize; } template Vector::~Vector() { // release the memory location on heap back to os if dynamic array is not null if(m_array!=nullptr) { delete[] m_array; m_array = nullptr; } } template int Vector::GetLength() { // return the length of the array return m_length; } template int Vector::GetSize() { // return the total size of the array return m_Tsize; } template void Vector::Resize() { // resize the array by 1.5 times its original size m_Tsize = m_Tsize*1.5; T *newarray = new T[m_Tsize]; // open new dyanmic array pointer for (int i = 0; i bool Vector::IsEmpty() { // return true if there is not element in the array if (m_length == 0) { return true; } else { return false; } } template void Vector::PushBack(T value) { // add elements to the back of the array m_array[m_length] = value; m_length ++; // if the array element is more than half full if(m_length>m_Tsize/2) { // call Resize function to resize the array Resize(); } } template bool Vector::PopBack() { // if there is element in the array, minus the length of the array by 1 if(m_length!=0) { // the last element then is available to be overwritten or not in used m_array[m_length] = 0; m_length--; return true; } else { // if the length if empty return false return false; } } template T& Vector::operator[](int index) const { // return the array element when [] is called return m_array[index]; } #endif // VECTOR_H