/* UPStest.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 // for getch() using namespace std; // Prototypes - see the function defintions for details 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); /***************************************************************************** * THIS IS FOR TESTING PUPROSES ONLY *****************************************************************************/ int main() { cout << "TESTING UPS FUNCTIONS\n\n"; cout << calcGirth(10,20) << endl; cout << classify(10,0,20,5) << endl; cout << classify(20,30,10,15) << endl; cout << classify(20,30,10,55) << endl; cout << classify(50,20,18,69) << endl; cout << classify(95,10,10,60) << endl; cout << classify(10,10,10,10) << endl; 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; }