//--------------------------------------------------------- // The purpose of this program is to test the list class // implementation for Lab 05. This program reads a file // containing double values and displays the number of // values, the minimum, the maximum and the average. // // D. Searls // Asbury College // Jan 2008 //--------------------------------------------------------- #include #include #include #include "Lab05_list.cpp" using namespace std; //--------------------------------------------------------- // readData // // Read a list of doubles from a file. // // Out Parameter: myList //--------------------------------------------------------- void readData(list& myList) { string filename; ifstream infile; double x; cout << "Enter the name of the data file: "; cin >> filename; cout << endl; infile.open(filename.c_str()); if (infile.fail()) { cout << "Could not open the input file!" << endl; exit(0); } infile >> x; while (!infile.fail()) { myList.push_front(x); infile >> x; } } //--------------------------------------------------------- // minimum // // Returns the minimum value in the list. // // In Parameter: myList //--------------------------------------------------------- double minimum(list& myList) { double* minLowPos = myList.first(); double min; double* itr; for(itr = myList.first(); itr != myList.end(); itr = myList.next()) { if (*itr < *minLowPos) { minLowPos = itr; } } min = *minLowPos; return min; } //--------------------------------------------------------- // maximum // // Returns the maximum value in the list. // // In Parameter: myList //--------------------------------------------------------- double maximum(list& myList) { double max = *(myList.first()); double* itr; for(itr = myList.first(); itr != myList.end(); itr = myList.next()) { if (*itr > max) { max = *itr; } } return max; } //--------------------------------------------------------- // mean // // Return a record containing the mean of the low // temperatures and the mean of the high temperatures. // // In Parameter: myList //--------------------------------------------------------- double mean(list& myList) { double sum; double* itr; sum = 0.0; for(itr = myList.first(); itr != myList.end(); itr = myList.next()) { sum = sum + *itr; } return sum/static_cast(myList.size()); } //--------------------------------------------------------- // displayResults // // Display the number of values, the mean, the minimum, and // the maximum. // // In Parameters: n, mean, min, max //--------------------------------------------------------- void displayResults(int n, double mean, double min, double max) { cout << fixed << setprecision(1); cout << " n:" << setw(3) << n << endl; cout << "Mean:" << setw(5) << mean << endl; cout << " Min:" << setw(5) << min << endl; cout << " Max:" << setw(5) << max << endl; } //--------------------------------------------------------- // displayList // // Display contents of list on one line. // // In parameter: myList //--------------------------------------------------------- void displayList(list& myList) { double* itr; cout << fixed << setprecision(0); for(itr = myList.first(); itr != myList.end(); itr = myList.next()) { cout << " " << *itr; } cout << endl << endl; } //********************************************************* // M A I N D R I V E R //********************************************************* int main() { list myList; // List of temperature records readData(myList); displayList(myList); displayResults(myList.size(), mean(myList), minimum(myList), maximum(myList)); cout << endl; // Create a copy of the list and then clear the orginal. list copy(myList); myList.clear(); cout << "Size of myList: " << myList.size() << " (0 EXPECTED)\n"; cout << "First element in myList: "; if (myList.first() != myList.end()) { cout << *(myList.first()); } cout << "(blank EXPECTED)" << endl << endl; // Display the results for the copy. displayList(copy); displayResults(copy.size(), mean(copy), minimum(copy), maximum(copy)); cout << endl; // Remove first two elements and display the results. copy.pop_front(); copy.pop_front(); displayList(copy); // Clear list and try to remove first element copy.clear(); copy.pop_front(); cout << "Size of copy: " << copy.size() << " (0 EXPECTED)\n"; cout << "First element in copy: "; if (copy.first() != copy.end()) { cout << *(copy.first()); } cout << "(blank EXPECTED)" << endl; return 0; }