/** * * Project 2 - Refugee camp water allocation * * @author Clay Shields * **/ #include #include #include #include #include using namespace std; int main(){ const double minWaterPerPersonPerDay = 7.5; const double maxWaterPerPersonPerDay = 15; const double tapLitersPerMinute = 7.5; const double pumpLitersPerMinute = 17; const double wellLitersPerMinute = 12.5; const double litersToGallons = 3.78541; const double waterBufffaloCapacityGallons = 400; const double waterBuffaloCapacityLiters = 400 * litersToGallons; const int minutesWaterPerDay = 60 * 8; string inputFilename; ifstream datafile; string campName; unsigned int numRefugees = 0 ; unsigned int totalNumRefugees = 0; double waterNeeds = 0.0; int tapCount = 0; int wellCount = 0; int pumpCount = 0; double waterNeeded = 0; double waterAvailable = 0; double waterSurplus = 0; double waterDeficit = 0; double totalWaterSurplus = 0; double totalWaterDeficit = 0; int buffalosNeeded = 0; int buffalosExtra = 0; int totalBuffalosNeeded = 0; int totalBuffalosExtra = 0; cout << "Multiple camp water distribution helper" << endl; cout << "This program will read a file containing information about " << endl << "multiple refugee camps and determine which camps have enough " << endl << "water and which do not. It will then total the number of water " << endl << "buffalos needed to transport water between camps" << endl << endl; // cout << "The input file has a format of: " << endl // << " " << endl // << "where:" << endl // << " is a single word with the name of the camp " << endl // << " (spaces replaced with underscores if needed" << endl // << " is the number of inhabitants of that camp" << endl // << " is the liters of water needed per person per" << endl // << " day at that camp (local conditions vary)" << endl // << " is the number of water taps available" << endl // << " is the number of wells available " << endl // << " is the number of pumps available" << endl << endl << endl; // Take a filename as input cout << "Please enter the name of the input file: "; getline (cin,inputFilename); // open file if it is there datafile.open(inputFilename.c_str()); while (!datafile) { cout << "Could not open file: "<< inputFilename << " for reading." << endl; cout << "Please re-enter filename: "; getline (cin,inputFilename); datafile.open(inputFilename.c_str()); } // cout format the output header cout << setw(10) << left << "Camp" << right << setw(10) << "Number" << setw(15) << "Water" << setw (15) << "Surplus" << setw(15) << "Water" << setw (15) << "Need" << endl; cout << setw(10) << left << "Name" << right << setw(10) << "Refugees" << setw(15) << "Surplus" << setw (15) << "Buffalos" << setw(15) << "Deficit" << setw (15) << "Buffalos" << endl; cout << setw(10) << left << "----" << right << setw(10) << "--------" << setw(15) << "-------" << setw (15) << "--------" << setw(15) << "-------" << setw (15) << "--------" << endl; // read each line in the file while (datafile >> campName >> numRefugees >> waterNeeds >> tapCount >> wellCount >> pumpCount){ // do the error checking if (numRefugees < 0) { cout << "Negative number of refguees is not possible" << endl; return 0; } if (numRefugees > 100000) { cout << "No support for camps of over 100,000" << endl; return 0; } if ((waterNeeds < minWaterPerPersonPerDay) || (waterNeeds > maxWaterPerPersonPerDay)){ cout << "Water needs outside normal acceptable range" << endl; return 0; } if (tapCount < 0){ cout << "Negative number of taps is not possible" << endl; return 0; } if (wellCount < 0){ cout << "Negative number of wells is not possible" << endl; return 0; } if (pumpCount < 0){ cout << "Negative number of pumps is not possible" << endl; return 0; } // Add up refugees totalNumRefugees += numRefugees; // Compute how much water is available and how much is needed waterNeeded = waterNeeds * numRefugees; // Calculate the water available waterAvailable = (((tapCount * tapLitersPerMinute) + (pumpCount * pumpLitersPerMinute) + (wellCount * wellLitersPerMinute)) * minutesWaterPerDay); // cerr << "tap: " << tapCount << " pump: " << pumpCount << " well: " << wellCount // << " need: " << waterNeeded << " av: " << waterAvailable << endl; if (waterAvailable == waterNeeded){ buffalosNeeded = 0; buffalosExtra = 0; waterSurplus = 0; waterDeficit = 0; } else { if (waterAvailable > waterNeeded){ waterSurplus = waterAvailable - waterNeeded; totalWaterSurplus += waterSurplus; waterDeficit = 0; buffalosNeeded = 0; buffalosExtra = floor(waterSurplus/waterBuffaloCapacityLiters); totalBuffalosExtra += buffalosExtra; } else { waterSurplus = 0; waterDeficit = waterNeeded - waterAvailable; totalWaterDeficit += waterDeficit; buffalosNeeded = ceil(waterDeficit/waterBuffaloCapacityLiters); totalBuffalosNeeded += buffalosNeeded; buffalosExtra = 0; } } cout << left << setw(10) << campName << right << setw(10) << numRefugees << setw(15) << waterSurplus << setw(15) << buffalosExtra << setw(15) << waterDeficit << setw(15) << buffalosNeeded << endl; } cout << left << setw(10) << "----" << right << setw(10) << "--------" << setw(15) << "-------" << setw (15) << "--------" << setw(15) << "-------" << setw (15) << "--------" << endl; cout << left << setw(10) << "Totals:" << right << setw(10) << totalNumRefugees << setw(15) << totalWaterSurplus << setw(15) << totalBuffalosExtra << setw(15) << totalWaterDeficit << setw(15) << totalBuffalosNeeded << endl; }