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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;
bool openAndVerify(ifstream &, string &);
double averageCalc(ifstream &, string &);
double stndrdDevCalc(ifstream &, string &, double);
void print(double, double, string);
string getBankName(ifstream &, string &);
int main() {
int counter, numDesired;
double mean, stndrdDev;
string bankName, fileName;
ifstream inputFile;
double min = 0;
string betterBank;
cout << "Greetings!\nEnter number of banks would you like to analyze: ";
cin >> numDesired;
counter = 0;
while(counter < numDesired) {
if(openAndVerify(inputFile,fileName) == false) {
cout << "Error: bad file name. Program terminated!" << endl;
return 0;
} else {
bankName = getBankName(inputFile, fileName);
cout << bankName;
mean = averageCalc(inputFile, fileName);
stndrdDev = stndrdDevCalc(inputFile, fileName, mean);
if (stndrdDev < min){
betterBank = bankName;
}
print(stndrdDev, mean, bankName);
counter++;
}
}
cout << betterBank << " is the preferred bank because it has the lowest standard deviation." << endl;
return 0;
}
// PART A
bool openAndVerify(ifstream &inputFile, string &fileName) {
cout << "Please enter file name: ";
cin >> fileName;
inputFile.open(fileName.c_str());
if(inputFile.fail()) {
inputFile.close();
return false;
} else {
inputFile.close();
return true;
}
}
// PART B
double averageCalc(ifstream &inputFile, string &fileName) {
int numEntries = 0;
double time, sum;
sum = 0;
inputFile.open(fileName.c_str());
inputFile.ignore(500, '\n');
//getline(inputFile, fileName)
while(!inputFile.eof()) {
inputFile >> time;
sum += time;
numEntries++;
}
inputFile.close();
return sum / numEntries;
}
// PART C
double stndrdDevCalc(ifstream &inputFile, string &fileName, double mean) {
int numEntries = 0;
double time, stndrdDevCalcNumerator = 0;
inputFile.open(fileName.c_str());
inputFile.ignore(500, '\n');
while(!inputFile.eof()) {
inputFile >> time;
stndrdDevCalcNumerator += pow((time - mean), 2);
numEntries++;
}
inputFile.close();
return sqrt(stndrdDevCalcNumerator / (numEntries - 1));
}
// Part D
void print(double stndrdDev, double mean, string bankName) {
cout << "===============================================" << endl;
cout << "Bank Name: " << bankName << endl;
cout << setprecision(1) << showpoint << fixed;
cout << "Average wait time: " << mean << endl;
cout << "Standard Deviation of the wait time list: " << stndrdDev << endl;
cout << "===============================================" << endl << endl;
}
string getBankName(ifstream &inputFile, string &fileName) {
string bankName;
inputFile.open(fileName.c_str());
inputFile >> bankName;
return bankName;
}
|