Facebook
From Tan Jun QI , 4 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 12
  1. #include "date.h"
  2. #include "time.h"
  3. #include "vector.h"
  4. #include "weather.h"
  5. #include<iostream>
  6. #include <fstream>
  7. #include<sstream>
  8. #include <cmath>
  9.  
  10.  
  11. using namespace std;
  12.  
  13.  
  14. string FileName = "MetData_Mar01-2014-Mar01-2015-ALL.csv"; // enter the file name of the file here to read the file
  15.  
  16. string OutFileName = "WindTempSolar.csv"; // enter the file name here to write to the file
  17.  
  18. void ReadAllDataFromFile(Vector<Weather> &WeatherVec,istream &infile); // used to read the record from the file and store in class Vector obj
  19. void PrintMenu(); // print the menu options
  20. float CalAvgWindSpeed(Vector<Weather> & Vec,unsigned month, unsigned year); // calculatate the average windspeed of the month and year
  21. float CalAvgTemp(Vector<Weather> & Vec,unsigned month, unsigned year); // calculate the average temperature of the month and year
  22. float CalSolarRad(Vector<Weather> &WeatherVecObj, unsigned Month, unsigned Year); // calculate the solar radiation of the month and year
  23. string GetMonthName(unsigned month); // get the string name of the month
  24. void ReadVecInputOption1(Vector<Weather> & WeatherVecObj,Vector<Weather> &WeatherVec, unsigned Month,unsigned Year); // read the input from the Vector and take relevant record to another Vector for option 1
  25. void  ReadVecInputOption2 (Vector<Weather> &WeatherVec,Vector<Weather> & WeatherVecObj, unsigned Year); // read the input from the Vector and take relevant record to another Vector for option 2
  26. void ReadVecInputWSolarOption3( Vector<Weather> & WeatherVec,Vector<Weather>&WeatherVecObj,unsigned Year);// read the input from the Vector and take relevant record to another Vector for option 3
  27. bool CheckChoice(int &I_Choice, string S_choice); // error checking of user input of the Choice
  28. bool CheckYear(unsigned &Year, string S_Year);// error checking of user input of the year
  29. bool CheckMonth(unsigned &Month, string S_Month);// error checking of user input of the month
  30. void PrintYearRequest(); // prompt for the year
  31. void PrintMonthRequest(); // prompt for the month
  32. void PrintOption1Output(unsigned Month,unsigned Year,float AverageTemperature,float AverageWindSpeed); // print option  1 to screen
  33. void PrintOption2OutPut(unsigned Month, float AverageTemperature, float AverageWindSpeed); // print option 2 output to screen
  34. void PrintOption3Output(unsigned Month,float Solar_Kwh_Msqr); // print option 3 output to screen
  35. void PrintOption4ToCsv(unsigned Month,float Solar_Kwh_Msqr,float AvgWindSpeed,float AvgTemperature,unsigned Year,ofstream &Outfile); // print option 4 to screen
  36.  
  37.  
  38.  
  39. int main ()
  40. {//----------------------------Initialising and declaration of variable--------------------------
  41.     unsigned C_Year=0,C_Month=0;
  42.     float AvgWindSpeed=0,AvgTemperature=0, Solar_Kwh_Msqr=0;
  43.     int Choice=0;
  44.     string S_Choice ="",S_Month ="", S_Year="",IgnoreLine="";
  45.  //-----------------------------------------------------------------------------------------------
  46.     Vector<Weather> WeatherVec; // create a Vector object of type Weather and name it WeatherVec
  47.     ifstream infile; // instantiate ifstream to read file
  48.     infile.open(FileName); // open file through ifstream
  49.     if(infile.is_open())
  50.     { // check if the file is open
  51.         getline(infile,IgnoreLine,'\n'); // read in the file line of the file which contains the titles
  52.         ReadAllDataFromFile(WeatherVec,infile); // call the function to store all the record in Vector obj
  53.     }
  54.     else
  55.     { // if file is not opened print error
  56.         cout<<"Error reading file"<<endl;
  57.         return -1; // return -1 to end the program
  58.     }
  59.     infile.close(); // close the file
  60.     cout<<"Number of Record loaded : "<<WeatherVec.GetLength()<<endl; // print to screen the number of records in the array
  61.  
  62.     PrintMenu(); // print the menu options
  63.  
  64.     cout<<"Please enter your choice"<<endl; // prompt for S_Choice
  65.     cin>>S_Choice;//Get S_Choice
  66.  
  67.     while(CheckChoice(Choice,S_Choice)==false)
  68.     { // if incorrect user input prompt and get again
  69.         cout<<"Please enter your choice again 1 - 5"<<endl;
  70.         cin>>S_Choice;
  71.     }
  72.     cout<<endl; // print new line
  73.  
  74.     while (Choice!=5) // if the choice inputed by the user not equal 5 enter while loop
  75.     {
  76.         switch(Choice)
  77.         { // check the choice value
  78.         case 1:
  79.         { // if Choice = 1
  80.             Vector<Weather> WeatherVecObj; // create a Vector object with Weather Data type and name it WeatherVecObj
  81.             PrintMonthRequest(); // prompt S_Month
  82.             cin>>S_Month; //get S_Month
  83.             while (CheckMonth(C_Month, S_Month)==false)
  84.             { // if the C_Month is incorrect enter prompt and get again
  85.                 PrintMonthRequest();
  86.                 cin>>S_Month;
  87.             }
  88.  
  89.             PrintYearRequest(); // prompt for S_Year
  90.             cin>>S_Year; // get S_Year
  91.             while (CheckYear(C_Year,S_Year)==false)
  92.             { // if the C_Year is incorrect enter prompt and get again
  93.                 PrintYearRequest();
  94.                 cin>>S_Year;
  95.             }
  96.             ReadVecInputOption1(WeatherVecObj,WeatherVec, C_Month,C_Year); // get the record from the Vector with conditions and placed into WeatherVecObj
  97.             AvgWindSpeed= CalAvgWindSpeed(WeatherVecObj,C_Month, C_Year); // calculate average windspeed
  98.             AvgTemperature= CalAvgTemp(WeatherVecObj,C_Month,C_Year); // calculate average temperature
  99.             PrintOption1Output(C_Month,C_Year,AvgTemperature,AvgWindSpeed); // print the output to screen
  100.             WeatherVecObj.~Vector(); // destruct the WeatherVecObj
  101.  
  102.         }
  103.  
  104.         break;
  105.  
  106.         case 2:
  107.         {
  108.  
  109.             Vector<Weather> WeatherVecObj;// create a Vector object with Weather Data type and name it WeatherVecObj
  110.             PrintYearRequest();// prompt for S_Year
  111.             cin>>S_Year;// get S_Year
  112.             while (CheckYear(C_Year,S_Year)==false)
  113.             {// if the C_Year is incorrect enter prompt and get again
  114.                 PrintYearRequest();
  115.                 cin>>S_Year;
  116.             }
  117.             ReadVecInputOption2(WeatherVec, WeatherVecObj, C_Year);// get the record from the Vector with conditions and placed into WeatherVecObj
  118.             for (unsigned i = 0; i<12; i++) // loop 12 times
  119.             {
  120.                 AvgWindSpeed = CalAvgWindSpeed(WeatherVecObj,i+1, C_Year); // calculate the average windspeed of each month
  121.                 AvgTemperature = CalAvgTemp(WeatherVecObj,i+1, C_Year); // caclualte the average temperature of each month
  122.                 PrintOption2OutPut(i+1,AvgTemperature,AvgWindSpeed); // print the out put for each month
  123.             }
  124.             WeatherVecObj.~Vector(); // destruct the weatherVecObj
  125.  
  126.         }
  127.  
  128.         break;
  129.  
  130.         case 3:
  131.         {
  132.  
  133.             Vector<Weather> WeatherVecObj; // create a Vector object with Weather Data type and name it WeatherVecObj
  134.             PrintYearRequest();// prompt for S_Year
  135.             cin>>S_Year;// get S_Year
  136.             while (CheckYear(C_Year,S_Year)==false)
  137.             {// if the C_Year is incorrect enter prompt and get again
  138.                 PrintYearRequest();
  139.                 cin>>S_Year;
  140.             }
  141.  
  142.             ReadVecInputWSolarOption3(WeatherVec,WeatherVecObj,C_Year);// get the record from the Vector with conditions and placed into WeatherVecObj
  143.  
  144.             for(unsigned i = 0; i<12; i++)
  145.             {// loop 12 times
  146.                 Solar_Kwh_Msqr = CalSolarRad(WeatherVecObj, i+1, C_Year); // calculate the solar radiation kwh/m2 for each month of the year
  147.                 PrintOption3Output(i+1,Solar_Kwh_Msqr); // print the output of option 3
  148.             }
  149.  
  150.             WeatherVecObj.~Vector(); // destruct the WeatherVecObj
  151.  
  152.         }
  153.  
  154.         break;
  155.  
  156.  
  157.         case 4:
  158.         {
  159.             ofstream Outfile; // instantiate the ofstream object to write file
  160.             Outfile.open(OutFileName); // open WindTempSolar.csv if not found create one
  161.  
  162.             Vector<Weather> WeatherVecObj; // create a Vector object with Weather Data type and name it WeatherVecObj
  163.             PrintYearRequest();// prompt for S_Year
  164.             cin>>S_Year;// get S_Year
  165.             while (CheckYear(C_Year,S_Year)==false)
  166.             {// if the C_Year is incorrect enter prompt and get again
  167.                 PrintYearRequest();
  168.                 cin>>S_Year;
  169.             }
  170.             ReadVecInputWSolarOption3(WeatherVec,WeatherVecObj,C_Year);// get the record from the Vector with conditions and placed into WeatherVecObj
  171.             Outfile<<C_Year<<endl; // write the year to file
  172.             for( int i = 0; i<12; i++)
  173.             {
  174.                 Solar_Kwh_Msqr = CalSolarRad(WeatherVecObj, i+1, C_Year);// calculate the solar radiation kwh/m2 for each month of the year
  175.                 AvgTemperature =  CalAvgTemp(WeatherVecObj,i+1,C_Year);// caclualte the average temperature of each month
  176.                 AvgWindSpeed = CalAvgWindSpeed(WeatherVecObj,i+1, C_Year);// calculate the average windspeed of each month
  177.                 PrintOption4ToCsv(i+1,Solar_Kwh_Msqr,AvgWindSpeed,AvgTemperature,C_Year,Outfile); // write the records of each month of the year to the file
  178.  
  179.  
  180.             }
  181.             cout<<"Successfully wrote to "<<OutFileName<<" file"<<endl; // print to screen to indicate wrote successfully
  182.             Outfile.close(); // close the ofstream
  183.             WeatherVecObj.~Vector(); // destruct the WeatherVecObj
  184.         }
  185.  
  186.  
  187.  
  188.         break;
  189.  
  190.         }
  191.         cout<<endl;
  192.         PrintMenu(); // print the menu option again
  193.         cout<<"Please enter your choice again"<<endl; // prompt for the S_choice
  194.         cin>>S_Choice; // get S_Choice
  195.         cout<<endl;
  196.         while(CheckChoice(Choice,S_Choice)==false)
  197.         { // if user input incorrect prompt and get S_Choice again
  198.             cout<<"Please enter your choice again 1 - 5 "<<endl;
  199.             cin>>S_Choice;
  200.         }
  201.         if(Choice==5)
  202.         {// if the user input is = 5 break from the loop
  203.             break;
  204.         }
  205.     }
  206.  
  207.     cout<<"Program ended"<<endl; // print to screen program ended
  208.  
  209.     return 0; // return to os
  210. }
  211.  
  212. void PrintMenu()
  213. { // printing out the menu option titles
  214.     cout<<endl;
  215.     cout<<"1. Print the average wind speed and average ambient air temperature for a specified month and year to screen."<<endl;
  216.     cout<<endl;
  217.     cout<<"2. Print the Average wind speed and average ambient air temperature for each month of a specified year. "<<endl;
  218.     cout<<endl;
  219.     cout<<"3. Print the Total solar radiation in kWh/m2 for each month of a specified year.  "<<endl;
  220.     cout<<endl;
  221.     cout<<"4. Write the Average wind speed (km/h), average ambient air temperature and total solar radiation in \n"
  222.         <<"   kWh/m2 for each month of a specified year to file. "<<endl;
  223.     cout<<endl;
  224.     cout<<"5. Exit the program. "<<endl;
  225.     cout<<endl;
  226.  
  227. }
  228.  
  229. void PrintYearRequest()
  230. { // prompting for year
  231.     cout<<"Please Enter the Year "<<endl;
  232.     cout<<endl;
  233. }
  234.  
  235. void PrintMonthRequest()
  236. { // prompting for month
  237.     cout<<"Please Enter the Month "<<endl;
  238.     cout<<endl;
  239. }
  240.  
  241. float CalAvgWindSpeed(Vector<Weather> & WeatherVecObj,unsigned month, unsigned year)
  242. { // calculating average windspeed for the months of the year
  243.     float AverageWindSpeed=0.0,ConvertingSpeed=0.0;
  244.     unsigned I_Month=0,counter=0;
  245.     for(int i = 0; i<WeatherVecObj.GetLength(); i++) // loop till the last element of the vector
  246.     {
  247.         istringstream(WeatherVecObj[i].GetDate().GetMonth())>>I_Month; // convert string month to unsigned U_Month
  248.         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
  249.         {
  250.  
  251.             ConvertingSpeed = WeatherVecObj[i].GetSpeed()*3.6; // the windspeed of the element times 3.6 and assigned to Converting Speed
  252.             AverageWindSpeed += ConvertingSpeed; // AverageWindSpeed plus adding ConvertingSpeed in the loop
  253.             counter++; // increment counter by 1
  254.         }
  255.     }
  256.     if(AverageWindSpeed!=0)
  257.     { // if avaerage wind speed not equals to 0
  258.         AverageWindSpeed = AverageWindSpeed/counter; //AverageWindSpeed is divided by counter
  259.         AverageWindSpeed = round(AverageWindSpeed*10)/10; // round up to the first decimal point
  260.     }
  261.     return  AverageWindSpeed;// return the average wind speed of the month of the year
  262.  
  263.  
  264. }
  265.  
  266. float CalSolarRad(Vector<Weather> &WeatherVecObj, unsigned Month, unsigned Year)
  267. { // calculate solar radiation
  268.     float Total_Solar_Rad_kWh_Msqr=0.0,Get_Solar_Rad_W_Msqr=0.0,Cal_Solar_Rad_W_Msqr=0.0;
  269.     unsigned I_Month=0;
  270.  
  271.     for(int i = 0; i<WeatherVecObj.GetLength(); i++) // loop till the end of the Vector
  272.     {
  273.         istringstream(WeatherVecObj[i].GetDate().GetMonth())>>I_Month;// converting string month to unsigned I_Month
  274.  
  275.         if(WeatherVecObj[i].GetDate().GetYear() == Year && I_Month==Month && WeatherVecObj[i].GetSolarMSqr()>=100)
  276.         { // if year and month = to specific year AND solar radiation value of the element is more or equal to 100 enter if statement
  277.  
  278.  
  279.             Get_Solar_Rad_W_Msqr = WeatherVecObj[i].GetSolarMSqr();// retrieve solar radiation of the element
  280.             Cal_Solar_Rad_W_Msqr = Get_Solar_Rad_W_Msqr*1/6; // converting the solar radiation
  281.             Cal_Solar_Rad_W_Msqr = Cal_Solar_Rad_W_Msqr/1000; // calculating the solar radiation
  282.             Total_Solar_Rad_kWh_Msqr += Cal_Solar_Rad_W_Msqr; // plus adding the solar radiation to Total_Solar_Rad_kWh_Msqr
  283.  
  284.  
  285.         }
  286.     }
  287.  
  288.     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
  289.  
  290.     return Total_Solar_Rad_kWh_Msqr; // return the total solar radiation
  291.  
  292. }
  293.  
  294.  
  295. float CalAvgTemp(Vector<Weather> & WeatherVecObj,unsigned month, unsigned year)
  296. {
  297.     float ConvertingTemp=0.0,AverageTemperature=0.0;
  298.     unsigned I_Month,counter=0;
  299.  
  300.     for(int i = 0; i<WeatherVecObj.GetLength(); i++)
  301.     {
  302.         istringstream(WeatherVecObj[i].GetDate().GetMonth())>>I_Month; // converting string month to unsigned I_Month
  303.         if(WeatherVecObj[i].GetDate().GetYear() == year && I_Month==month)
  304.         {// if year and month = to specific year AND Month
  305.             ConvertingTemp += WeatherVecObj[i].GetTemperature(); // adding to the ConvertingTemp of the temperature of each of the element
  306.             counter++; // increment counter by 1 every loop
  307.         }
  308.     }
  309.     if(ConvertingTemp!=0)
  310.     { // if ConvertingTemp not equals to 0
  311.  
  312.         AverageTemperature = ConvertingTemp/counter; // ConvertingTemp divided by counter to get average
  313.         AverageTemperature = round(AverageTemperature*10)/10; // round up to the nearing tenth
  314.     }
  315.     return AverageTemperature;
  316. }
  317.  
  318.  
  319.  
  320. string GetMonthName(unsigned month)
  321. { // return the string name of the month entered in the parameter
  322.     string Month = "";
  323.     if(month == 1)
  324.     {
  325.         Month = "January";
  326.         return Month;
  327.     }
  328.     else if (month == 2)
  329.     {
  330.         Month = "February";
  331.         return Month;
  332.     }
  333.     else if (month == 3)
  334.     {
  335.         Month = "March";
  336.         return Month;
  337.     }
  338.     else if (month == 4)
  339.     {
  340.         Month = "April";
  341.         return Month;
  342.     }
  343.     else if (month == 5)
  344.     {
  345.         Month = "May";
  346.         return Month;
  347.     }
  348.     else if (month == 6)
  349.     {
  350.         Month = "June";
  351.         return Month;
  352.     }
  353.     else if (month == 7)
  354.     {
  355.         Month = "July";
  356.         return Month;
  357.     }
  358.     else if (month == 8)
  359.     {
  360.         Month = "August";
  361.         return Month;
  362.     }
  363.     else if (month == 9)
  364.     {
  365.         Month = "September";
  366.         return Month;
  367.     }
  368.     else if (month == 10)
  369.     {
  370.         Month = "October";
  371.         return Month;
  372.     }
  373.     else if (month == 11)
  374.     {
  375.         Month = "November";
  376.         return Month;
  377.     }
  378.     else if (month == 12)
  379.     {
  380.         Month = "December";
  381.         return Month;
  382.     }
  383.     return Month;
  384. }
  385.  
  386. void ReadVecInputOption1(Vector<Weather> & AddWeatherVec,Vector<Weather> &WeatherVec, unsigned Month,unsigned Year)
  387. { // read the Vector input into new Vector in option 1
  388.  
  389.     unsigned U_Month;
  390.     float I_Speed,F_Temperature;
  391.     Weather T_WObj;
  392.     Date T_DateObj;
  393.     Time T_TimeObj;
  394.  
  395.  
  396.     for(int i = 0; i<WeatherVec.GetLength(); i++) // run till the end of the WeatherVec
  397.     {
  398.         istringstream(WeatherVec[i].GetDate().GetMonth())>>U_Month; // converting string month into unsigned U_Month
  399.         if(U_Month == Month && WeatherVec[i].GetDate().GetYear() == Year) // if month and year of the element is equal to specific month and year
  400.         {
  401.             T_DateObj = WeatherVec[i].GetDate(); //retrieve date of the element
  402.             T_TimeObj =  WeatherVec[i].GetTime(); // retrieve the time of the element
  403.             I_Speed = WeatherVec[i].GetSpeed(); // retrieve the wind speed of the element
  404.             F_Temperature = WeatherVec[i].GetTemperature();// retrieve the temperature of the element
  405.  
  406.  
  407.             T_WObj.SetSpeed(I_Speed); // set windspeed to weather obj
  408.             T_WObj.SetDate(T_DateObj);// set date to weather obj
  409.             T_WObj.SetTime(T_TimeObj);// set time to weather obj
  410.             T_WObj.SetTemperature(F_Temperature);// set temeperature to weather obj
  411.             AddWeatherVec.PushBack(T_WObj); //push weather object into new Vector
  412.         }
  413.     }
  414.  
  415. }
  416.  
  417. void  ReadVecInputOption2 (Vector<Weather> &WeatherVec,Vector<Weather> & AddWeatherVec, unsigned Year)
  418. {// read the Vector input into new Vector in option 2
  419.  
  420.  
  421.     float I_Speed,F_Temperature;
  422.     Weather T_WObj;
  423.     Date T_DateObj;
  424.     Time T_TimeObj;
  425.  
  426.  
  427.     for(int i = 0; i<WeatherVec.GetLength(); i++)
  428.     {
  429.  
  430.         if( WeatherVec[i].GetDate().GetYear() == Year)
  431.         {
  432.             T_DateObj = WeatherVec[i].GetDate();//retrieve date of the element
  433.             T_TimeObj =  WeatherVec[i].GetTime();// retrieve the time of the element
  434.             I_Speed = WeatherVec[i].GetSpeed();// retrieve the wind speed of the element
  435.             F_Temperature = WeatherVec[i].GetTemperature(); // retrieve temeperature to weather obj
  436.  
  437.  
  438.             T_WObj.SetSpeed(I_Speed);// set windspeed to weather obj
  439.             T_WObj.SetDate(T_DateObj);// set date to weather obj
  440.             T_WObj.SetTime(T_TimeObj);// set time to weather obj
  441.             T_WObj.SetTemperature(F_Temperature);// set temeperature to weather obj
  442.             AddWeatherVec.PushBack(T_WObj);//push weather object into new Vector
  443.         }
  444.     }
  445.  
  446. }
  447.  
  448. bool CheckMonth(unsigned &Month, string S_Month)
  449. {// method to error check the month input
  450.     unsigned U_Month;
  451.     istringstream(S_Month)>> U_Month; // convert string month to unsigned U_Month
  452.     if(U_Month>0 && U_Month<13)
  453.     { // if U_Month is more than 0 and less than 13 set Month and return true
  454.         Month = U_Month;
  455.         return true;
  456.     }
  457.     else
  458.     { // else return false
  459.         return false;
  460.     }
  461.  
  462. }
  463.  
  464. bool CheckYear(unsigned &Year, string S_Year)
  465. {// method to error check the year  input
  466.     unsigned U_Year;
  467.     istringstream(S_Year)>>U_Year; // convert string month to unsigned U_Year
  468.     if(U_Year>1000 && U_Year<2021)
  469.     {// if U_Year  is more than 1000 and less than 2021 set Year  and return true
  470.         Year = U_Year; //
  471.         return true;
  472.     }
  473.     else
  474.     { // if condition not met return false
  475.         return false;
  476.     }
  477. }
  478.  
  479.  
  480. void ReadVecInputWSolarOption3( Vector<Weather> & WeatherVec,Vector<Weather> &WeatherVecObj,unsigned Year)
  481. {// read the Vector input into new Vector in option 3
  482.     float I_Speed,F_Temperature,F_Solar_M_Sqr;
  483.     Weather T_WObj;
  484.     Date T_DateObj;
  485.     Time T_TimeObj;
  486.  
  487.  
  488.     for(int i = 0; i<WeatherVec.GetLength(); i++)
  489.     { // run till the end of the WeatherVec
  490.  
  491.         if(WeatherVec[i].GetDate().GetYear() == Year)
  492.         { // if the element year is equal to the specific year
  493.             T_DateObj = WeatherVec[i].GetDate();//retrieve date of the element
  494.             T_TimeObj =  WeatherVec[i].GetTime();// retrieve the time of the element
  495.             I_Speed = WeatherVec[i].GetSpeed();// retrieve the wind speed of the element
  496.             F_Temperature = WeatherVec[i].GetTemperature();// set temeperature of the element
  497.             F_Solar_M_Sqr = WeatherVec[i].GetSolarMSqr();// retrieve the solar radiation of the element
  498.  
  499.             T_WObj.SetSpeed(I_Speed);// set windspeed to weather obj
  500.             T_WObj.SetDate(T_DateObj);// set date to weather obj
  501.             T_WObj.SetTime(T_TimeObj);// set time to weather obj
  502.             T_WObj.SetTemperature(F_Temperature);// set temeperature to weather obj
  503.             T_WObj.SetSolarMSqr(F_Solar_M_Sqr);// set the solar radiation to weather obj
  504.             WeatherVecObj.PushBack(T_WObj);// push the weather object to array
  505.         }
  506.  
  507.     }
  508.  
  509. }
  510. void PrintOption1Output(unsigned Month,unsigned Year,float AverageTemperature,float AverageWindSpeed)
  511. { // print output option
  512.     if(AverageTemperature==0 && AverageWindSpeed==0)
  513.     { // check if windspeed and temperature is 0
  514.         cout<< GetMonthName(Month)<<" "<<Year<<" : No data"<<endl;
  515.     }
  516.     else
  517.     {
  518.         cout<< GetMonthName(Month)<<" "<<Year<<" : "; // print the month name and the year followed by windspeed and temperature
  519.         cout<<AverageWindSpeed<<" km/h, "<< AverageTemperature<<" degrees C"<<'\n';
  520.         cout<<endl;
  521.     }
  522. }
  523.  
  524. void PrintOption2OutPut(unsigned Month, float AverageTemperature, float AverageWindSpeed)
  525. {
  526.     if(AverageTemperature!=0 && AverageWindSpeed!=0)
  527.     { // print out if temperature and windspeed is not equal to 0
  528.         cout<< GetMonthName(Month)<<" : "; // print the month name and the year followed by windspeed and temperature
  529.         cout<<AverageWindSpeed<<" km/h, "<< AverageTemperature<<" degrees C"<<'\n';
  530.         cout<<endl;
  531.     }
  532.     else
  533.     { // else print out no data for the month
  534.         cout<< GetMonthName(Month)<<" : No data"<<endl;
  535.         cout<<endl;
  536.     }
  537. }
  538.  
  539. void PrintOption3Output(unsigned Month,float Solar_Kwh_Msqr)
  540. {
  541.     if(Solar_Kwh_Msqr!=0)
  542.     { // if the solar radiation is not equals to 0 print out the solar radiation in kWh/m2
  543.         cout<<GetMonthName(Month)<<": "<<Solar_Kwh_Msqr<<" kWh/m2"<<endl;
  544.         cout<<endl;
  545.     }
  546.     else
  547.     { // print out the no data for the month
  548.         cout<<GetMonthName(Month)<<": No Data"<<endl;
  549.         cout<<endl;
  550.     }
  551. }
  552.  
  553. void PrintOption4ToCsv(unsigned Month,float Solar_Kwh_Msqr,float AvgWindSpeed,float AvgTemperature,unsigned Year,ofstream &Outfile)
  554. { // print out for the option 4
  555.  
  556.     if(Solar_Kwh_Msqr!=0 &&AvgWindSpeed!=0 && AvgTemperature!=0)
  557.     { // if solar radiation , windspeed and temperature is not equal to 0 print the output
  558.         Outfile<<GetMonthName(Month)<<','<<AvgWindSpeed<<','<<AvgTemperature<<','<<Solar_Kwh_Msqr<<endl;
  559.         cout<<endl;
  560.     }
  561.     else
  562.     {
  563.         Outfile<<GetMonthName(Month)<<','<<"No Data"<<endl;
  564.         cout<<endl;
  565.     }
  566. }
  567.  
  568. void ReadAllDataFromFile(Vector<Weather> &WeatherVec,istream &infile)
  569. {
  570.     //----------------------------------Variable and objects declaration and initialisation-----------------------------------
  571.     string IgnoreColumn,T_speed,getLine,T_Temperature="",S_Day,S_Month,S_Year,S_Hrs,S_Mins,IgnoreLine,S_Solar_M_Sqr;
  572.     unsigned Temp_Day=0,Temp_Year=0;
  573.     float I_Speed=0,F_Temperature=0,F_Solar_M_Sqr=0;
  574.     int Temp_Hrs=0,Temp_Mins=0;
  575.     Date T_DateObj;
  576.     Time T_TimeObj;
  577.     Weather T_WObj;
  578. //----------------------------------------------------------------------------------------------------------------------------
  579.     while(!infile.eof())
  580.     { // loop till the end of file
  581.         if(infile.eof())
  582.         {// if end of file reach break from loop
  583.             break;
  584.         }
  585.  
  586.         getline(infile,S_Day,'/');// read data till the delimiter /
  587.         istringstream(S_Day)>>Temp_Day; // convert string day to unsigned day
  588.         getline(infile,S_Month,'/');// read data till the delimiter /
  589.         getline(infile,S_Year,' ');// read data till the delimiter ' '
  590.         istringstream (S_Year)>>Temp_Year;// convert string year to unsigned year
  591.         getline(infile,S_Hrs,':');// read data till the delimiter :
  592.         istringstream(S_Hrs)>>Temp_Hrs;// convert string hours to int hours
  593.         getline(infile,S_Mins,',');// read data till the delimiter ,
  594.         istringstream(S_Mins)>>Temp_Mins;// convert string minutes to int minutes
  595.  
  596.         T_DateObj.SetDay(Temp_Day);//Set the day to date object T_DateObj
  597.         T_DateObj.SetMonth(S_Month); // set the month to date object T_DateObj
  598.         T_DateObj.SetYear(Temp_Year); // set the year  to date object T_DateObj
  599.         T_TimeObj.SetHours(Temp_Hrs); // set the hours to time object T_TimeObj
  600.         T_TimeObj.SetMins(Temp_Mins); // set the minutes to time object T_TimeObj
  601.  
  602.         T_WObj.SetDate(T_DateObj); //set the date T_WObj weather object
  603.         T_WObj.SetTime(T_TimeObj); // set the time T_WObj weather object
  604.         for(int i =0; i<9; i++)
  605.         { // loop 9 times to ignore 9 columns
  606.             getline(infile,IgnoreColumn,',');
  607.  
  608.         }
  609.         getline(infile,T_speed,','); // read data till the delimiter ,
  610.         istringstream(T_speed)>>I_Speed; // convert string windspeed to float windspeed
  611.         T_WObj.SetSpeed(I_Speed);
  612.         getline(infile,S_Solar_M_Sqr,',');// read data till the delimiter ,
  613.         istringstream(S_Solar_M_Sqr)>>F_Solar_M_Sqr; // convert string solar radiation to float solar radiation
  614.         T_WObj.SetSolarMSqr(F_Solar_M_Sqr); // set the solar radiation to T_WObj weather object
  615.         for(int i =0; i<5; i++)
  616.         { // loop 5 times to ignore 5 columns
  617.             getline(infile,IgnoreColumn,',');// read data till the delimiter ,
  618.         }
  619.         getline(infile,T_Temperature,'\n');// read data till the delimiter \n
  620.         if(!T_Temperature.empty()) // check if the temperature string is not empty
  621.         {
  622.             istringstream(T_Temperature)>>F_Temperature; // convert the string temperature to float temperature
  623.             T_WObj.SetTemperature(F_Temperature); // set temperature to T_WObj weather object
  624.             WeatherVec.PushBack(T_WObj); // push the T_WObj weather object to Vector
  625.         }
  626.         T_Temperature = ""; // erase the value of temperature
  627.  
  628.     }
  629.  
  630. }
  631.  
  632.  
  633. bool CheckChoice(int &I_Choice,string S_choice)
  634. { // error checking the choice user input
  635.     int T_Choice;
  636.  
  637.  
  638.     istringstream(S_choice)>>T_Choice;// convert string choice to int choice
  639.  
  640.     if(T_Choice>0 && T_Choice <6)
  641.     { // int choice is more than 0 AND less than 5
  642.         I_Choice = T_Choice; // set I_Choice to int T_Choice
  643.  
  644.         return true;// return true
  645.     }
  646.     else
  647.     {//if condition not met return false
  648.         return false;
  649.     }
  650.  
  651.  
  652. }
  653.  
  654.  
  655.  
  656. #include "date.h"
  657.  
  658. Date::Date()
  659. {
  660.     m_day = 0; // initialise the m_day
  661.     m_month="Invalid Month";// initialise the m_month
  662.     m_year = 0; // initialise m_year
  663. }
  664.  
  665. Date::~Date()
  666. {
  667.     m_day = 0; // overwrite the m_day
  668.     m_month="Invalid Month";// overwrite the m_month
  669.     m_year = 0; // overwrite the m_year
  670.  
  671. }
  672.  
  673.  
  674. bool Date::SetDay(unsigned day )
  675. {
  676.     if(day > 0 && day <= 31)
  677.     { // if day is more than 0 and less or equal to 31
  678.         m_day = day; // set  m_day to day
  679.         return true; // return true
  680.     }
  681.     else
  682.     { // condition not met return false
  683.         return false;
  684.     }
  685.  
  686. }
  687.  
  688. unsigned Date::GetDay() const
  689. { // return value of m_day
  690.     return m_day;
  691. }
  692.  
  693. bool Date::SetMonth(string month)
  694. {
  695.     unsigned U_Month;
  696.     istringstream(month)>>U_Month; // convert string month to unsigned U_Month
  697.     if(U_Month>0 && U_Month<13)
  698.     { // if I_Month more than 0 and less than 13
  699.         m_month=month; // set m_month = month
  700.         return true;
  701.     }
  702.     else
  703.     { // if condition not met return false
  704.         return false;
  705.     }
  706.  
  707. }
  708. string Date::GetMonth() const
  709. {
  710.     return m_month; // return the value of month of the class
  711. }
  712.  
  713. bool Date::SetYear(unsigned year)
  714. {
  715.     if(year>1000 && year<=2020)
  716.     { // if year more than 1000 AND year less or equal to 2020
  717.         m_year = year; // set m_year = year and return true
  718.         return true;
  719.     }
  720.     else
  721.     {// if condition not met return false
  722.         return false;
  723.     }
  724.  
  725. }
  726.  
  727. unsigned Date:: GetYear() const
  728. { //return the year of the class
  729.     return m_year;
  730. }
  731.  
  732.  
  733. #include "weather.h"
  734.  
  735. Weather::Weather()
  736. {// initialise the value of members
  737.     m_Speed = 0;
  738.     m_Temperature = 0;
  739.     m_Solar_M_Sqr = 0;
  740.  
  741.  
  742. }
  743.  
  744. Weather::~Weather()
  745. { //// erase the value or overwritting the value of members
  746.     m_Speed = 0;
  747.     m_Temperature = 0;
  748.     m_Solar_M_Sqr = 0;
  749.  
  750. }
  751.  
  752.  
  753.  
  754. bool Weather:: SetSpeed( float T_Speed)
  755. { // check if T_Speed is more or equal to 0
  756.     if(T_Speed >=0 )
  757.     { // if yes set member speed to T_speed
  758.         m_Speed = T_Speed;
  759.         return true;
  760.     }
  761.     else
  762.     {// if not return false
  763.         return false;
  764.     }
  765.  
  766. }
  767.  
  768.  
  769. void Weather:: SetTemperature( float T_Temperature)
  770. {
  771.  // set m_Temperature to T_Temperature
  772.     m_Temperature = T_Temperature;
  773. }
  774.  
  775.  
  776. void  Weather::SetDate(Date T_DateObj)
  777. { // set m_DateObj = Date T_DateObj
  778.     m_DateObj = T_DateObj;
  779. }
  780.  
  781.  
  782. void Weather::SetTime(Time T_TimeObj)
  783. {
  784.     m_TimeObj = T_TimeObj;
  785. } // set m_TimeObj to T_TimeObj
  786.  
  787.  
  788.  float Weather::GetSpeed() const
  789. {// return the value of windspeed of the class
  790.     return m_Speed;
  791. }
  792.  
  793.  float Weather::GetTemperature() const
  794. {
  795.     return m_Temperature;
  796. }
  797.  
  798. void Weather::SetSolarMSqr(float SolarMSqr)
  799. { // set m_Solar_M_Sqr = SolarMSqr
  800.     m_Solar_M_Sqr = SolarMSqr;
  801. }
  802.  
  803. float Weather::GetSolarMSqr()
  804. { // return the value of m_Solar_M_Sqr
  805.     return m_Solar_M_Sqr;
  806. }
  807.  
  808. Date Weather::GetDate() const
  809. { // return the of the Date of the class
  810.     return m_DateObj;
  811. }
  812.  
  813. Time Weather::GetTime() const
  814. { // return the m_TimeObj value
  815.     return m_TimeObj;
  816. }
  817.  
  818.  
  819.  
  820. #include "time.h"
  821.  
  822. Time::Time()
  823. { // inititalise the members of the class
  824.     m_Hours = 0;
  825.     m_Mins = 0;
  826. }
  827.  
  828. Time::~Time()
  829. { // erase or overwrite the value of instantiated Time class
  830.     m_Hours = 0;
  831.     m_Mins = 0;
  832. }
  833.  
  834. bool Time::SetHours(int Hours)
  835. { // set Hours equals to m_Hours if Hour is more or equal to zero AND less than 24
  836.     if(Hours >=0 && Hours<24)
  837.     {
  838.          m_Hours = Hours;
  839.          return true;
  840.     }
  841.     else
  842.     {// if condition not met then return false
  843.         return false;
  844.     }
  845.  
  846. }
  847.  
  848. bool Time::SetMins(int minutes)
  849. { // if minutes more to equal to zero AND minutes less than 60
  850.     if (minutes >=0 && minutes <60)
  851.     {// set member m_Mins to minutes
  852.         m_Mins = minutes;
  853.         return true;
  854.     }
  855.     else
  856.     {// return false if condition not met
  857.         return false;
  858.     }
  859.  
  860. }
  861. int Time::GetHours() const
  862. { // return the value of hours of the class
  863.     return m_Hours;
  864. }
  865. int Time::GetMins() const
  866. {// return the value of minutes of the class
  867.     return m_Mins;
  868. }
  869.  
  870.  
  871.  
  872.  
  873.  
  874. #ifndef VECTOR_H
  875. #define VECTOR_H
  876. #include <iostream>
  877.  
  878. #define DefaultSize 10
  879.  
  880. //---------------------------------------------------------------
  881. /**
  882. *@class Vector
  883. *@brief The Vector template that holds the inputted data
  884. *
  885. *
  886. *The Vector Template can be created in with any data type
  887. *it is used to hold the inputted data from the file
  888. *and store it.
  889. *
  890. *
  891. *
  892. *
  893. *@author Tan Jun Qi
  894. *@version 10
  895. *@date 19/June/2020 Singapore,Singapore
  896. *
  897. *
  898. *
  899. */
  900. //---------------------------------------------------------------------------
  901.  
  902. using namespace std;
  903.  
  904. template<class T>
  905. class Vector
  906. {
  907. public:
  908.     /**
  909.     *@brief The Default Vector constructor
  910.     *
  911.     *This initialise the member or attributes of time class and
  912.     *allocate memory space in the heap memory for the dynamic array
  913.     *
  914.     *
  915.     *@return void
  916.     *
  917.     */
  918.     Vector();
  919.     /**
  920.      *
  921.      *@fn GetSize()
  922.      *
  923.      *@brief Function to return the total size of the array
  924.      *
  925.      *
  926.      *return the total size of the array to check if the array
  927.      *is expanding
  928.      *
  929.      *
  930.      *@return int
  931.      *
  932.      */
  933.     int GetSize();
  934.     /**
  935.     *
  936.     *@fn IsEmpty()
  937.     *
  938.     *@brief Function to check if the array is empty
  939.     *
  940.     *
  941.     *check if there are any elements inside of the array
  942.     *return false if there is elements return true if its
  943.     *empty
  944.     *
  945.     *@return bool
  946.     *
  947.     */
  948.     bool IsEmpty();
  949.     /**
  950.     *
  951.     *@fn PushBack(T value)
  952.     *
  953.     *@brief Function to add in new data into array
  954.     *
  955.     *
  956.     *Add the data into the last element of the array
  957.     *
  958.     *
  959.     *
  960.     *@return void
  961.     *
  962.     */
  963.     void PushBack(T value);
  964.     /**
  965.     *
  966.     *@fn PopBack()
  967.     *
  968.     *@brief Erase the last element of the array
  969.     *
  970.     *
  971.     *Dis count the last element of the array and enable
  972.     *it to be overwritten by new data
  973.     *
  974.     *return true if there is element in array and discounted
  975.     *
  976.     *return false if there is no element in the array
  977.     *
  978.     *@pre The array must have elements inside
  979.     *@return bool
  980.     *
  981.     */
  982.     bool PopBack();
  983.     /**
  984.     *
  985.     *@fn Resize()
  986.     *
  987.     *@brief Resize the total size of the array
  988.     *
  989.     *
  990.     *Expand the array if the element in the array is more
  991.     *than half of the total size by 1.5 times
  992.     *
  993.     *
  994.     *@return void
  995.     *
  996.     */
  997.     void Resize();
  998.     /**
  999.     *
  1000.     *@fn GetLength()
  1001.     *
  1002.     *@brief Resize the length of the array
  1003.     *
  1004.     *
  1005.     *return the total number of elements residing inside
  1006.     *of the array.
  1007.     *
  1008.     *@return int
  1009.     *
  1010.     */
  1011.     int GetLength();
  1012.     /**
  1013.     *
  1014.     *@overload T& operator[](int index) const
  1015.     *
  1016.     *@brief Overloading of [] operator
  1017.     *
  1018.     *overload the [] to return the element of the array
  1019.     *passed in to the paramemter
  1020.     *
  1021.     *@param index - int type
  1022.     *@return T&
  1023.     *
  1024.     */
  1025.  
  1026.  
  1027.     T& operator[](int index) const;
  1028.     /**
  1029.     *
  1030.     *@fn Vector();
  1031.     *
  1032.     *@brief The destructor of Vector
  1033.     *
  1034.     *Release the memory back to the OS and set the
  1035.     *memory location of the array to null.
  1036.     *
  1037.     *
  1038.     *@return void
  1039.     *
  1040.     */
  1041.  
  1042.     ~Vector();
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048. private:
  1049.     /// Holds the number of element in the array
  1050.     int m_length;
  1051.     /// The dynamic array that holds the records
  1052.     T *m_array;
  1053.     /// Hold the total size of the array
  1054.     int m_Tsize;
  1055. };
  1056.  
  1057.  
  1058.  
  1059. template<class T>
  1060. Vector<T>::Vector()
  1061. { // initiate member variables to
  1062.     m_length = 0;
  1063.     m_array = new T[DefaultSize]; // allocate new memory location on heaps for the dynamic array
  1064.     m_Tsize = DefaultSize;
  1065. }
  1066. template<class T>
  1067. Vector<T>::~Vector()
  1068. { // release the memory location on heap back to os if dynamic array is not null
  1069.     if(m_array!=nullptr)
  1070.     {
  1071.         delete[] m_array;
  1072.         m_array = nullptr;
  1073.     }
  1074. }
  1075.  
  1076. template<class T>
  1077. int Vector<T>::GetLength()
  1078. { // return the length of the array
  1079.     return m_length;
  1080. }
  1081.  
  1082. template<class T>
  1083. int Vector<T>::GetSize()
  1084. { // return the total size of the array
  1085.     return m_Tsize;
  1086. }
  1087.  
  1088.  
  1089. template<class T>
  1090. void Vector<T>::Resize()
  1091. { // resize the array by 1.5 times its original size
  1092.     m_Tsize = m_Tsize*1.5;
  1093.     T *newarray = new T[m_Tsize]; // open new dyanmic array pointer
  1094.     for (int i = 0; i<m_length; i++)
  1095.     { // allocate old elements in  array to new array
  1096.         newarray[i] = m_array[i];
  1097.     }
  1098.     delete [] m_array; // delete old array
  1099.     m_array = nullptr; // set old array to null
  1100.     m_array = newarray; // allocate the memory location of new array to m_array
  1101.  
  1102. }
  1103.  
  1104. template<class T>
  1105. bool Vector<T>::IsEmpty()
  1106. { // return true if there is not element in the array
  1107.     if (m_length == 0)
  1108.     {
  1109.         return true;
  1110.     }
  1111.     else
  1112.     {
  1113.         return false;
  1114.     }
  1115. }
  1116.  
  1117.  
  1118. template<class T>
  1119. void Vector<T>::PushBack(T value)
  1120. { // add elements to the back of the array
  1121.     m_array[m_length] = value;
  1122.     m_length ++; // if the array element is more than half full
  1123.     if(m_length>m_Tsize/2)
  1124.     {
  1125.         // call Resize function to resize the array
  1126.         Resize();
  1127.  
  1128.     }
  1129.  
  1130. }
  1131.  
  1132. template<class T>
  1133. bool Vector<T>::PopBack()
  1134. { // if there is element in the array, minus the length of the array by 1
  1135.     if(m_length!=0)
  1136.     { // the last element then is available to be overwritten or not in used
  1137.         m_array[m_length] = 0;
  1138.         m_length--;
  1139.         return true;
  1140.     }
  1141.     else
  1142.     { // if the length if empty return false
  1143.         return false;
  1144.     }
  1145.  
  1146. }
  1147.  
  1148. template<class T>
  1149. T& Vector<T>::operator[](int index) const
  1150. { // return the array element when [] is called
  1151.     return m_array[index];
  1152. }
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158. #endif // VECTOR_H
captcha