/* UPS.cpp * CS401 - Intro to Comp Sci, summer 2001 * University of Pittsburgh * * The functions below represent a C++ solution to the UPS rules regarding * package classification. This implementation provides a top level function * called classify() that returns the classification number (details below). * See http://www.ups.com/using/services/packaging/oversize-guide.html * for more information. * * Throughout the code, distances are measured in inches, and weights are * measured in pounds. * * Note: I've used ints here, but doubles would be superior if you needed * better precision. The thought is that UPS is probably cool with an * approximation as long as you're within an inch or a pound. * * Note2: The main() function is just ONE way that these functions could be * used. It could be that they are called by other software packages that * need the classifications, or perhaps from an automated phone system that * UPS uses for callers that need packages classified at home. */ #include #include #include #include // for getch() using namespace std; // Prototypes - see the function defintions for details void displayGreeting(); void displayResults(int, int, int, int); int classify(int, int, int, int); // input is length, longwidth, shwidth, wt int calcGirth(int, int); // input is longwidth and shortwidth bool oversize1(int, int, int); // input is length, girth, and wt bool oversize2(int, int, int); int main() { // variables int length, shortWidth, longWidth, weight; int packageCount, regCount=0, os1Count=0, os2Count=0, invalidCount=0; // input displayGreeting(); cout << "How many packages do you have today? "; cin >> packageCount; cout << endl; for (int i=0; i> length; cout << " Enter the width of the longest side (in inches): "; cin >> shortWidth; cout << " Enter the width of the shorter side (in inches): "; cin >> longWidth; cout << " Enter the weight (in pounds): "; cin >> weight; cout << endl; int classification = classify(length, longWidth, shortWidth, weight); switch (classification) { case 0: { cout << "That is a regular package.\n\n"; regCount++; } break; case 1: { cout << "That is an oversize1 package.\n\n"; os1Count++; } break; case 2: { cout << "That is an oversize2 package.\n\n"; os2Count++; } break; case -1: { cout << "That is an invalid package.\n\n"; invalidCount++; } break; default: cout << "something went wrong.\n\n"; } // end switch } // end for displayResults(regCount, os1Count, os2Count, invalidCount); cout << "That's it! Hit the space bar to exit the program."; getch(); } // PACKAGE CLASSIFICATION FUNCTIONS // calcGirth: function to calculate the girth of a package // input: the long width and the short width of a box // returns: the distance around the box int calcGirth(int lw, int sw) { return (2*(lw+sw)); } // oversize1() and oversize2(): functions to determine if OS1 or OS2 apply // input: the length (bottom to top), girth, and weight of a box // returns: bool oversize1(int len, int g, int wt) { return ( (len + g > 84) && (len + g <= 108) && (wt < 30) ); } bool oversize2(int len, int g, int wt) { return ( (len + g > 108) && (wt < 70) ); } // classify(): top-level function // input: length, long width, short width, weight // returns: 0 if regular, 1 if OS1, 2 if OS2, -1 if there was a problem int classify(int len, int lwidth, int swidth, int wt) { // first weed out impossible packages if ((len <= 0) || (lwidth <= 0) || (swidth <= 0) || (wt <= 0)) return -1; // calculate the girth int girth = calcGirth(lwidth, swidth); // see if the package is unshippable (too large in some way) if ((len > 108) || (wt >= 70) || (len + girth > 130)) return -1; // now handle the two easy classifications (we have functions for them) if (oversize1(len, girth, wt)) return 1; if (oversize2(len, girth, wt)) return 2; // if we made it here, we've got a regular package return 0; } // OTHER (HELPER) FUNCTIONS // greeting function void displayGreeting() { cout << "This program will classify your UPS packages " << "(assuming they are in boxes).\n\n" << "Please measure the following things:\n" << " 1. the length in inches (or height, from top to bottom).\n" << " 2. the width of the longest side in inches\n" << " 3. the width of the shortest side in inches\n" << " 4. the weight of your package in pounds\n\n"; cout << "hit the space bar when you're ready to start.\n\n"; getch(); } // displays the final results void displayResults(int rct, int os1ct, int os2ct, int ict) { cout << "Package totals:\n"; cout << " regular packages\t" << setw(8) << rct << endl << " oversize1 packages\t" << setw(8) << os1ct << endl << " oversize2 packages\t" << setw(8) << os2ct << endl << " invalid packages\t" << setw(8) << ict << endl << endl; } // end UPS.cpp