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
|
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int NUM_MON = 12;
struct utilityInfo
{
char utility[20];
double monthlyExpenses[NUM_MON];
};
void printUtilityStat( utilityInfo teamAr[], int numUtilities );
void calcStats( double ar[], int arSize, double &sumVal, double &avgVal, double &highVal, double &lowVal );
int main()
{
ifstream inFile2;
utilityInfo UtilitiesArray[20];
int numUtilities;
inFile2.open( "binary_expenses", ios::binary );
if( inFile2.fail() )
{
cout << "input file 2 did not open";
exit(-1);
}
numUtilities = 0;
inFile2.read( (char *) &UtilitiesArray[numUtilities], sizeof(utilityInfo));
while( inFile2 )
{
numUtilities++;
inFile2.read( (char *) &UtilitiesArray[numUtilities], sizeof(utilityInfo));
}
inFile2.close();
for( int sub = 0; sub < numUtilities; sub++ )
{
cout << UtilitiesArray[sub].utility << "\t" << UtilitiesArray[sub].monthlyExpenses[NUM_MON] << endl;
}
inFile2.close();
cout << endl;
system("pause");
return 0;
}
void calcStats( double ar[], int arSize, double &sumVal, double &avgVal, double &highVal, double &lowVal )
{
int i,j;
for ( i = 0; i < 12; i++ )
avgVal = sumVal / arSize;
for ( i = 0; i < arSize;i++)
{
for(j = 0; j < 12; j++)
sumVal += ar[i].utility[j];
}
if (ar[i] > highVal)
highVal = ar[i];
for ( i = 0; i < arSize;i++)
{
if (ar[i] < lowVal)
lowVal = ar[i];
}
}
void printUtilityStat( utilityInfo teamAr[], int numUtilities )
{
double sumval, avgval, highval, lowval;
int sub;
int i;
for(int j = 0; j < 5; j ++)
{
calcStats( teamAr[i].monthlyExpenses, numUtilities, sumval, avgval, highval, lowval );
for ( sub = 0; sub < numUtilities; sub++)
{
cout << setprecision(2) << fixed << "sum over 12 month : $" << sumval;
cout << " Average : $" << avgval << endl;
cout << "highest cost : $" << highval ;
cout << " lowest cost : $" << lowval << endl << endl;
}
}
}
|