#include #include #include #include using namespace std; // MAX number of experiments const int MAX_EXPERIMENTS = 10; // Contains the Temperature data at a given time // for an experiment. struct TempData { double temp; time_t time; }; // Contains a list of TempData for an experiment. class TempList { public: TempList() : s_size(0) {}; // Default Constructor void add_temp(double temp); // add_temp adds a temp for time now void add_temp(double temp, time_t time); // add_temp adds a temp and a time. int size() const { return s_size; }; // Returns number of elements in data_list. bool isFull() const { return (s_size >= MAX_DATA); }; // Tests if list is full, return true else false friend ostream& operator<<(ostream& out, const TempList& list); // Prints the data_list TempData& at(int index); // Returns the TempData at the given index. // Assures that the index >= 0 && index < size() // Overload index[] opertor uses at(int index) above TempData& operator[](int index) { return at(index); }; static const int MAX_DATA = 10; // Class constant private: TempData data_list[MAX_DATA]; // TempData Array in class int s_size; // Number of elements }; // overload operator<< for struct TempData // NOTE: Doesn't need to be a friend since it is public by default. ostream& operator<<(ostream& out, const TempData& tempDat) { out << "temp: " << tempDat.temp << " time: " << tempDat.time; return out; } // overload operator<< for class TempList // NOTE: Uses index[] and size() to iterator over data_list. // Depends on overloaded operator above. ostream& operator<<(ostream& out, TempList& tempList) { for (int i = 0; i < tempList.size(); i++) out << tempList[i] << endl; return out; } // Main program // Reads in number of experiments and number of data items per experiment. // Collects data for the experiments. int main() { TempList experiments[MAX_EXPERIMENTS]; int numExper = 0; int numData = 0; // Get the number of experiments and data elements for each. do { cout << "Enter the number of experiments: " << endl; cin >> numExper; } while (numExper < 0 || numExper >= MAX_EXPERIMENTS); do { cout << "Enter the number of data points to collect: " << endl; cin >> numData; } while (numData < 0 || numData >= TempList::MAX_DATA); // Collect data for the Experiments for (int i = 0; i < numExper; i++) { cout << "Enter data for experiment # " << (i+1) << endl; for (int j = 0; j < numData; j++) { double temp; cout << "You will be prompted to take temp after after 5 seconds\n"; sleep(5); cout << "Enter temp data now: "; cin >> temp; experiments[i].add_temp(temp); } } // Print the data for the experiments for (int i = 0; i < numExper; i++) { cout << "Data for Experiment # " << (i+1) << endl; cout << experiments[i] << endl; } } // Adds a temp to the end of data_list if it is not full. // Assumes time is now and calls the other version. void TempList::add_temp(double temp) { add_temp(temp, time(NULL)); } // Adds a temp to the end of data_list if it is not full. void TempList::add_temp(double temp, long time) { if (!isFull()) { TempData tempData = { temp, time }; data_list[s_size++] = tempData; } } // Returns the object at the given index as long as it is in bounds. // index >= 0 && index < size() TempData& TempList::at(int index) { assert(index >= 0 && index < s_size); return data_list[index]; }