Patrick B. answered 09/14/20
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <conio.h>
#define MONTH_STR_LENGTH (25)
inline bool compareIntCallback( int i, int j) { return(i<j); }
typedef
class Temperature
{
private:
vector<int> temps;
char month[25];
int year;
int iIndexException;
public:
Temperature ( char * mon, int yr)
{
strncpy(month,mon,MONTH_STR_LENGTH); year=yr;
}
inline void GetMonth( char * mon) { strncpy(mon,month,MONTH_STR_LENGTH); }
inline int GetYear() { return(year); }
inline void SetMonth(char * mon) { strncpy(month,mon,MONTH_STR_LENGTH) ; }
inline void SetYear( int yr) { year = yr; }
inline void AddTemperature( int t) { temps.push_back(t); }
inline void Sort() { sort(temps.begin(), temps.end(),compareIntCallback); }
inline int IndexException() { return(iIndexException); } //this determines if what the Indexer returns is valid
inline int GetNumTemps() { return(temps.size()); }
// returns the element at the specified position; otherwise sets the IndexException flag if necessary
int IndexerGetIndexAt( int iIndexPos)
{
int iReturn=0;
if(
!(
(iIndexPos<0) || (iIndexPos>=temps.size())
)
)
{
iReturn = temps[iIndexPos];
iIndexException=0;
}
else
{
iIndexException=-1;
}
return(iReturn);
} //Indexer
double AverageTemp() //returns the average temp
{
int N = this->GetNumTemps();
double avgTempReturn = 0;
double sumTemps =0;
//adds them up
for (int iLoop=0; iLoop<N; iLoop++)
{
int iTemp = this->IndexerGetIndexAt(iLoop) ;
sumTemps += iTemp;
}
if (N>0) // average calculated here
{
avgTempReturn = (double) ((sumTemps*1.0f)/N);
}
return(avgTempReturn);
}
int MaxTemp() //linear searches for the max temp
{
int N = this->GetNumTemps();
int maxTemp = -32767;
for (int iLoop=0; iLoop<N; iLoop++)
{
int iTemp = this->IndexerGetIndexAt(iLoop) ;
if (iTemp>maxTemp) { maxTemp = iTemp; }
}
return(maxTemp);
}
int MinTemp() //linear searches for the min temp
{
int N = this->GetNumTemps();
int minTemp = 32767;
for (int iLoop=0; iLoop<N; iLoop++)
{
int iTemp = this->IndexerGetIndexAt(iLoop) ;
if (iTemp<minTemp) { minTemp = iTemp; }
}
return(minTemp);
}
void Output(char * msgStr=NULL)
{
cout << "********************************************************" << endl;
if (msgStr != NULL)
{
cout << msgStr << endl;
cout << "********************************************************" << endl;
}
this->Sort();
int N = this->GetNumTemps();
cout << " Month is " << this->month << endl;
cout << " Year is " << this->year << endl;
cout << N << " temperatures " << endl;
cout << "-------------------------------------" << endl;
for (int iLoop=0; iLoop<N; iLoop++)
{
cout << this->IndexerGetIndexAt(iLoop) << endl;
}
cout << " average temp is " << this->AverageTemp() << endl;
cout << " max temp is " << this->MaxTemp() << endl;
cout << " min temp is " << this->MinTemp() << endl;
}
} * TTemperature;
#define TEMPERATURE_SIZE ( sizeof(Temperature))
// reads the temperatures from the file and returns pointer temperature object that contains them
// returns NULL pointer if read fails or file corrupted with non-numerics
TTemperature FileInput()
{
char filename[4096];
TTemperature tempPtrReturn=NULL;
cout << " Filename :>";
cin >> filename;
//cout << filename << endl;
FILE * fptr = fopen(filename,"r");
if (fptr != NULL)
{
char month[MONTH_STR_LENGTH];
char inbuff[4096];
fgets(month,MONTH_STR_LENGTH,fptr);
fgets(inbuff,4096,fptr);
//cout << month << endl;
//cout << inbuff << endl;
tempPtrReturn = new Temperature(month,atoi(inbuff));
int iTemp;
bool done_flag = false;
while (!done_flag)
{
if (fscanf(fptr,"%d",&iTemp)==1)
{
tempPtrReturn->AddTemperature(iTemp);
}
else // error on input format : type mismatch
{
delete(tempPtrReturn);
tempPtrReturn=NULL;
break;
}
done_flag = feof(fptr);
}
fclose(fptr);
}
return(tempPtrReturn);
}
int main()
{
TTemperature temp = FileInput();
if (temp != NULL)
{
temp->Output();
}
else
{
cout << " INPUT FILE NOT FOUND OR CORRUPTED ....." << endl;
cout << " press key...." << endl;
while (!getch()) { }
}
}
/* ALL OF THIS WORKS FINE!!!!
// ----------------------------------------------
int main()
{
Temperature temp((char*)"July",2020);
temp.AddTemperature(95);
temp.AddTemperature(88);
temp.AddTemperature(90);
temp.AddTemperature(101);
temp.AddTemperature(92);
temp.AddTemperature(87);
temp.Sort();
int N = temp.GetNumTemps();
cout << N << endl;
for (int iLoop=0; iLoop<N; iLoop++)
{
cout << temp.IndexerGetIndexAt(iLoop) << endl;
}
cout << " average temp is " << temp.AverageTemp() << endl;
cout << " max temp is " << temp.MaxTemp() << endl;
cout << " min temp is " << temp.MinTemp() << endl;
}
//---------------------------------------------------
*******************************************************/