Advertisement
kyamaliev

Example for BGMAMMA

Apr 26th, 2022
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. // main method at the end of file
  7.  
  8. int problem1(double a, double b, double c) {
  9.     string validTriangleMessage = "The triangle is a valid right angle triangle.\n";
  10.     string invalidTriangleMessage = "No triangle can be constructed from the given sides.\n";
  11.     string invalidPythagoreanTripleMessage = "There exists no such a right angle triangle with the given sides.\n";
  12.     string noNegativeSidesMessage = "Sides of a triangle must be positive\n";
  13.  
  14.     if (a < 0 || b < 0 || c < 0) {
  15.         cout << noNegativeSidesMessage;
  16.  
  17.         return(1);
  18.     }
  19.  
  20.     // is it really safe to compare doubles for equality in C++?
  21.     // triangle inequality
  22.     bool isValidTriangle = (a < c + b) && (b < a + c) && (c < a + b);
  23.     // for the case where the sides of the triangle are given arbitrarily
  24.     bool isValidPythagoreanTriple = (a * a + b * b == c * c) || (c * c + b * b == a * a) || (c * c + a * a == b * b);
  25.  
  26.     if (isValidTriangle)
  27.     {
  28.         if (isValidPythagoreanTriple) {
  29.             cout << validTriangleMessage;
  30.         }
  31.         else {
  32.             cout << invalidPythagoreanTripleMessage;
  33.         }
  34.     }
  35.     else {
  36.         cout << invalidTriangleMessage;
  37.     }
  38.  
  39.     cout << endl;
  40. }
  41.  
  42. void problem2(double a, double b, double c) {
  43.     // variant 1 - conditional statements
  44.     double max_if = 0;
  45.  
  46.     if (a >= b && a >= c)
  47.     {
  48.         max_if = a;
  49.     }
  50.     else if (b >= c && b >= a)
  51.     {
  52.         max_if = b;
  53.     }
  54.     else
  55.     {
  56.         max_if = c;
  57.     }
  58.  
  59.     cout << "The maximum of the 3 numbers (calculated with IFs) is:  " << max_if << endl;
  60.     //variant2 - using array and loops
  61.  
  62.     double numbers[3] = { a, b, c };
  63.     double max_loop = -numeric_limits<double>::max();
  64.     for (int i = 0; i < size(numbers); i++)
  65.     {
  66.         if (numbers[i] >= max_loop) {
  67.             max_loop = numbers[i];
  68.         }
  69.     }
  70.  
  71.     cout << "The maximum of the 3 numbers (calculated with loop) is: " << max_loop << endl;
  72.     cout << endl;
  73. }
  74.  
  75. void problem3(int number) {
  76.     for (int i = 1; i <= number; ++i)
  77.     {
  78.         if (i == 1)
  79.         {
  80.             cout << setw(3) << "X";
  81.             for (int k = 1; k <= number; k++)
  82.             {
  83.                 cout << setw(6) << k;
  84.             }
  85.  
  86.             cout << endl;
  87.             for (int i = 0; i <= 6 * number; i++)
  88.             {
  89.                 cout << "-";
  90.             }
  91.  
  92.             cout << "---" << endl;
  93.         }
  94.  
  95.         cout << std::setw(3) << i;
  96.         for (int j = 1; j <= number; ++j)
  97.         {
  98.             cout << setw(6) << i * j;
  99.         }
  100.  
  101.         cout << endl;//<< endl;
  102.     }
  103. }
  104.  
  105. int main() {
  106.     //cout << "Please enter a number to run the corresponding program (1, 2 or 3) or anything else to exit." << endl;
  107.     int programNumber;
  108.     while (true)
  109.     {
  110.         cout << "Please enter a number to run the corresponding program (1, 2 or 3) or anything else to exit." << endl;
  111.         cin >> programNumber;
  112.         double a, b, c;
  113.         switch (programNumber)
  114.         {
  115.         case 1:
  116.             cout << "Please enter three numbers to check if they can be sides of a right angle triangle" << endl;
  117.             cin >> a >> b >> c;
  118.             problem1(a, b, c);
  119.             break;
  120.         case 2:
  121.             cout << "Please enter three numbers. The program will return the biggest one." << endl;
  122.             cin >> a >> b >> c;
  123.             problem2(a, b, c);
  124.             break;
  125.         case 3:
  126.             cout << "Please enter an integer. The program will create the multiplication table up to this integer." << endl;
  127.             int n;
  128.             cin >> n;
  129.             problem3(n);
  130.             break;
  131.         default:
  132.             cout << "You have to choose 1, 2 or 3. Goodbye." << endl;
  133.             return 0;
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement