Advertisement
Spocoman

Courier Express

Sep 17th, 2023 (edited)
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     double weight;
  9.     cin >> weight;
  10.  
  11.     string service;
  12.     cin >> service;
  13.  
  14.     int distance;
  15.     cin >> distance;
  16.  
  17.     double standardPrice = 0;
  18.  
  19.     if (weight < 1) {
  20.         standardPrice = 0.03;
  21.     }
  22.     else if (weight >= 1 && weight < 10) {
  23.         standardPrice = 0.05;
  24.     }
  25.     else if (weight >= 10 && weight < 40) {
  26.         standardPrice = 0.10;
  27.     }
  28.     else if (weight >= 40 && weight < 90) {
  29.         standardPrice = 0.15;
  30.     }
  31.     else if (weight >= 90 && weight < 150) {
  32.         standardPrice = 0.20;
  33.     }
  34.  
  35.     double expressPrice = 0;
  36.  
  37.     if (service == "express") {
  38.         if (weight < 1) {
  39.             expressPrice = standardPrice * 0.80;
  40.         }
  41.         else if (weight >= 1 && weight < 10) {
  42.             expressPrice = standardPrice * 0.40;
  43.         }
  44.         else if (weight >= 10 && weight < 40) {
  45.             expressPrice = standardPrice * 0.05;
  46.         }
  47.         else if (weight >= 40 && weight < 90) {
  48.             expressPrice = standardPrice * 0.02;
  49.         }
  50.         else if (weight >= 90 && weight < 150) {
  51.             expressPrice = standardPrice * 0.01;
  52.         }
  53.     }
  54.  
  55.     double totalPrice = standardPrice * distance + expressPrice * distance * weight;
  56.  
  57.     cout << "The delivery of your shipment with weight of "
  58.         << fixed << setprecision(3) << weight << " kg.would cost "
  59.         << setprecision(2) << totalPrice << " lv." << endl;
  60.  
  61.     return 0;
  62. }
  63.  
  64. Решение с тернарен оператор и printf():
  65.  
  66. #include <iostream>
  67. #include <string>
  68.  
  69. using namespace std;
  70.  
  71. int main() {
  72.     double weight;
  73.     cin >> weight;
  74.  
  75.     string service;
  76.     cin >> service;
  77.  
  78.     int distance;
  79.     cin >> distance;
  80.  
  81.     double standardPrice =
  82.         weight < 1 ? 0.03 :
  83.         weight < 10 ? 0.05 :
  84.         weight < 40 ? 0.10 :
  85.         weight < 90 ? 0.15 :
  86.         weight < 150 ? 0.20 : 0;
  87.  
  88.     double expressPrice = 0;
  89.  
  90.     if (service == "express") {
  91.         expressPrice = 1 *
  92.             (weight < 1 ? 0.80 :
  93.             weight < 10 ? 0.40 :
  94.             weight < 40 ? 0.05 :
  95.             weight < 90 ? 0.02 :
  96.             weight < 150 ? 0.01 : 0) * standardPrice * weight;
  97.     }
  98.    
  99.     double totalPrice = (standardPrice + expressPrice) * distance;
  100.  
  101.     printf("The delivery of your shipment with weight of %.3f kg. would cost %.2f lv.\n", weight, totalPrice);
  102.  
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement