1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef vector<string> stringVector; //Defines a vector of string values
const int MAX_SIZE = 31;
void OpenInputFile(ifstream&, string);
void OpenOutputFile(ofstream&, string);
int ReadArray(ifstream&, int[]);
float CalcAverage(int[], int array_size);
float CalcMax(int array[], int array_size);
int getDir(string dir, stringVector &files);
int main()
{
ifstream indiv_file;
ofstream ofs_max;
ofstream ofs_average;
int array[MAX_SIZE], next, count = 0;
float average_temp, maximum_temp;
int array_size;
string dir, out_file_max = "MaxsOfArrays.txt", out_file_average = "AvgsOfArrays.txt";
cout << "Enter directory with files available for use: ";
cin >> dir;
stringVector files = stringVector();
OpenOutputFile (ofs_max, out_file_max);
OpenOutputFile (ofs_average, out_file_average);
getDir (dir, files);
int fsize = files.size();
for (unsigned int i = 0; i < fsize; i++)
{
count = 0;
if (files[i].at(0)!= '.')
{
//cout << endl << dir + files[i];
OpenInputFile (indiv_file, (dir + files[i]));
if (indiv_file.fail())
{
cout << "Fail";
return 0;
}
array_size = ReadArray(indiv_file, array);
average_temp = CalcAverage(array, array_size);
maximum_temp = CalcMax(array, array_size);
/* for (int j = 0; j < array_size; j++)
{
cout << array[j] << " ";
} */
cout << endl << "The average temperature in " << files[i] << " was: " << average_temp << endl;
cout << "The maximum temperature in " << files[i] << " was: " << maximum_temp << endl;
out_file_max << "The maximum temperature in " << files[i] << " was: " << maximum_temp << endl;
}
indiv_file.close();
}
cout << endl;
return 0;
}
//----------------------------------------
int getDir (string dir, stringVector &files)
{
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL) {
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL) {
files.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
}
//---------------------------------------
void OpenInputFile(ifstream &in_file, string file_name)
{
in_file.open(file_name.c_str());
if (in_file.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}
}
//--------------------------------------
void OpenOutputFile(ofstream &out_file, string file_name)
{
out_file.open("MaxsOfArrays.txt");
out_file.open("AvgsOfArrays.txt");
}
//--------------------------------------
int ReadArray(ifstream &in_file, int number[])
{
int next(0);
while (in_file >> number[next])
{
next++;
if (next == MAX_SIZE)
break;
}
return next;
}
//-------------------------------------
float CalcAverage(int array[], int array_size) /
{
int total = 0, i = 0;
float average;
for (i; i < array_size; i++)
total += array[i];
average = ((static_cast <float> (total)) / array_size);
}
//------------------------------------
float CalcMax(int array[], int array_size)
{
float max;
int temp;
for (int i = array_size - 1; i >= 0; i--)
{
for (int j = 0; j < array_size - 1; j++)
{
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
max = array[array_size - 1];
return max;
}
|