Advertisement
Spocoman

10. Weather Forecast - Part 2

Sep 1st, 2023 (edited)
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     double degrees;
  7.     cin >> degrees;
  8.  
  9.     string weather;
  10.  
  11.     if (degrees >= 5 && degrees < 12) {
  12.         weather = "Cold";
  13.     }
  14.     else if (degrees >= 12 && degrees < 15) {
  15.         weather = "Cool";
  16.     }
  17.     else if (degrees >= 15 && degrees <= 20) {
  18.         weather = "Mild";
  19.     }
  20.     else if (degrees > 20 && degrees < 26) {
  21.         weather = "Warm";
  22.     }
  23.     else if (degrees >= 26 && degrees <= 35) {
  24.         weather = "Hot";
  25.     }
  26.     else {
  27.         weather = "unknown";
  28.     }
  29.  
  30.     cout << weather << endl;
  31.  
  32.     return 0;
  33. }
  34.  
  35. Решение с тернарен оператор:
  36.  
  37. #include <iostream>
  38.  
  39. using namespace std;
  40.  
  41. int main() {
  42.     double degrees;
  43.     cin >> degrees;
  44.  
  45.     string weather =
  46.         degrees >= 5 && degrees < 12 ? "Cold" :
  47.         degrees >= 12 && degrees < 15 ? "Cool" :
  48.         degrees >= 15 && degrees <= 20 ? "Mild" :
  49.         degrees > 20 && degrees < 26 ? "Warm" :
  50.         degrees >= 26 && degrees <= 35 ? "Hot" : "unknown";
  51.  
  52.     cout << weather << endl;
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement