1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <iostream>
#include <iomanip>
#include <algorithm>
// function prototypes
int getGirth( int, int, int );
int getCost( const int[], int, int );
bool read_package_data( int& weight, int& length, int& width, int& height );
bool valid( int weight, int length, int width, int height );
bool over_sized( int weight, int length, int width, int height );
int main()
{
const int size = 15;
const int weightArray[size] = {1, 2, 3, 5, 7, 10, 13, 16, 20, 25, 30, 35, 40, 45, 50};
const double costArray[size] = {1.50, 2.10, 4.00, 6.75, 9.90, 14.95, 19.40, 24.20, 27.30,
31.90, 38.50, 43.50, 44.80, 47.40, 55.20 };
int transaction = 0 ;
int weight = 0;
int length = 0;
int width = 0;
int height = 0;
int accepted = 0;
int rejected = 0;
std::cout << "For each transaction, enter package weight and 3 dimensions. Enter -1 to quit.\n" ;
while ( read_package_data( weight, length, width, height ) )
{
++transaction ;
if ( valid( weight, length, width, height ) )
{
const bool accept = !over_sized( weight, length, width, height ) ;
if(accept) ++accepted ;
else ++rejected ;
std::cout << "Status: " << std::setw(12) << ( accept ? "Accepted" : "Rejected" )
<< "\nWeight: " << std::setw(12) << weight << '\n' ;
if(accept)
{
const double cost = costArray[ getCost( weightArray, size, weight ) ] ;
std::cout << "Cost: " << std::fixed << std::setw(14) << std::setprecision(2) << cost << '\n' ;
}
}
else // ! valid( weight, length, width, height )
{
std::cout << "Error - package weight and dimensions must be larger than 0\n"
"Please re-enter transaction\n" ;
}
}
std::cout << "Number of rejected packages: " << rejected << '\n'
<< "Number of accepted packages: " << accepted << '\n' ;
}
bool read_package_data( int& weight, int& length, int& width, int& height )
{
std::cout << "\nEnter package weight and 3 dimensions (-1 to quit): " ;
return std::cin >> weight && weight != -1 && std::cin >> length >> height >> width ;
}
bool valid( int weight, int length, int width, int height )
{ return weight > 0 && length > 0 && width > 0 && height > 0 ; }
bool over_sized( int weight, int length, int width, int height )
{
const int girth = getGirth( length, width, height );
return ( weight > 50 ) || ( length > 36 ) || ( width > 36 ) || ( height > 36 ) || ( girth > 60 ) ;
}
int getGirth( int length, int width, int height )
{
// http://en.cppreference.com/w/cpp/algorithm/max
return 2 * ( length + width + height - std::max( { length, width, height } ) );
}
int getCost( const int w[], int s, int value )
{
for( int index = 0 ; index < s ; ++index ) if( w[index] >= value ) return index ;
return s-1 ; // max possible cost
}
|