Advertisement
Lavig

Другий семестр. Лабораторна робота №7-8 (Завдання 1)

Mar 14th, 2025
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #define _USE_MATH_DEFINES
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. double calculate_area(double length, double width) {
  9.     return length * width;
  10. }
  11. double calculate_area(double radius) {
  12.     return M_PI * radius * radius;
  13. }
  14. int main()
  15. {
  16.     SetConsoleOutputCP(1251);
  17.     SetConsoleCP(1251);
  18.     double a{}, b{}, r{};
  19.     while (true) {
  20.         cout << "Введіть довжину прямокутника: ";
  21.         cin >> a;
  22.         if (cin.fail() || a <= 0) {
  23.             cin.clear();
  24.             cin.ignore(32767, '\n');
  25.             cout << "Довжину прямокутника було введено неправильно. Спробуйте ще раз!" << endl;
  26.             continue;
  27.         }
  28.         else {
  29.             break;
  30.         }
  31.     }
  32.     while (true) {
  33.         cout << "Введіть ширину прямокутника: ";
  34.         cin >> b;
  35.         if (cin.fail() || b <= 0) {
  36.             cin.clear();
  37.             cin.ignore(32767, '\n');
  38.             cout << "Ширину прямокутника було введено неправильно. Спробуйте ще раз!" << endl;
  39.             continue;
  40.         }
  41.         else {
  42.             break;
  43.         }
  44.     }
  45.     while (true) {
  46.         cout << "Введіть радіус кола: ";
  47.         cin >> r;
  48.         if (cin.fail() || r <= 0) {
  49.             cin.clear();
  50.             cin.ignore(32767, '\n');
  51.             cout << "Радіус кола було введено неправильно. Спробуйте ще раз!" << endl;
  52.             continue;
  53.         }
  54.         else {
  55.             break;
  56.         }
  57.     }
  58.     cout << "Площа прямокутника: " << calculate_area(a, b) << endl;
  59.     cout << "Площа круга: " << calculate_area(r) << endl;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement