Advertisement
Spocoman

Fitness Card

Sep 6th, 2024 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     double cash, sum;
  8.     cin >> cash;
  9.  
  10.     string gender;
  11.     cin >> gender;
  12.  
  13.     int age;
  14.     cin >> age;
  15.  
  16.     string sport;
  17.     cin >> sport;
  18.  
  19.     if (gender == "m") {
  20.         if (sport == "Gym") {
  21.             sum = 42;
  22.         }
  23.         else if (sport == "Boxing") {
  24.             sum = 41;
  25.         }
  26.         else if (sport == "Yoga") {
  27.             sum = 45;
  28.         }
  29.         else if (sport == "Zumba") {
  30.             sum = 34;
  31.         }
  32.         else if (sport == "Dances") {
  33.             sum = 51;
  34.         }
  35.         else if (sport == "Pilates") {
  36.             sum = 39;
  37.         }
  38.     }
  39.     else {
  40.         if (sport == "Gym") {
  41.             sum = 35;
  42.  
  43.         }
  44.         else if (sport == "Boxing") {
  45.             sum = 37;
  46.         }
  47.         else if (sport == "Yoga") {
  48.             sum = 42;
  49.         }
  50.         else if (sport == "Zumba") {
  51.             sum = 31;
  52.         }
  53.  
  54.         else if (sport == "Dances") {
  55.             sum = 53;
  56.         }
  57.         else if (sport == "Pilates") {
  58.             sum = 37;
  59.         }
  60.     }
  61.  
  62.     if (age <= 19) {
  63.         sum *= 0.8;
  64.     }
  65.  
  66.     if (sum <= cash) {
  67.         cout << "You purchased a 1 month pass for " << sport << ".\n";
  68.     }
  69.     else {
  70.         cout << "You don't have enough money! You need $" << fixed << setprecision(2) << sum - cash << " more.\n";
  71.     }
  72.     return 0;
  73. }
  74.  
  75. Решение с тернарен оператор:
  76.  
  77. #include <iostream>
  78. #include <string>
  79.  
  80. using namespace std;
  81.  
  82. int main() {
  83.     double cash, sum;
  84.     cin >> cash;
  85.  
  86.     string gender;
  87.     cin >> gender;
  88.  
  89.     int age;
  90.     cin >> age;
  91.  
  92.     string sport;
  93.     cin >> sport;
  94.  
  95.     sum =
  96.         (sport == "Gym" ? (gender == "m" ? 42 : 35) :
  97.             sport == "Boxing" ? (gender == "m" ? 41 : 37) :
  98.             sport == "Yoga" ? (gender == "m" ? 45 : 42) :
  99.             sport == "Zumba" ? (gender == "m" ? 34 : 31) :
  100.             sport == "Dances" ? (gender == "m" ? 51 : 53) :
  101.             sport == "Pilates" ? (gender == "m" ? 39 : 37) : 0) * (age <= 19 ? 0.8 : 1);
  102.  
  103.     sum <= cash
  104.         ? printf("You purchased a 1 month pass for %s.\n", sport.c_str())
  105.         : printf("You don't have enough money! You need $%.2f more.\n", sum - cash);
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement