Facebook
From Tan Jun Qi, 4 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 13
  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. #include<map>
  10. #include<string>
  11.  
  12. #include "BinarySearchTree.h"
  13.  
  14. using namespace std;
  15.  
  16.  
  17. string FileName = "data/met_index.txt"; // enter the file name of the file here to read the file
  18.  
  19. string OutFileName = "WindTempSolar.csv"; // enter the file name here to write to the file
  20.  
  21. void ReadAllDataFromFile(Vector<Weather> &WeatherVec,string FileName, Bst<Weather> &WeatherTree); // used to read the record from the file and store in class Vector obj
  22. void PrintMenu(); // print the menu options
  23. float CalAvgWindSpeed(Vector<Weather> & Vec,unsigned month, unsigned year); // calculatate the average windspeed of the month and year
  24. float CalAvgTemp(Vector<Weather> & Vec,unsigned month, unsigned year); // calculate the average temperature of the month and year
  25. float CalSolarRad(Vector<Weather> &WeatherVecObj, unsigned Month, unsigned Year); // calculate the solar radiation of the month and year
  26. string GetMonthName(unsigned month); // get the string name of the month
  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 Option1(Bst<Weather> &WeatherTree,multimap<int,Weather> MonthMap); //do the task of option 1
  33. void Option2(Bst<Weather> &WeatherTree); // do the task of option 2
  34. void Option3(Bst<Weather> &WeatherTree); // do the task of option 3
  35. void Option4(Bst<Weather> &WeatherTree); // do the task of option 4
  36. void PrintOption1Output(unsigned Month,unsigned Year,float AverageTemperature,float AverageWindSpeed); // print option  1 to screen
  37. void PrintOption2OutPut(unsigned Month, float AverageTemperature, float AverageWindSpeed); // print option 2 output to screen
  38. void PrintOption3Output(unsigned Month,float Solar_Kwh_Msqr); // print option 3 output to screen
  39. void PrintOption4ToCsv(unsigned Month,float Solar_Kwh_Msqr,float AvgWindSpeed,float AvgTemperature,unsigned Year,ofstream &Outfile); // print option 4 to screen
  40.  
  41.  
  42.  
  43.  
  44. int main ()
  45. {
  46.     //----------------------------Initialising and declaration of variable--------------------------
  47.  
  48.     int Choice=0;
  49.     string S_Choice ="",IgnoreLine="",InsertIntoBst;
  50.  
  51. //-----------------------------------------------------------------------------------------------
  52.  
  53.     Vector<Weather> WeatherVec; // create a Vector object of type Weather and name it WeatherVec
  54.     Bst<Weather> WeatherTree;
  55.     multimap<int,Weather> MonthMap;
  56.     ifstream infile; // instantiate ifstream to read file
  57.     infile.open(FileName); // open file through ifstream
  58.     if(infile.is_open())
  59.     {
  60.         // check if the file is open
  61.         ReadAllDataFromFile(WeatherVec,FileName,WeatherTree); // call the function to store all the record in Vector obj
  62.         infile.close(); // close the file
  63.         PrintMenu(); // print the menu options
  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.         {
  69.             // if incorrect user input prompt and get again
  70.             cout<<"Please enter your choice again 1 - 5"<<endl;
  71.             cin>>S_Choice;
  72.         }
  73.         cout<<endl; // print new line
  74.  
  75.         while (Choice!=5) // if the choice inputed by the user not equal 5 enter while loop
  76.         {
  77.             switch(Choice)
  78.             {
  79.             // check the choice value
  80.             case 1:
  81.  
  82.  
  83.                 Option1(WeatherTree,MonthMap); // call option 1 functions
  84.  
  85.  
  86.  
  87.                 break;
  88.  
  89.             case 2:
  90.  
  91.  
  92.                 Option2(WeatherTree); // call option 2 functions
  93.  
  94.  
  95.  
  96.  
  97.                 break;
  98.  
  99.             case 3:
  100.  
  101.                 Option3(WeatherTree); // call option 3 functions
  102.  
  103.  
  104.                 break;
  105.  
  106.  
  107.             case 4:
  108.  
  109.                 Option4(WeatherTree); // call option 4 functions
  110.  
  111.                 break;
  112.  
  113.             }
  114.             cout<<endl;
  115.             PrintMenu(); // print the menu option again
  116.             cout<<"Please enter your choice again"<<endl; // prompt for the S_choice
  117.             cin>>S_Choice; // get S_Choice
  118.             cout<<endl;
  119.             while(CheckChoice(Choice,S_Choice)==false)
  120.             {
  121.                 // if user input incorrect prompt and get S_Choice again
  122.                 cout<<"Please enter your choice again 1 - 5 "<<endl;
  123.                 cin>>S_Choice;
  124.             }
  125.             if(Choice==5)
  126.             {
  127.                 // if the user input is = 5 break from the loop
  128.                 break;
  129.             }
  130.         }
  131.  
  132.     }
  133.     else
  134.     {
  135.         // if file is not opened print error
  136.         cout<<"Error reading file"<<endl;
  137.  
  138.     }
  139.     // print to screen program ended
  140.     cout<<"Program ended"<<endl;
  141.     return 0; // return to os
  142. }
  143.  
  144. void PrintMenu()
  145. {
  146.     // printing out the menu option titles
  147.     cout<<endl;
  148.     cout<<"1. Print the average wind speed and average ambient air temperature for a specified month and year to screen."<<endl;
  149.     cout<<endl;
  150.     cout<<"2. Print the Average wind speed and average ambient air temperature for each month of a specified year. "<<endl;
  151.     cout<<endl;
  152.     cout<<"3. Print the Total solar radiation in kWh/m2 for each month of a specified year.  "<<endl;
  153.     cout<<endl;
  154.     cout<<"4. Write the Average wind speed (km/h), average ambient air temperature and total solar radiation in \n"
  155.         <<"   kWh/m2 for each month of a specified year to file. "<<endl;
  156.     cout<<endl;
  157.     cout<<"5. Exit the program. "<<endl;
  158.     cout<<endl;
  159.  
  160. }
  161.  
  162.  
  163.  
  164. void PrintYearRequest()
  165. {
  166.  
  167.     cout<<"Please Enter the Year (Not earlier than 1998 and not later than 2020)"<<endl;
  168.     cout<<endl;
  169. }
  170.  
  171. void PrintMonthRequest()
  172. {
  173.  
  174.     cout<<"Please Enter the Month (Between 1 to 12) "<<endl;
  175.     cout<<endl;
  176. }
  177.  
  178. float CalAvgWindSpeed(Vector<Weather> & WeatherVecObj,unsigned month, unsigned year)
  179. {
  180.     // calculating average windspeed for the months of the year
  181.     float AverageWindSpeed=0.0,ConvertingSpeed=0.0;
  182.     unsigned I_Month=0,counter=0;
  183.     for(int i = 0; i<WeatherVecObj.GetLength(); i++) // loop till the last element of the vector
  184.     {
  185.         istringstream(WeatherVecObj[i].GetDate().GetMonth())>>I_Month; // convert string month to unsigned U_Month
  186.         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
  187.         {
  188.  
  189.             ConvertingSpeed = WeatherVecObj[i].GetSpeed()*3.6; // the windspeed of the element times 3.6 and assigned to Converting Speed
  190.             AverageWindSpeed += ConvertingSpeed; // AverageWindSpeed plus adding ConvertingSpeed in the loop
  191.             counter++; // increment counter by 1
  192.         }
  193.     }
  194.     if(AverageWindSpeed!=0)
  195.     {
  196.         // if avaerage wind speed not equals to 0
  197.         AverageWindSpeed = AverageWindSpeed/counter; //AverageWindSpeed is divided by counter
  198.         AverageWindSpeed = round(AverageWindSpeed*10)/10; // round up to the first decimal point
  199.     }
  200.     return  AverageWindSpeed;// return the average wind speed of the month of the year
  201.  
  202.  
  203. }
  204.  
  205. float CalSolarRad(Vector<Weather> &WeatherVecObj, unsigned Month, unsigned Year)
  206. {
  207.     // calculate solar radiation
  208.     float Total_Solar_Rad_kWh_Msqr=0.0,Get_Solar_Rad_W_Msqr=0.0,Cal_Solar_Rad_W_Msqr=0.0;
  209.     unsigned I_Month=0;
  210.  
  211.     for(int i = 0; i<WeatherVecObj.GetLength(); i++) // loop till the end of the Vector
  212.     {
  213.         istringstream(WeatherVecObj[i].GetDate().GetMonth())>>I_Month;// converting string month to unsigned I_Month
  214.  
  215.         if(WeatherVecObj[i].GetDate().GetYear() == Year && I_Month==Month && WeatherVecObj[i].GetSolarMSqr()>=100)
  216.         {
  217.             // if year and month = to specific year AND solar radiation value of the element is more or equal to 100 enter if statement
  218.  
  219.  
  220.             Get_Solar_Rad_W_Msqr = WeatherVecObj[i].GetSolarMSqr();// retrieve solar radiation of the element
  221.             Cal_Solar_Rad_W_Msqr = Get_Solar_Rad_W_Msqr*1/6; // converting the solar radiation
  222.             Cal_Solar_Rad_W_Msqr = Cal_Solar_Rad_W_Msqr/1000; // calculating the solar radiation
  223.             Total_Solar_Rad_kWh_Msqr += Cal_Solar_Rad_W_Msqr; // plus adding the solar radiation to Total_Solar_Rad_kWh_Msqr
  224.         }
  225.     }
  226.  
  227.     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
  228.  
  229.     return Total_Solar_Rad_kWh_Msqr; // return the total solar radiation
  230.  
  231. }
  232.  
  233. float CalAvgTemp(Vector<Weather> & WeatherVecObj,unsigned month, unsigned year)
  234. {
  235.     float ConvertingTemp=0.0,AverageTemperature=0.0;
  236.     unsigned I_Month,counter=0;
  237.  
  238.     for(int i = 0; i<WeatherVecObj.GetLength(); i++)
  239.     {
  240.         istringstream(WeatherVecObj[i].GetDate().GetMonth())>>I_Month; // converting string month to unsigned I_Month
  241.         if(WeatherVecObj[i].GetDate().GetYear() == year && I_Month==month)
  242.         {
  243.             // if year and month = to specific year AND Month
  244.             ConvertingTemp += WeatherVecObj[i].GetTemperature(); // adding to the ConvertingTemp of the temperature of each of the element
  245.             counter++; // increment counter by 1 every loop
  246.         }
  247.     }
  248.     if(ConvertingTemp!=0)
  249.     {
  250.         // if ConvertingTemp not equals to 0
  251.  
  252.         AverageTemperature = ConvertingTemp/counter; // ConvertingTemp divided by counter to get average
  253.         AverageTemperature = round(AverageTemperature*10)/10; // round up to the nearing tenth
  254.     }
  255.     return AverageTemperature;
  256. }
  257.  
  258.  
  259.  
  260. string GetMonthName(unsigned month)
  261. {
  262.     // return the string name of the month entered in the parameter
  263.     string Month = "";
  264.     if(month == 1)
  265.     {
  266.         Month = "January";
  267.         return Month;
  268.     }
  269.     else if (month == 2)
  270.     {
  271.         Month = "February";
  272.         return Month;
  273.     }
  274.     else if (month == 3)
  275.     {
  276.         Month = "March";
  277.         return Month;
  278.     }
  279.     else if (month == 4)
  280.     {
  281.         Month = "April";
  282.         return Month;
  283.     }
  284.     else if (month == 5)
  285.     {
  286.         Month = "May";
  287.         return Month;
  288.     }
  289.     else if (month == 6)
  290.     {
  291.         Month = "June";
  292.         return Month;
  293.     }
  294.     else if (month == 7)
  295.     {
  296.         Month = "July";
  297.         return Month;
  298.     }
  299.     else if (month == 8)
  300.     {
  301.         Month = "August";
  302.         return Month;
  303.     }
  304.     else if (month == 9)
  305.     {
  306.         Month = "September";
  307.         return Month;
  308.     }
  309.     else if (month == 10)
  310.     {
  311.         Month = "October";
  312.         return Month;
  313.     }
  314.     else if (month == 11)
  315.     {
  316.         Month = "November";
  317.         return Month;
  318.     }
  319.     else if (month == 12)
  320.     {
  321.         Month = "December";
  322.         return Month;
  323.     }
  324.     return Month;
  325. }
  326.  
  327.  
  328.  
  329.  
  330.  
  331. bool CheckMonth(unsigned &Month, string S_Month)
  332. {
  333.     // method to error check the month input
  334.     unsigned U_Month;
  335.     istringstream(S_Month)>> U_Month; // convert string month to unsigned U_Month
  336.     if(U_Month>0 && U_Month<13)
  337.     {
  338.         // if U_Month is more than 0 and less than 13 set Month and return true
  339.         Month = U_Month;
  340.         return true;
  341.     }
  342.     else
  343.     {
  344.         // else return false
  345.         return false;
  346.     }
  347.  
  348. }
  349.  
  350. bool CheckYear(unsigned &Year, string S_Year)
  351. {
  352.     // method to error check the year  input
  353.     unsigned U_Year;
  354.     istringstream(S_Year)>>U_Year; // convert string month to unsigned U_Year
  355.     if(U_Year>=1998 && U_Year<2021)
  356.     {
  357.         // if U_Year  is more than 1000 and less than 2021 set Year  and return true
  358.         Year = U_Year; //
  359.         return true;
  360.     }
  361.     else
  362.     {
  363.         // if condition not met return false
  364.         return false;
  365.     }
  366. }
  367.  
  368. void Option1(Bst<Weather> &WeatherTree,multimap<int,Weather> MonthMap)
  369. {
  370.     // if Choice = 1
  371.     unsigned C_Year=0,C_Month=0,Temp_Month=0;
  372.     float AvgWindSpeed=0,AvgTemperature=0;
  373.     string S_Month ="", S_Year="";
  374.     Vector<Weather> YearVec;
  375.     Weather TempObj;
  376.     Vector<Weather> MonthVec;
  377.     PrintMonthRequest(); // prompt S_Month
  378.     cin>>S_Month; //get S_Month
  379.     while (CheckMonth(C_Month, S_Month)==false)
  380.     {
  381.         // if the C_Month is incorrect enter prompt and get again
  382.         PrintMonthRequest();
  383.         cin>>S_Month;
  384.     }
  385.  
  386.     PrintYearRequest(); // prompt for S_Year
  387.     cin>>S_Year; // get S_Year
  388.     while (CheckYear(C_Year,S_Year)==false)
  389.     {
  390.         // if the C_Year is incorrect enter prompt and get again
  391.         PrintYearRequest();
  392.         cin>>S_Year;
  393.     }
  394.     if(WeatherTree.Search(C_Year)) // check if the key exist in the bst
  395.     {
  396.         YearVec = WeatherTree.ReturnVec(C_Year); // if the key exist return the Vector of the Key
  397.         {
  398.             for(int i = 0; i<YearVec.GetLength(); i++) // loop through the vector
  399.             {
  400.                 istringstream(YearVec[i].GetDate().GetMonth())>>Temp_Month; // convert the string to unsigned
  401.                 if(Temp_Month == C_Month)
  402.                 {
  403.                     TempObj = YearVec[i]; // if the weather obj in the element is = to c_Month then assign to TempObj
  404.                     MonthMap.insert(pair<int,Weather>(C_Month,TempObj)); //Pair the Month together with the tempObj in multimap
  405.                 }
  406.             }
  407.         }
  408.         for(auto elem: MonthMap) // for each element in MonthMap
  409.         {
  410.             MonthVec.PushBack(elem.second); // Insert the value into MonthVec
  411.         }
  412.         AvgWindSpeed= CalAvgWindSpeed(MonthVec,C_Month, C_Year); // calculate average windspeed
  413.         AvgTemperature= CalAvgTemp(MonthVec,C_Month,C_Year); // calculate average temperature
  414.  
  415.     }
  416.     else
  417.     {
  418.         cout<<"No Data"<<endl;
  419.     }
  420.  
  421.     PrintOption1Output(C_Month,C_Year,AvgTemperature,AvgWindSpeed); // print the output to screen*/
  422.  
  423.  
  424. }
  425.  
  426. void PrintOption1Output(unsigned Month,unsigned Year,float AverageTemperature,float AverageWindSpeed)
  427. {
  428.     // print output option
  429.     if(AverageTemperature==0 && AverageWindSpeed==0)
  430.     {
  431.         // check if windspeed and temperature is 0
  432.         cout<< GetMonthName(Month)<<" "<<Year<<" : No data"<<endl;
  433.     }
  434.     else
  435.     {
  436.         cout<< GetMonthName(Month)<<" "<<Year<<" : "; // print the month name and the year followed by windspeed and temperature
  437.         cout<<AverageWindSpeed<<" km/h, "<< AverageTemperature<<" degrees C"<<'\n';
  438.         cout<<endl;
  439.     }
  440. }
  441.  
  442. void Option2(Bst<Weather> &WeatherTree)
  443. {
  444.     unsigned C_Year=0;
  445.     float AvgWindSpeed=0,AvgTemperature=0;
  446.     string S_Year="";
  447.     Vector<Weather> YearVec;
  448.     PrintYearRequest();// prompt for S_Year
  449.     cin>>S_Year;// get S_Year
  450.     while (CheckYear(C_Year,S_Year)==false)
  451.     {
  452.         // if the C_Year is incorrect enter prompt and get again
  453.         PrintYearRequest();
  454.         cin>>S_Year;
  455.     }
  456.  
  457.     if(WeatherTree.Search(C_Year)) // check if the key exist in the bst
  458.     {
  459.         YearVec = WeatherTree.ReturnVec(C_Year); // if the key exist return the Vector of the Key
  460.         for (unsigned i = 0; i<12; i++) // loop 12 times
  461.         {
  462.             AvgWindSpeed = CalAvgWindSpeed(YearVec,i+1, C_Year); // calculate the average windspeed of each month
  463.             AvgTemperature = CalAvgTemp(YearVec,i+1, C_Year); // caclualte the average temperature of each month
  464.             PrintOption2OutPut(i+1,AvgTemperature,AvgWindSpeed); // print the out put for each month
  465.         }
  466.     }
  467.     else
  468.     {// if key not found print No Data.
  469.         cout<<"No Data"<<endl;
  470.     }
  471. }
  472. void PrintOption2OutPut(unsigned Month, float AverageTemperature, float AverageWindSpeed)
  473. {
  474.     if(AverageTemperature!=0 && AverageWindSpeed!=0)
  475.     {
  476.         // print out if temperature and windspeed is not equal to 0
  477.         cout<< GetMonthName(Month)<<" : "; // print the month name and the year followed by windspeed and temperature
  478.         cout<<AverageWindSpeed<<" km/h, "<< AverageTemperature<<" degrees C"<<'\n';
  479.         cout<<endl;
  480.     }
  481.     else
  482.     {
  483.         // else print out no data for the month
  484.         cout<< GetMonthName(Month)<<" : No data"<<endl;
  485.         cout<<endl;
  486.     }
  487. }
  488.  
  489. void Option3(Bst<Weather> &WeatherTree)
  490. {
  491.     unsigned C_Year=0;
  492.     float Solar_Kwh_Msqr=0;
  493.     string S_Year="";
  494.     Vector<Weather> YearVec;
  495.     PrintYearRequest();// prompt for S_Year
  496.     cin>>S_Year;// get S_Year
  497.     while (CheckYear(C_Year,S_Year)==false)
  498.     {
  499.         // if the C_Year is incorrect enter prompt and get again
  500.         PrintYearRequest();
  501.         cin>>S_Year;
  502.     }
  503.     if(WeatherTree.Search(C_Year))// check if the key exist in the bst
  504.     {
  505.         YearVec = WeatherTree.ReturnVec(C_Year); // if the key exist return the Vector of the Key
  506.         for(unsigned i = 0; i<12; i++)
  507.         {
  508.             // loop 12 times
  509.             Solar_Kwh_Msqr = CalSolarRad(YearVec, i+1, C_Year); // calculate the solar radiation kwh/m2 for each month of the year
  510.             PrintOption3Output(i+1,Solar_Kwh_Msqr); // print the output of option 3
  511.         }
  512.     }
  513.     else
  514.     {// if key not found print No Data.
  515.         cout<<"No Data"<<endl;
  516.     }
  517. }
  518.  
  519.  
  520. void PrintOption3Output(unsigned Month,float Solar_Kwh_Msqr)
  521. {
  522.     if(Solar_Kwh_Msqr!=0)
  523.     {
  524.         // if the solar radiation is not equals to 0 print out the solar radiation in kWh/m2
  525.         cout<<GetMonthName(Month)<<": "<<Solar_Kwh_Msqr<<" kWh/m2"<<endl;
  526.         cout<<endl;
  527.     }
  528.     else
  529.     {
  530.         // print out the no data for the month
  531.         cout<<GetMonthName(Month)<<": No Data"<<endl;
  532.         cout<<endl;
  533.     }
  534. }
  535.  
  536. void Option4(Bst<Weather> &WeatherTree)
  537. {
  538.     unsigned C_Year=0;
  539.     float AvgWindSpeed=0,AvgTemperature=0, Solar_Kwh_Msqr=0;
  540.     string S_Year="";
  541.     Vector<Weather> YearVec;
  542.     ofstream Outfile; // instantiate the ofstream object to write file
  543.     Outfile.open(OutFileName); // open WindTempSolar.csv if not found create one
  544.  
  545.  
  546.     PrintYearRequest();// prompt for S_Year
  547.     cin>>S_Year;// get S_Year
  548.     while (CheckYear(C_Year,S_Year)==false)
  549.     {
  550.         // if the C_Year is incorrect enter prompt and get again
  551.         PrintYearRequest();
  552.         cin>>S_Year;
  553.     }
  554.     Outfile<<C_Year<<endl; // write the year to file
  555.     if(WeatherTree.Search(C_Year)) // check if the key exist in the bst
  556.     {
  557.         YearVec = WeatherTree.ReturnVec(C_Year); // if the key exist return the Vector of the Key
  558.         for( int i = 0; i<12; i++)
  559.         {
  560.             Solar_Kwh_Msqr = CalSolarRad(YearVec, i+1, C_Year);// calculate the solar radiation kwh/m2 for each month of the year
  561.             AvgTemperature =  CalAvgTemp(YearVec,i+1,C_Year);// caclualte the average temperature of each month
  562.             AvgWindSpeed = CalAvgWindSpeed(YearVec,i+1, C_Year);// calculate the average windspeed of each month
  563.             PrintOption4ToCsv(i+1,Solar_Kwh_Msqr,AvgWindSpeed,AvgTemperature,C_Year,Outfile); // write the records of each month of the year to the file
  564.         }
  565.     }
  566.     else // if key not found print No Data.
  567.     {
  568.         Outfile<<"No Data"<<endl;
  569.     }
  570.  
  571.     cout<<"Successfully wrote to "<<OutFileName<<" file"<<endl; // print to screen to indicate wrote successfully
  572.     Outfile.close(); // close the ofstream
  573. }
  574.  
  575. void PrintOption4ToCsv(unsigned Month,float Solar_Kwh_Msqr,float AvgWindSpeed,float AvgTemperature,unsigned Year,ofstream &Outfile)
  576. {
  577.  
  578.     if(Solar_Kwh_Msqr!=0 &&AvgWindSpeed!=0 && AvgTemperature!=0)
  579.     {
  580.         Outfile<<GetMonthName(Month)<<','<<AvgWindSpeed<<','<<AvgTemperature<<','<<Solar_Kwh_Msqr<<endl;
  581.         cout<<endl;
  582.     }
  583.     else
  584.     {
  585.         Outfile<<GetMonthName(Month)<<','<<"No Data"<<endl;
  586.         cout<<endl;
  587.     }
  588. }
  589.  
  590. void ReadAllDataFromFile(Vector<Weather> &WeatherVec,string FileName,Bst<Weather> &WeatherTree)
  591. {
  592.     //----------------------------------Variable and objects declaration and initialisation-----------------------------------
  593.     string IgnoreColumn,T_speed,getLine,T_Temperature="",S_Day,S_Month,S_Year,S_Hrs,S_Mins,IgnoreLine,S_Solar_M_Sqr,FileNames;
  594.     unsigned Temp_Day=0,Temp_Year=0;
  595.     float I_Speed=0,F_Temperature=0,F_Solar_M_Sqr=0;
  596.     int Temp_Hrs=0,Temp_Mins=0,ErrorFile = 0,LoadedFile =0;
  597.     Date T_DateObj;
  598.     Time T_TimeObj;
  599.     Weather T_WObj;
  600. //----------------------------------------------------------------------------------------------------------------------------
  601.     ifstream infile(FileName);
  602.     cout<<"Loading Files";
  603.     while(!infile.eof())
  604.     {
  605.         cout<<".";
  606.         getline(infile,FileNames);
  607.  
  608.         // loop till the end of file
  609.  
  610.         FileNames="data/"+FileNames;
  611.  
  612.         ifstream infiles (FileNames);
  613.         if (infiles.is_open())
  614.         {
  615.             cout<<".";
  616.             getline(infiles,IgnoreLine,'\n'); // read in the file line of the file which contains the titles
  617.  
  618.             while(!infiles.eof())
  619.             {
  620.                 getline(infiles,S_Day,'/');// read data till the delimiter /
  621.                 istringstream(S_Day)>>Temp_Day; // convert string day to unsigned day
  622.                 getline(infiles,S_Month,'/');// read data till the delimiter /
  623.                 getline(infiles,S_Year,' ');// read data till the delimiter ' '
  624.                 istringstream (S_Year)>>Temp_Year;// convert string year to unsigned year
  625.                 getline(infiles,S_Hrs,':');// read data till the delimiter :
  626.                 istringstream(S_Hrs)>>Temp_Hrs;// convert string hours to int hours
  627.                 getline(infiles,S_Mins,',');// read data till the delimiter ,
  628.                 istringstream(S_Mins)>>Temp_Mins;// convert string minutes to int minutes
  629.                 T_DateObj.SetDay(Temp_Day);//Set the day to date object T_DateObj
  630.                 T_DateObj.SetMonth(S_Month);// set the month to date object T_DateObj
  631.                 T_DateObj.SetYear(Temp_Year); // set the year  to date object T_DateObj
  632.                 T_TimeObj.SetHours(Temp_Hrs); // set the hours to time object T_TimeObj
  633.                 T_TimeObj.SetMins(Temp_Mins); // set the minutes to time object T_TimeObj
  634.                 T_WObj.SetDate(T_DateObj); //set the date T_WObj weather object
  635.                 T_WObj.SetTime(T_TimeObj); // set the time T_WObj weather object
  636.  
  637.                 for(int i =0; i<9; i++)
  638.                 {
  639.                     // loop 9 times to ignore 9 columns
  640.                     getline(infiles,IgnoreColumn,',');
  641.  
  642.                 }
  643.                 getline(infiles,T_speed,','); // read data till the delimiter ,
  644.                 istringstream(T_speed)>>I_Speed; // convert string windspeed to float windspeed
  645.                 T_WObj.SetSpeed(I_Speed);
  646.                 getline(infiles,S_Solar_M_Sqr,',');// read data till the delimiter ,
  647.                 istringstream(S_Solar_M_Sqr)>>F_Solar_M_Sqr; // convert string solar radiation to float solar radiation
  648.                 T_WObj.SetSolarMSqr(F_Solar_M_Sqr); // set the solar radiation to T_WObj weather object
  649.                 for(int i =0; i<5; i++)
  650.                 {
  651.                     // loop 5 times to ignore 5 columns
  652.                     getline(infiles,IgnoreColumn,',');// read data till the delimiter ,
  653.                 }
  654.  
  655.                 getline(infiles,T_Temperature,'\n');// read data till the delimiter \n
  656.                 if(!T_Temperature.empty()) // check if the temperature string is not empty
  657.                 {
  658.                     istringstream(T_Temperature)>>F_Temperature; // convert the string temperature to float temperature
  659.                     T_WObj.SetTemperature(F_Temperature); // set temperature to T_WObj weather object
  660.                     WeatherTree.Insert(Temp_Year,T_WObj); // insert the object into Bst Vector according to the Key(Temp_Year)
  661.  
  662.                 }
  663.                 T_Temperature = ""; // erase the value of temperature
  664.  
  665.             }
  666.             LoadedFile++; // count the successfully opened files
  667.         }
  668.         else
  669.         {
  670.             cout<<endl;
  671.             cout<<"Failed to open: "<<FileNames<<endl; // print the failed to open file name to screen
  672.             cout<<"Loading Files";
  673.             ErrorFile++; // count the files that failed to open
  674.         }
  675.     }
  676.     cout<<endl;
  677.     cout<<"Loading Completed"<<endl;
  678.     cout<<"Total files loaded: "<<LoadedFile<<" "<<"Number of files not loaded: "<<ErrorFile<<" "<<endl;
  679.  
  680. }
  681.  
  682.  
  683. bool CheckChoice(int &I_Choice,string S_choice)
  684. {
  685.     // error checking the choice user input
  686.     int T_Choice; //
  687.  
  688.  
  689.     istringstream(S_choice)>>T_Choice;// convert string choice to int choice
  690.  
  691.     if(T_Choice>0 && T_Choice <6)
  692.     {
  693.         // int choice is more than 0 AND less than 5
  694.         I_Choice = T_Choice; // set I_Choice to int T_Choice
  695.  
  696.         return true;// return true
  697.     }
  698.     else
  699.     {
  700.         //if condition not met return false
  701.         return false;
  702.     }
  703.  
  704. }
  705.  
  706.  
  707.  
  708. #include "date.h"
  709.  
  710. Date::Date()
  711. {
  712.     m_day = 0; // initialise the m_day
  713.     m_month="Invalid Month";// initialise the m_month
  714.     m_year = 0; // initialise m_year
  715. }
  716.  
  717. Date::~Date()
  718. {
  719.     m_day = 0; // overwrite the m_day
  720.     m_month="Invalid Month";// overwrite the m_month
  721.     m_year = 0; // overwrite the m_year
  722.  
  723. }
  724.  
  725.  
  726. bool Date::SetDay(unsigned day )
  727. {
  728.     if(day > 0 && day <= 31)
  729.     { // if day is more than 0 and less or equal to 31
  730.         m_day = day; // set  m_day to day
  731.         return true; // return true
  732.     }
  733.     else
  734.     { // condition not met return false
  735.         return false;
  736.     }
  737.  
  738. }
  739.  
  740. unsigned Date::GetDay() const
  741. { // return value of m_day
  742.     return m_day;
  743. }
  744.  
  745. bool Date::SetMonth(string month)
  746. {
  747.     unsigned U_Month;
  748.     istringstream(month)>>U_Month; // convert string month to unsigned U_Month
  749.     if(U_Month>0 && U_Month<13)
  750.     { // if I_Month more than 0 and less than 13
  751.         m_month=month; // set m_month = month
  752.         return true;
  753.     }
  754.     else
  755.     { // if condition not met return false
  756.         return false;
  757.     }
  758.  
  759. }
  760. string Date::GetMonth() const
  761. {
  762.     return m_month; // return the value of month of the class
  763. }
  764.  
  765. bool Date::SetYear(unsigned year)
  766. {
  767.     if(year>1998 && year<=2020)
  768.     { // if year more than 1000 AND year less or equal to 2020
  769.         m_year = year; // set m_year = year and return true
  770.         return true;
  771.     }
  772.     else
  773.     {// if condition not met return false
  774.         return false;
  775.     }
  776.  
  777. }
  778.  
  779. unsigned Date:: GetYear() const
  780. { //return the year of the class
  781.     return m_year;
  782. }
  783.  
  784.  
  785.  
  786. #include "time.h"
  787.  
  788. Time::Time()
  789. { // inititalise the members of the class
  790.     m_Hours = 0;
  791.     m_Mins = 0;
  792. }
  793.  
  794. Time::~Time()
  795. { // erase or overwrite the value of instantiated Time class
  796.     m_Hours = 0;
  797.     m_Mins = 0;
  798. }
  799.  
  800. bool Time::SetHours(int Hours)
  801. { // set Hours equals to m_Hours if Hour is more or equal to zero AND less than 24
  802.     if(Hours >=0 && Hours<24)
  803.     {
  804.          m_Hours = Hours;
  805.          return true;
  806.     }
  807.     else
  808.     {// if condition not met then return false
  809.         return false;
  810.     }
  811.  
  812. }
  813.  
  814. bool Time::SetMins(int minutes)
  815. { // if minutes more to equal to zero AND minutes less than 60
  816.     if (minutes >=0 && minutes <60)
  817.     {// set member m_Mins to minutes
  818.         m_Mins = minutes;
  819.         return true;
  820.     }
  821.     else
  822.     {// return false if condition not met
  823.         return false;
  824.     }
  825.  
  826. }
  827. int Time::GetHours() const
  828. { // return the value of hours of the class
  829.     return m_Hours;
  830. }
  831. int Time::GetMins() const
  832. {// return the value of minutes of the class
  833.     return m_Mins;
  834. }
  835.  
  836.  
  837.  
  838. #include "weather.h"
  839.  
  840. Weather::Weather()
  841. {// initialise the value of members
  842.     m_Speed = 0;
  843.     m_Temperature = 0;
  844.     m_Solar_M_Sqr = 0;
  845.  
  846.  
  847. }
  848.  
  849. Weather::~Weather()
  850. { //// erase the value or overwritting the value of members
  851.     m_Speed = 0;
  852.     m_Temperature = 0;
  853.     m_Solar_M_Sqr = 0;
  854.  
  855. }
  856.  
  857.  
  858.  
  859. bool Weather:: SetSpeed( float T_Speed)
  860. { // check if T_Speed is more or equal to 0
  861.     if(T_Speed >=0 )
  862.     { // if yes set member speed to T_speed
  863.         m_Speed = T_Speed;
  864.         return true;
  865.     }
  866.     else
  867.     {// if not return false
  868.         return false;
  869.     }
  870.  
  871. }
  872.  
  873.  
  874. void Weather:: SetTemperature( float T_Temperature)
  875. {
  876.  // set m_Temperature to T_Temperature
  877.     m_Temperature = T_Temperature;
  878. }
  879.  
  880.  
  881. void  Weather::SetDate(Date T_DateObj)
  882. { // set m_DateObj = Date T_DateObj
  883.     m_DateObj = T_DateObj;
  884. }
  885.  
  886.  
  887. void Weather::SetTime(Time T_TimeObj)
  888. {
  889.     m_TimeObj = T_TimeObj;
  890. } // set m_TimeObj to T_TimeObj
  891.  
  892.  
  893.  float Weather::GetSpeed() const
  894. {// return the value of windspeed of the class
  895.     return m_Speed;
  896. }
  897.  
  898.  float Weather::GetTemperature() const
  899. {
  900.     return m_Temperature;
  901. }
  902.  
  903. void Weather::SetSolarMSqr(float SolarMSqr)
  904. { // set m_Solar_M_Sqr = SolarMSqr
  905.     m_Solar_M_Sqr = SolarMSqr;
  906. }
  907.  
  908. float Weather::GetSolarMSqr()
  909. { // return the value of m_Solar_M_Sqr
  910.     return m_Solar_M_Sqr;
  911. }
  912.  
  913. Date Weather::GetDate() const
  914. { // return the of the Date of the class
  915.     return m_DateObj;
  916. }
  917.  
  918. Time Weather::GetTime() const
  919. { // return the m_TimeObj value
  920.     return m_TimeObj;
  921. }
  922.  
  923.  
  924.  
  925.  
  926. #ifndef VECTOR_H
  927. #define VECTOR_H
  928. #include <iostream>
  929.  
  930. #define DefaultSize 10
  931.  
  932. //---------------------------------------------------------------
  933. /**
  934. *@class Vector
  935. *@brief The Vector template that holds the inputted data
  936. *
  937. *
  938. *The Vector Template can be created in with any data type
  939. *it is used to hold the inputted data from the file
  940. *and store it.
  941. *
  942. *
  943. *
  944. *
  945. *@author Tan Jun Qi
  946. *@version 10
  947. *@date 19/June/2020 Singapore,Singapore
  948. *
  949. *
  950. *
  951. */
  952. //---------------------------------------------------------------------------
  953.  
  954. using namespace std;
  955.  
  956. template<class T>
  957. class Vector
  958. {
  959. public:
  960.     /**
  961.     *@brief The Default Vector constructor
  962.     *
  963.     *This initialise the member or attributes of time class and
  964.     *allocate memory space in the heap memory for the dynamic array
  965.     *
  966.     *
  967.     *@return void
  968.     *
  969.     */
  970.     Vector();
  971.     /**
  972.      *
  973.      *@fn GetSize()
  974.      *
  975.      *@brief Function to return the total size of the array
  976.      *
  977.      *
  978.      *return the total size of the array to check if the array
  979.      *is expanding
  980.      *
  981.      *
  982.      *@return int
  983.      *
  984.      */
  985.     int GetSize() const ;
  986.     /**
  987.     *
  988.     *@fn IsEmpty()
  989.     *
  990.     *@brief Function to check if the array is empty
  991.     *
  992.     *
  993.     *check if there are any elements inside of the array
  994.     *return false if there is elements return true if its
  995.     *empty
  996.     *
  997.     *@return bool
  998.     *
  999.     */
  1000.     bool IsEmpty();
  1001.     /**
  1002.     *
  1003.     *@fn PushBack(T value)
  1004.     *
  1005.     *@brief Function to add in new data into array
  1006.     *
  1007.     *
  1008.     *Add the data into the last element of the array
  1009.     *
  1010.     *
  1011.     *
  1012.     *@return void
  1013.     *
  1014.     */
  1015.     void PushBack(T value);
  1016.     /**
  1017.     *
  1018.     *@fn PopBack()
  1019.     *
  1020.     *@brief Erase the last element of the array
  1021.     *
  1022.     *
  1023.     *Dis count the last element of the array and enable
  1024.     *it to be overwritten by new data
  1025.     *
  1026.     *return true if there is element in array and discounted
  1027.     *
  1028.     *return false if there is no element in the array
  1029.     *
  1030.     *@pre The array must have elements inside
  1031.     *@return bool
  1032.     *
  1033.     */
  1034.     bool PopBack();
  1035.     /**
  1036.     *
  1037.     *@fn Resize()
  1038.     *
  1039.     *@brief Resize the total size of the array
  1040.     *
  1041.     *
  1042.     *Expand the array if the element in the array is more
  1043.     *than half of the total size by 1.5 times
  1044.     *
  1045.     *
  1046.     *@return void
  1047.     *
  1048.     */
  1049.     void Resize();
  1050.     /**
  1051.     *
  1052.     *@fn GetLength()
  1053.     *
  1054.     *@brief Resize the length of the array
  1055.     *
  1056.     *
  1057.     *return the total number of elements residing inside
  1058.     *of the array.
  1059.     *
  1060.     *@return int
  1061.     *
  1062.     */
  1063.     int GetLength() const;
  1064.     /**
  1065.     *
  1066.     *@overload T& operator[](int index) const
  1067.     *
  1068.     *@brief Overloading of [] operator
  1069.     *
  1070.     *overload the [] to return the element of the array
  1071.     *passed in to the paramemter
  1072.     *
  1073.     *@param index - int type
  1074.     *@return T&
  1075.     *
  1076.     */
  1077.  
  1078.  
  1079.     T& operator[](int index) const;
  1080.     /**
  1081.     *
  1082.     *@fn Vector();
  1083.     *
  1084.     *@brief The destructor of Vector
  1085.     *
  1086.     *Release the memory back to the OS and set the
  1087.     *memory location of the array to null.
  1088.     *
  1089.     *
  1090.     *@return void
  1091.     *
  1092.     */
  1093.  
  1094.     ~Vector();
  1095.  
  1096.     /**
  1097.     *
  1098.     *@overload operator = (const Vector<T> &Vec)
  1099.     *
  1100.     *@brief The = operator overloader
  1101.     *
  1102.     *Calls the copy function and return this object.
  1103.     *
  1104.     *
  1105.     *@param Vec - Vector of type class.
  1106.     *@return Vector<T>&
  1107.     *
  1108.     */
  1109.     Vector<T>&  operator = (const Vector<T> &Vec);
  1110.     /**
  1111.     *
  1112.     *
  1113.     *@brief The copy constructor
  1114.     *
  1115.     *Calls the copy function
  1116.     *
  1117.     *
  1118.     *@param Vec - Vector of type class.
  1119.     *
  1120.     */
  1121.     Vector(const Vector<T>& Vec);
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127. private:
  1128.     /// Holds the number of element in the array
  1129.     int m_length;
  1130.     /// The dynamic array that holds the records
  1131.     T *m_array;
  1132.     /// Hold the total size of the array
  1133.     int m_Tsize;
  1134.     /**
  1135.     *
  1136.     *@fn Copy(const Vector<T> & Vec)
  1137.     *
  1138.     *@brief The copy function
  1139.     *
  1140.     *The function copies the data in the elements in other Vector
  1141.     *to this Vector object (Deep copying)
  1142.     *
  1143.     *
  1144.     *@param Vec - Vector of type class.
  1145.     *@return bool
  1146.     *
  1147.     */
  1148.     bool Copy(const Vector<T> & Vec);
  1149. };
  1150.  
  1151.  
  1152.  
  1153. template<class T>
  1154. Vector<T>::Vector()
  1155. {
  1156.     // initiate member variables to
  1157.     m_length = 0;
  1158.     m_array = new T[DefaultSize]; // allocate new memory location on heaps for the dynamic array
  1159.     m_Tsize = DefaultSize;
  1160. }
  1161. template<class T>
  1162. Vector<T>::Vector(const Vector<T>& Vec) // copy constructor
  1163. {
  1164.     Copy(Vec); // calls the copy function
  1165. }
  1166.  
  1167. template<class T>
  1168. bool Vector<T>::Copy(const Vector<T> & Vec) // copy function
  1169. {
  1170.     if(Vec.m_array!=nullptr && Vec.GetLength()!=0 && Vec.GetSize()!=0)
  1171.     { // if other Vec m_array!=null and vec not empty and size not zero
  1172.         m_length = Vec.GetLength(); // assigning Other vect's data to this object members
  1173.         m_Tsize = Vec.GetSize();
  1174.         m_array = new T[Vec.GetSize()]; // allocating new memory spaces for m_array
  1175.         for(int i = 0; i<Vec.GetLength(); i++)
  1176.         {
  1177.             m_array[i] = Vec.m_array[i]; // assigning other Vector array elements to this object
  1178.         }
  1179.         return true;
  1180.     }
  1181.     else
  1182.     {
  1183.         return false;
  1184.     }
  1185. }
  1186. template<class T>
  1187. Vector<T>& Vector<T>::operator = (const Vector<T> &Vec)
  1188. {
  1189.     Copy(Vec); // calls the copy function
  1190.     return *this; // return this object
  1191. }
  1192.  
  1193.  
  1194. template<class T>
  1195. Vector<T>::~Vector()
  1196. {
  1197.     // release the memory location on heap back to os if dynamic array is not null
  1198.     if(m_array!=nullptr)
  1199.     {
  1200.         delete[] m_array;
  1201.         m_array = nullptr;
  1202.     }
  1203.     m_Tsize = 0;
  1204.     m_length = 0;
  1205. }
  1206.  
  1207. template<class T>
  1208. int Vector<T>::GetLength() const
  1209. {
  1210.     // return the length of the array
  1211.     return m_length;
  1212. }
  1213.  
  1214. template<class T>
  1215. int Vector<T>::GetSize() const
  1216. {
  1217.     // return the total size of the array
  1218.     return m_Tsize;
  1219. }
  1220.  
  1221.  
  1222. template<class T>
  1223. void Vector<T>::Resize()
  1224. {
  1225.     // resize the array by 1.5 times its original size
  1226.     m_Tsize = m_Tsize*1.5;
  1227.     T *newarray = new T[m_Tsize]; // open new dyanmic array pointer
  1228.     for (int i = 0; i<m_length; i++)
  1229.     {
  1230.         // allocate old elements in  array to new array
  1231.         newarray[i] = m_array[i];
  1232.     }
  1233.     delete [] m_array; // delete old array
  1234.     m_array = nullptr; // set old array to null
  1235.     m_array = newarray; // allocate the memory location of new array to m_array
  1236.  
  1237. }
  1238.  
  1239. template<class T>
  1240. bool Vector<T>::IsEmpty()
  1241. {
  1242.     // return true if there is not element in the array
  1243.     if (m_length == 0)
  1244.     {
  1245.         return true;
  1246.     }
  1247.     else
  1248.     {
  1249.         return false;
  1250.     }
  1251. }
  1252.  
  1253.  
  1254. template<class T>
  1255. void Vector<T>::PushBack(T value)
  1256. {
  1257.     // add elements to the back of the array
  1258.     m_array[m_length] = value;
  1259.     m_length ++; // if the array element is more than half full
  1260.     if(m_length>m_Tsize/2)
  1261.     {
  1262.         // call Resize function to resize the array
  1263.         Resize();
  1264.  
  1265.     }
  1266.  
  1267. }
  1268.  
  1269. template<class T>
  1270. bool Vector<T>::PopBack()
  1271. {
  1272.     // if there is element in the array, minus the length of the array by 1
  1273.     if(m_length!=0)
  1274.     {
  1275.         // the last element then is available to be overwritten or not in used
  1276.         m_array[m_length] = 0;
  1277.         m_length--;
  1278.         return true;
  1279.     }
  1280.     else
  1281.     {
  1282.         // if the length if empty return false
  1283.         return false;
  1284.     }
  1285. }
  1286.  
  1287. template<class T>
  1288. T& Vector<T>::operator[](int index) const
  1289. {
  1290.     // return the array element when [] is called
  1291.     return m_array[index];
  1292. }
  1293.  
  1294.  
  1295.  
  1296.  
  1297.  
  1298. #endif // VECTOR_H
  1299.  
  1300. #include <iostream>
  1301. #include "Vector.h"
  1302.  
  1303. //---------------------------------------------------------------
  1304. /**
  1305. *@class Bst
  1306. *@brief The Sorting of the data through the use of Key
  1307. *
  1308. *
  1309. *
  1310. *Used the key to distinguish with Data should be under which node,
  1311. *then the data enters into the Vector of that node. The key will not have any
  1312. *duplicates
  1313. *
  1314. *
  1315. *
  1316. *
  1317. *@author Tan Jun Qi
  1318. *@version 4
  1319. *@date 22/July/2020 Singapore,Singapore
  1320. *
  1321. *
  1322. *
  1323. */
  1324. //---------------------------------------------------------------------------
  1325.  
  1326.  
  1327. using namespace std;
  1328.  
  1329. /**
  1330.     *@brief Node Struct
  1331.     *
  1332.     *This struct contains Key for identification, Vector for storing of datas,
  1333.     *left and right points. This struct is used to create the structure of the
  1334.     *Binary search tree, nodes with the key less than the root's key will be
  1335.     *placed on the left and if more than root's key will be placed on the right.
  1336.     */
  1337.  
  1338. template <class T>
  1339. struct Node  // answer why struct encapsulation is used - rationale
  1340. {
  1341.     /// Used to identify the struct
  1342.     int Key;
  1343.     /// Used to store datas with the same key
  1344.     Vector <T> YearsData;
  1345.     /// left pointer
  1346.     Node <T> *Left;
  1347.     /// right pointer
  1348.     Node <T> *Right;
  1349. };
  1350.  
  1351.  
  1352. template <class T>
  1353. class Bst
  1354. {
  1355.  
  1356. public:
  1357.  
  1358.     /**
  1359.     *@brief The Default constructor
  1360.     *
  1361.     *This constructor points the m_Root member to nullptr
  1362.     *
  1363.     *
  1364.     *
  1365.     */
  1366.     Bst();
  1367.     /**
  1368.     *@brief The Destructor
  1369.     *
  1370.     *This Destructor called the "void Delete(Node<T> *CurrentNode)"
  1371.     *functions to delete all the nodes in the tree.
  1372.     *
  1373.     *
  1374.     *
  1375.     */
  1376.     ~Bst();
  1377.     /**
  1378.     *
  1379.     *@fn typedef void (*InOrderTraversalPtr)(const  T&)
  1380.     *
  1381.     *@brief Declaration of function pointers with the signature
  1382.     *
  1383.     *This function pointers take in another function with the same signature to
  1384.     *perform the operation/task of that function.
  1385.     *
  1386.     *
  1387.     *
  1388.     *@param T& - Template data type
  1389.     *@return void
  1390.     *
  1391.     */
  1392.     typedef void (*InOrderTraversalPtr)(const  T&);
  1393.     /**
  1394.     *
  1395.     *@fn typedef void (*PreOrderTraversalPtr)(const T&)
  1396.     *
  1397.     *@brief Declaration of function pointers with the signature
  1398.     *
  1399.     *This function pointers take in another function with the same signature to
  1400.     *perform the operation/task of that function.
  1401.     *
  1402.     *
  1403.     *
  1404.     *@param T& - Template data type
  1405.     *@return void
  1406.     *
  1407.     */
  1408.     typedef void (*PreOrderTraversalPtr)(const T&);
  1409.     /**
  1410.     *
  1411.     *@fn typedef void (*PostOrderTraversalPtr)(const T&)
  1412.     *
  1413.     *@brief Declaration of function pointers with the signature
  1414.     *
  1415.     *This function pointers take in another function with the same signature to
  1416.     *perform the operation/task of that function.
  1417.     *
  1418.     *
  1419.     *
  1420.     *@param T& - Template data type
  1421.     *@return void
  1422.     *
  1423.     */
  1424.     typedef void (*PostOrderTraversalPtr)(const T&);
  1425.     /**
  1426.     *
  1427.     *@fn Search(int Key) const
  1428.     *
  1429.     *@brief The Searching of the Key
  1430.     *
  1431.     *
  1432.     *This function performs the task of searching through the tree
  1433.     *by its Key, if the key is found in the tree returns true else
  1434.     *returns false.
  1435.     *
  1436.     *
  1437.     *@param Key - int data type
  1438.     *@return bool
  1439.     *
  1440.     */
  1441.     bool Search(int Key) const;
  1442.     /**
  1443.     *
  1444.     *@fn Insert(int Key,T DataObj)
  1445.     *
  1446.     *@brief Inserting of data with the Key and Data
  1447.     *
  1448.     *This function takes in two parameter, one which is the key and
  1449.     *another one which is the data. It will search through the tree if
  1450.     *the key exist or not. If the key exist, input the data into the vector
  1451.     *of that node. Else find the call the "void Insert(int Key, Node<T> * CurrentNode,T DataObj)"
  1452.     *to determine where the new node should be created and store the vector into the node identified
  1453.     *with the key.
  1454.     *
  1455.     *
  1456.     *@param Key - int data type
  1457.     *@param DataObj - Template Data type
  1458.     *@return void
  1459.     *
  1460.     */
  1461.     void Insert(int Key,T DataObj);
  1462.     /**
  1463.     *
  1464.     *@fn InOrderTraversal(InOrderTraversalPtr Print) const
  1465.     *
  1466.     *@brief In order Traversal function
  1467.     *
  1468.     *This function calls the
  1469.     *InOrderTraversal(Node<T> * CurrentNode, InOrderTraversalPtr Print) const
  1470.     *
  1471.     *@param Print - InOrderTraversalptr (Contains a function to be executed with the same signature)
  1472.     *@return void
  1473.     *
  1474.     */
  1475.     void InOrderTraversal(InOrderTraversalPtr Print)const;
  1476.     /**
  1477.     *
  1478.     *@fn PreOrderTraversal(PreOrderTraversalPtr Print)const
  1479.     *
  1480.     *@brief Pre order Traversal function
  1481.     *
  1482.     *This function calls the
  1483.     *PreOrderTraversal(Node<T> *CurrentNode, PreOrderTraversalPtr Print) const
  1484.     *
  1485.     *
  1486.     *@param Print - PreOrderTraversalPtr  (Contains a function to be executed with the same signature)
  1487.     *@return void
  1488.     *
  1489.     */
  1490.     void PreOrderTraversal(PreOrderTraversalPtr Print)const;
  1491.     /**
  1492.     *
  1493.     *@fn PostOrderTraversal(PostOrderTraversalPtr Print) const
  1494.     *
  1495.     *@brief Post order Traversal function
  1496.     *
  1497.     *
  1498.     *This function calls the
  1499.     *PostOrderTraversal(Node<T> *CurrentNode,PostOrderTraversalPtr Print) const
  1500.     *
  1501.     *
  1502.     *@param Print - PostOrderTraversalPtr  (Contains a function to be executed with the same signature)
  1503.     *@return void
  1504.     *
  1505.     */
  1506.     void PostOrderTraversal(PostOrderTraversalPtr Print) const;
  1507.  
  1508.     /**
  1509.     *
  1510.     *@fn ReturnVec(int Key)
  1511.     *
  1512.     *@brief Returns the Vector of the key node
  1513.     *
  1514.     *This functions returns the whole Vector of the key node where
  1515.     *the key node is user inputted.
  1516.     *
  1517.     *
  1518.     *
  1519.     *@param Key - int type
  1520.     *@return Vector<T>
  1521.     *
  1522.     */
  1523.     Vector<T> ReturnVec(int Key);
  1524.  
  1525.  
  1526.  
  1527.  
  1528. private:
  1529.     /// store the pointer of the Root node
  1530.     Node<T> *m_Root;
  1531.     /// store the pointer of a Node
  1532.     Node<T> *m_FindNode;
  1533.     /// store the Vector Object of a node
  1534.     Vector<T> m_ReturnVec;
  1535.     /**
  1536.     *
  1537.     *@fn Insert(int Key, Node<T> * CurrentNode,T DataObj)
  1538.     *
  1539.     *@brief Inserting of data with the Key, DataObj and the Node Pointer
  1540.     *
  1541.     *This function takes in 3 parameter, one which is the key and
  1542.     *another one which is the data. This function will find where the
  1543.     *location of the new node should be based on the key and create the
  1544.     *node. It will then identify the node using the Key and enter the Data into
  1545.     *it's Vector.
  1546.     *
  1547.     *
  1548.     *@param Key - int data type
  1549.     *@param DataObj - Template Data type
  1550.     *@param CurrentNode - Node pointer of template class type
  1551.     *@return void
  1552.     *
  1553.     */
  1554.     void Insert(int Key, Node<T> * CurrentNode,T DataObj);
  1555.     /**
  1556.     *
  1557.     *@fn InOrderTraversal(Node<T> * CurrentNode, InOrderTraversalPtr Print) const
  1558.     *
  1559.     *@brief In order Traversal function Overloader
  1560.     *
  1561.     *This function will traverse through the nodes in the tree
  1562.     *and print the key of the nodes. it will print the left leaf node
  1563.     *then the root and lastly the right leaf node in the respective manner.
  1564.     *The output should be in ascending order.
  1565.     *
  1566.     *@param Print - InOrderTraversalptr (Contains a function to be executed with the same signature)
  1567.     *@param CurrentNode - Node* of template class type
  1568.     *@return void
  1569.     *
  1570.     */
  1571.     void InOrderTraversal(Node<T> * CurrentNode, InOrderTraversalPtr Print) const ;
  1572.     /**
  1573.     *
  1574.     *@fn PostOrderTraversal(Node<T> *CurrentNode,PostOrderTraversalPtr Print) const
  1575.     *
  1576.     *@brief Post order Traversal function overloader
  1577.     *
  1578.     *This function will traverse through the nodes in the tree
  1579.     *and print the key of the nodes. it will print the left leaf node
  1580.     *then the right leaf node and lastly the root node in the respective manner.
  1581.     *
  1582.     *
  1583.     *@param Print - InOrderTraversalptr (Contains a function to be executed with the same signature)
  1584.     *@param CurrentNode - Node* of template class type
  1585.     *@return void
  1586.     *
  1587.     */
  1588.     void PostOrderTraversal(Node<T> *CurrentNode,PostOrderTraversalPtr Print)const ;
  1589.     /**
  1590.     *
  1591.     *@fn PreOrderTraversal(Node<T> *CurrentNode, PreOrderTraversalPtr Print) const
  1592.     *@brief Pre order Traversal function overloader
  1593.     *
  1594.     *This function will traverse through the nodes in the tree
  1595.     *and print the key of the nodes. it will print the Root node
  1596.     *then the left leaf node and lastly the right leaf node in the respective manner.
  1597.     *
  1598.     *
  1599.     *@param Print - InOrderTraversalptr (Contains a function to be executed with the same signature)
  1600.     *@param CurrentNode - Node* of template class type
  1601.     *@return void
  1602.     *
  1603.     */
  1604.     void PreOrderTraversal(Node<T> *CurrentNode, PreOrderTraversalPtr Print) const ;
  1605.     /**
  1606.     *
  1607.     *@fn Search(int Key, Node<T> *CurrentNode) const
  1608.     *
  1609.     *@brief The Searching of the Key
  1610.     *
  1611.     *
  1612.     *This function performs the task of searching through the tree
  1613.     *by its Key. The function will continue to search until the node is null
  1614.     *or the key is found if the key is found in the tree returns true else
  1615.     *returns false.
  1616.     *
  1617.     *
  1618.     *@param Key - int data type
  1619.     *@param CurrentNode - Node* of template class type
  1620.     *@return bool
  1621.     *
  1622.     */
  1623.     bool Search(int Key, Node<T> *CurrentNode) const;
  1624.     /**
  1625.     *
  1626.     *@fn Delete(Node<T> *CurrentNode)
  1627.     *
  1628.     *@brief The Deleting of the tree.
  1629.     *
  1630.     *
  1631.     *This function deletes the whole tree with the Post traversal concept
  1632.     *it deletes the node by, severing the connection to the heap memory
  1633.     *using the delete keyword and setting the node to nullptr.
  1634.     *
  1635.     *
  1636.     *@param CurrentNode - Node* of template class type
  1637.     *@return void
  1638.     *
  1639.     */
  1640.     void Delete(Node<T> *CurrentNode);
  1641.     /**
  1642.     *
  1643.     *@fn AssignVector(Node<T> * CurrentNode, int Key)
  1644.     *
  1645.     *@brief Assigning of Vector
  1646.     *
  1647.     *Search the tree with the Key and assign the Vector
  1648.     *of that node with the same key to Vector<T> m_ReturnVec
  1649.     *
  1650.     *
  1651.     *
  1652.     *@param Key - int data type
  1653.     *@param CurrentNode - Node* of template class type
  1654.     *@return void
  1655.     *
  1656.     */
  1657.     void AssignVector(Node<T> * CurrentNode, int Key);
  1658.     /**
  1659.     *
  1660.     *@fn ReturnNode(int Key,Node<T>* CurrentNode)
  1661.     *
  1662.     *@brief Assigning the Node
  1663.     *
  1664.     *Search the tree with the Key when the node with the same
  1665.     *key is found store the pointer of the node to Node<T> *m_FindNode
  1666.     *
  1667.     *
  1668.     *
  1669.     *@param Key - int data type
  1670.     *@param CurrentNode - Node* of template class type
  1671.     *@return void
  1672.     *
  1673.     */
  1674.     void ReturnNode(int Key,Node<T>* CurrentNode);
  1675.     /**
  1676.     *
  1677.     *@fn GetData(int & Data) const
  1678.     *
  1679.     *@brief Get the Key of the node
  1680.     *
  1681.     *It returns the value of the parameter, it is used when its traversing through tree.
  1682.     *
  1683.     *
  1684.     *
  1685.     *@param Data - int Reference
  1686.     *@return int Reference
  1687.     *
  1688.     */
  1689.     int& GetData(int & Data) const; //int& GetData(int & Data) const ;
  1690.  
  1691. };
  1692. //Default constructor
  1693. template <class T>
  1694. Bst<T>::Bst()
  1695. {
  1696.     m_Root = nullptr; // initialising the root
  1697. }
  1698.  
  1699. template <class T>
  1700. Bst<T>::~Bst()
  1701. {
  1702.     // delete the whole tree.
  1703.     Delete(m_Root);
  1704. }
  1705.  
  1706. template <class T>
  1707. Vector<T> Bst<T>:: ReturnVec(int Key)
  1708. {
  1709.     AssignVector(m_Root, Key); // assigning the vector to m_ReturnVec
  1710.     return m_ReturnVec; // return m_ReturnVec
  1711. }
  1712.  
  1713. template <class T>
  1714. void Bst<T>::Insert(int Key,T DataObj)// search the key and store the data.
  1715. {
  1716.     if(m_Root !=nullptr) // if root not null
  1717.     {
  1718.         if(!Search(Key)) // if key not found
  1719.         {
  1720.             Insert(Key,m_Root,DataObj);
  1721.         }
  1722.         else // if key found
  1723.         {
  1724.             ReturnNode(Key,m_Root); // assigning m_FindNode to where address node of the found key.
  1725.             m_FindNode ->YearsData.PushBack(DataObj); // insert the obj into the vector of the node.
  1726.         }
  1727.     }
  1728.     else
  1729.     {
  1730.         // root is null
  1731.         m_Root = new Node<T>;
  1732.         m_Root -> Key = Key;
  1733.         m_Root -> YearsData.PushBack(DataObj);
  1734.         m_Root ->Left = nullptr;
  1735.         m_Root ->Right = nullptr;
  1736.     }
  1737.  
  1738.  
  1739. }
  1740.  
  1741. template <class T>
  1742. bool Bst<T>:: Search(int Key) const
  1743. {
  1744.     if(m_Root!=nullptr) // if root not null
  1745.     {
  1746.         return Search(Key,m_Root); // call function to return true or false
  1747.     }
  1748.     else
  1749.     {
  1750.         return false; // else return false
  1751.     }
  1752. }
  1753.  
  1754.  
  1755. template <class T>
  1756. void Bst<T>::InOrderTraversal( InOrderTraversalPtr Print) const
  1757. {
  1758.     // print the inorder key to screen
  1759.     InOrderTraversal(m_Root, Print);
  1760. }
  1761.  
  1762. template <class T>
  1763. void Bst<T>::PostOrderTraversal(PostOrderTraversalPtr Print) const
  1764. {
  1765.     // print the postorder key to screen
  1766.     PostOrderTraversal(m_Root,Print);
  1767. }
  1768.  
  1769. template <class T>
  1770. void Bst<T>::PreOrderTraversal(PreOrderTraversalPtr Print) const
  1771. {
  1772.     // print the preorder key to screen
  1773.     PreOrderTraversal(m_Root,Print);
  1774. }
  1775.  
  1776. template <class T>
  1777. void Bst<T>:: Insert(int Key, Node<T> * CurrentNode,T DataObj)
  1778. {
  1779.     if(Key>CurrentNode->Key) // if entered key is bigger than root key
  1780.     {
  1781.         if(CurrentNode->Right !=nullptr) // check if node right pointer not null
  1782.         {
  1783.             Insert(Key,CurrentNode->Right,DataObj); // recursive with root become the right pointer
  1784.         }
  1785.         else
  1786.         {
  1787.             // if the right pointer is null create a new node and assign data and key
  1788.             Node <T>* New_Node = new Node<T>;
  1789.             New_Node -> Key = Key;
  1790.             New_Node ->YearsData.PushBack(DataObj);
  1791.             New_Node ->Left = nullptr;
  1792.             New_Node ->Right = nullptr;
  1793.             CurrentNode->Right = New_Node; // point new create node to currentNode right pointer
  1794.         }
  1795.     }
  1796.     else // if key is not bigger than root key
  1797.     {
  1798.         if(CurrentNode->Left !=nullptr)// check if node left pointer not null
  1799.         {
  1800.             Insert(Key,CurrentNode->Left,DataObj);  // recursive with root become the left pointer
  1801.         }
  1802.         else
  1803.         {
  1804.             // if the left pointer is null create a new node and assign data and key
  1805.             Node<T> * New_Node = new Node<T>;
  1806.             New_Node->Key = Key;
  1807.             New_Node ->YearsData.PushBack(DataObj);
  1808.             New_Node ->Left = nullptr;
  1809.             New_Node ->Right = nullptr;
  1810.             CurrentNode->Left = New_Node; // point new create node to currentNode right pointer
  1811.         }
  1812.     }
  1813. }
  1814.  
  1815.  
  1816. template <class T>
  1817. int& Bst<T>:: GetData(int& Data) const
  1818. {
  1819.     return Data; // return the parameter value
  1820. }
  1821.  
  1822. template <class T>
  1823. void Bst<T>::InOrderTraversal(Node<T> * CurrentNode,InOrderTraversalPtr Print) const
  1824. {
  1825.     // recursive call
  1826.     if (CurrentNode->Left !=nullptr) // if left pointer not null
  1827.     {
  1828.         InOrderTraversal(CurrentNode->Left,Print); // recursive till left pointer is null
  1829.     }
  1830.     Print(GetData(CurrentNode->Key)); // call the print ptr function with getdata function as parameter
  1831.  
  1832.     if(CurrentNode->Right !=nullptr) // if right pointer not null
  1833.     {
  1834.         InOrderTraversal(CurrentNode->Right, Print); // recursive till right pointer is null
  1835.     }
  1836. }
  1837.  
  1838. template <class T>
  1839. void Bst<T>::PostOrderTraversal(Node<T> *CurrentNode,PostOrderTraversalPtr Print) const
  1840. {
  1841.     if (CurrentNode->Left!=nullptr) // if left pointer not null
  1842.     {
  1843.         PostOrderTraversal(CurrentNode->Left,Print);// recursive till left pointer is null
  1844.     }
  1845.     if (CurrentNode->Right!=nullptr)// if right pointer not null
  1846.     {
  1847.         PostOrderTraversal(CurrentNode->Right,Print);// recursive till right pointer is null
  1848.     }
  1849.     Print(GetData(CurrentNode->Key)); // call the print ptr function with getdata function as parameter
  1850. }
  1851.  
  1852. template <class T>
  1853. void Bst<T>::PreOrderTraversal(Node<T> *CurrentNode,PreOrderTraversalPtr Print) const
  1854. {
  1855.  
  1856.     Print(GetData(CurrentNode->Key)); // call the print ptr function with getdata function as parameter
  1857.     if (CurrentNode->Left!=nullptr) // if left pointer not null
  1858.     {
  1859.         PreOrderTraversal(CurrentNode->Left,Print); // recursive till left pointer is null
  1860.     }
  1861.     if (CurrentNode->Right!=nullptr)// if right pointer not null
  1862.     {
  1863.         PreOrderTraversal(CurrentNode->Right,Print); // recursive till right pointer is null
  1864.     }
  1865. }
  1866.  
  1867. template <class T>
  1868. bool Bst<T>::Search(int Key, Node<T> *CurrentNode) const
  1869. {
  1870.     if(CurrentNode == nullptr) // Current Node = nullptr
  1871.     {
  1872.         return false; //return false
  1873.     }
  1874.     else if(Key == CurrentNode->Key) // if key matches node key
  1875.     {
  1876.         return true; // return true
  1877.     }
  1878.     else if(Key>CurrentNode->Key) // if key is bigger than root node key
  1879.     {
  1880.         return Search(Key,CurrentNode->Right); // the root node points right pointer
  1881.     }
  1882.     else
  1883.     {
  1884.         // if key not bigger than root node key
  1885.         return Search(Key,CurrentNode->Left); // root node points to left pointer
  1886.     }
  1887. }
  1888.  
  1889. template <class T>
  1890. void Bst<T>::AssignVector(Node<T> * CurrentNode, int Key)
  1891. {
  1892.     Vector<T> ReturnVec;
  1893.     if(Key == CurrentNode->Key) // if key matches node key
  1894.     {
  1895.         m_ReturnVec = CurrentNode ->YearsData; //Assign the vector to m_ReturnVec
  1896.     }
  1897.     else if(Key>CurrentNode->Key) // if key is bigger than root node key
  1898.     {
  1899.         AssignVector(CurrentNode->Right,Key); // the root node points right pointer
  1900.     }
  1901.     else
  1902.     {
  1903.         // if key not bigger than root node key
  1904.         AssignVector(CurrentNode->Left,Key);  // root node points to left pointer
  1905.     }
  1906. }
  1907.  
  1908. template <class T>
  1909. void Bst<T>::ReturnNode(int Key,Node<T>* CurrentNode)
  1910. {
  1911.     if(Key == CurrentNode->Key) // if key matches node key
  1912.     {
  1913.         m_FindNode = CurrentNode; //Assign the node pointer to m_FindNode
  1914.     }
  1915.     else if(Key>CurrentNode->Key) // if key is bigger than root node key
  1916.     {
  1917.         ReturnNode(Key,CurrentNode->Right); // the root node points right pointer
  1918.     }
  1919.     else
  1920.     {
  1921.         // if key not bigger than root node key
  1922.         ReturnNode(Key,CurrentNode->Left); // root node points to left pointer
  1923.     }
  1924. }
  1925.  
  1926.  
  1927. template <class T>
  1928. void Bst<T>::Delete(Node<T>*CurrentNode)
  1929. {
  1930.     if(CurrentNode->Left!=nullptr)
  1931.     {
  1932.         Delete(CurrentNode->Left); //recursive till node left pointer is null
  1933.     }
  1934.     if(CurrentNode->Right!=nullptr)
  1935.     {
  1936.         Delete(CurrentNode->Right); // recursive till node right pointer is null
  1937.     }
  1938.     delete CurrentNode; // sever connection to heap memory with delete keyword
  1939.     CurrentNode=nullptr; // set the node pointer to null.
  1940. }
captcha