fork download
  1. //Natalie Zarate CIS 5 Chapter 4, P. 222, #13
  2.  
  3. /*******************************************************************************
  4.  *
  5.  * COMPUTE SHIPPING CHARGES
  6.  * _____________________________________________________________________________
  7.  * This program accepts and as input the weight of the user's package and the
  8.  * distance the package is being shipped, validates it, then calculates and
  9.  * displays the shipping charges.
  10.  *
  11.  * Computation will be based on the following input restrictions:
  12.  * - The package cannot weight less than 0 kg or more than 20 kg
  13.  * - The package cannot be shipped a distance less than 10 mi or more than 3,000
  14.  * mi
  15.  * _____________________________________________________________________________
  16.  * INPUT
  17.  * packageW : Weight of the package
  18.  * shipDist : Shipping distance
  19.  * distMult : Times greater than 500
  20.  * rate_1 : Rate for packages that weight 0-2 kg
  21.  * rate_2 : Rate for packages that weight 2-6 kg
  22.  * rate_3 : Rate for packages that weight 6-10 kg
  23.  * rate_4 : Rate for packages that weight 10-20 kg
  24.  * rate_1Max : Max weight for rate_1
  25.  * rate_2Max : Max weight for rate_2
  26.  * rate_3Max : Max weight for rate_3
  27.  * rate_4Max : Max weight for rate_4
  28.  * lowerW : Min package weight
  29.  * upperW : Max package weight
  30.  * distRate : Miles rates are based on, (rate per 500 mi)
  31.  * lowerD : Min shipping distance
  32.  * upperD : Max shipping distance
  33.  *
  34.  * OUTPUT
  35.  * shipCharge : Cost of shipping
  36.  *
  37.  ******************************************************************************/
  38. #include <iostream>
  39. #include <iomanip>
  40. using namespace std;
  41.  
  42. int main ()
  43. {
  44. /**********************************************************************
  45.   * CONSTANTS
  46.   * --------------------------------------------------------------------
  47.   * rate_1 : Rate for packages that weight 0-2 kg
  48.   * rate_2 : Rate for packages that weight 2-6 kg
  49.   * rate_3 : Rate for packages that weight 6-10 kg
  50.   * rate_4 : Rate for packages that weight 10-20 kg
  51.   * rate_1Max : Max weight for rate_1
  52.   * rate_2Max : Max weight for rate_2
  53.   * rate_3Max : Max weight for rate_3
  54.   * rate_4Max : Max weight for rate_4
  55.   * lowerW : Min package weight
  56.   * upperW : Max package weight
  57.   * distRate : Miles rates are based on, (rate per 500 mi)
  58.   * lowerD : Min shipping distance
  59.   * upperD : Max shipping distance
  60.   * *******************************************************************/
  61. const float rate_1 = 1.10;
  62. const float rate_2 = 2.20;
  63. const float rate_3 = 3.70;
  64. const float rate_4 = 4.80;
  65. const float rate_1Max = 2;
  66. const float rate_2Max = 6;
  67. const float rate_3Max = 10;
  68. const float rate_4Max = 20;
  69. const float lowerW = 0;
  70. const float upperW = 20;
  71. const int distRate = 500;
  72. const float lowerD = 10;
  73. const float upperD = 3000;
  74.  
  75. float packageW;
  76. float shipDist;
  77. float shipCharge;
  78. int distMult;
  79. // Prompt user for package weight
  80. cout << "Enter package weight in kilograms: " << endl;
  81. cin >> packageW;
  82.  
  83. // Validate input
  84. if (packageW <= lowerW || packageW > upperW)
  85. {
  86. cout << "Package weight must be greater than 0 kg and less than 20 kg.";
  87. cout << endl;
  88. cout << "Enter package weight in kilograms: " << endl;
  89. cin >> packageW;
  90. }
  91.  
  92. // Prompt user for shipping distance
  93. cout << "Enter shipping distance in miles:" << endl;
  94. cin >> shipDist;
  95.  
  96. // Validate Inout
  97. if (shipDist < lowerD || shipDist > upperD)
  98. {
  99. cout << "Shipping distance must be between 10 and 3000 miles." << endl;
  100. cout << "Enter shipping distance in miles: " << endl;
  101. cin >> shipDist;
  102. }
  103.  
  104. // Compute shipping charges
  105. if ((shipDist / distRate) < 1)
  106. {
  107. if (0 < packageW && packageW <= rate_1Max)
  108. shipCharge = rate_1;
  109. else if (packageW > rate_1Max && packageW <= rate_2Max)
  110. shipCharge = rate_2;
  111. else if (packageW > rate_2Max && packageW <= rate_3Max)
  112. shipCharge = rate_3;
  113. else
  114. shipCharge = rate_4;
  115. }
  116. if ((shipDist / distRate) >= 1 && (shipDist / distRate) < 2)
  117. {
  118. if (0 < packageW && packageW <= rate_1Max)
  119. shipCharge = rate_1 * 2;
  120. else if (packageW > rate_1Max && packageW <= rate_2Max)
  121. shipCharge = rate_2 * 2;
  122. else if (packageW > rate_2Max && packageW <= rate_3Max)
  123. shipCharge = rate_3 * 2;
  124. else
  125. shipCharge = rate_4 * 2;
  126. }
  127. if ((shipDist / distRate) >= 2 && (shipDist / distRate) < 3)
  128. {
  129. if (0 < packageW && packageW <= rate_1Max)
  130. shipCharge = rate_1 * 3;
  131. else if (packageW > rate_1Max && packageW <= rate_2Max)
  132. shipCharge = rate_2 * 3;
  133. else if (packageW > rate_2Max && packageW <= rate_3Max)
  134. shipCharge = rate_3 * 3;
  135. else
  136. shipCharge = rate_4 * 3;
  137. }
  138. if ((shipDist / distRate) >= 3 && (shipDist / distRate) < 4)
  139. {
  140. if (0 < packageW && packageW <= rate_1Max)
  141. shipCharge = rate_1 * 4;
  142. else if (packageW > rate_1Max && packageW <= rate_2Max)
  143. shipCharge = rate_2 * 4;
  144. else if (packageW > rate_2Max && packageW <= rate_3Max)
  145. shipCharge = rate_3 * 4;
  146. else
  147. shipCharge = rate_4 * 4;
  148. }
  149.  
  150. if ((shipDist / distRate) >= 4 && (shipDist / distRate) < 5)
  151. {
  152. if (0 < packageW && packageW <= rate_1Max)
  153. shipCharge = rate_1 * 5;
  154. else if (packageW > rate_1Max && packageW <= rate_2Max)
  155. shipCharge = rate_2 * 5;
  156. else if (packageW > rate_3Max && packageW <= rate_3Max)
  157. shipCharge = rate_3 * 5;
  158. else
  159. shipCharge = rate_4 * 5;
  160. }
  161. if ((shipDist / distRate) >= 5 && (shipDist / distRate) < 6)
  162. {
  163. if (0 < packageW && packageW <= rate_1Max)
  164. shipCharge = rate_1 * 6;
  165. else if (packageW > rate_1Max && packageW <= rate_2Max)
  166. shipCharge = rate_2 * 6;
  167. else if (packageW > rate_2Max && packageW <= rate_3Max)
  168. shipCharge = rate_3 * 6;
  169. else
  170. shipCharge = rate_4 * 6;
  171. }
  172.  
  173. // Output shipping charges
  174.  
  175. cout << "Shipping: $";
  176. cout << setprecision(3) << showpoint << shipCharge << endl;
  177.  
  178. return 0;
  179. }
Success #stdin #stdout 0s 5288KB
stdin
0
10
10

stdout
Enter package weight in kilograms: 
Package weight must be greater than 0 kg and less than 20 kg.
Enter package weight in kilograms: 
Enter shipping distance in miles:
Shipping: $3.70