Advertisement
Guest User

Pythagorean Theorem Calculator

a guest
Apr 23rd, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.58 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream> // Standard preprocessor directives
  3. #include <math.h>
  4. #include <string>
  5.  
  6. using std::cout; // So we don't have to continuously use std::cout or std::cin whenever taking user output or input
  7. using std::cin;
  8.  
  9. int LEAST_ACCEPTABLE_NUMBER = 1; // A leg's value can't be less than one
  10.  
  11. void menu()
  12. {
  13.     cout << "Menu options\n\n";
  14.     cout << "Enter 1 and press Enter to calculate regular pythagorean theorem problem(a squared + b squared = c squared)\n";
  15.     cout << "Enter 2 and press Enter to find if a set makes a pythagorean tri\n";
  16.     cout << "Enter 3 and press Enter to find the length of a leg\n";
  17.     cout << "Enter 4 and press Enter to find if a triangle is accute, obtuse, or is a right triangle\n";
  18.     cout << "Enter 5 and press Enter to exit\n\n";
  19. }
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23.     float entered_a, entered_b, entered_c; // For holding values that are user input
  24.     float changed_a, changed_b, changed_c; // For holding new values.
  25.     int choice; // For when the user enters input for the menu.
  26.  
  27. menu_return:
  28.  
  29.     menu(); // Call the menu function
  30.     cout << "Choice: ";
  31.     cin >> choice;
  32.  
  33.     if (choice > 5)
  34.     {
  35.         cout << "That is not an option, please try again.\n";
  36.         cin.clear();
  37.         cin.ignore(10000, '\n');
  38.         goto menu_return;
  39.  
  40.     }
  41.     else if (cin.fail()) // If the user tries to enter a letter
  42.     {
  43.         cout << "\nNice try buster!\n";
  44.         cin.clear();
  45.         return 1;
  46.     }
  47.     switch (choice)
  48.     {
  49.     case 1:
  50.         cout << "Please enter the first leg value: ";
  51.         cin >> entered_a;
  52.  
  53.         cout << "\nPlease enter the second leg value: ";
  54.         cin >> entered_b;
  55.  
  56.         if (entered_a < LEAST_ACCEPTABLE_NUMBER || entered_b < LEAST_ACCEPTABLE_NUMBER)
  57.         {
  58.             cout << "\nA legs length can not be less than 1\n\n";
  59.             goto menu_return;
  60.         }
  61.  
  62.         changed_a = pow(entered_a, 2);
  63.         changed_b = pow(entered_b, 2);
  64.         changed_c = changed_a + changed_b;
  65.  
  66.         cout << "\nANSWER: " << sqrt(changed_c) << "\n";
  67.         cout << "SOLUTION: " << entered_a << " squared + " << entered_b << " squared = " << changed_c << " Then square root that and you get " << sqrt(changed_c) << " \n\n";
  68.         goto menu_return;
  69.  
  70.         break;
  71.  
  72.     case 2:
  73.         cout << "Please enter the first leg value: ";
  74.         cin >> entered_a;
  75.  
  76.         cout << "\nPlease enter the second leg value: ";
  77.         cin >> entered_b;
  78.  
  79.         cout << "\nPlease enter the hypotenuse value: ";
  80.         cin >> entered_c;
  81.  
  82.         if (entered_a < LEAST_ACCEPTABLE_NUMBER || entered_b < LEAST_ACCEPTABLE_NUMBER || entered_c < LEAST_ACCEPTABLE_NUMBER)
  83.         {
  84.             cout << "\nNeither the leg nor the hypotenuse can be below 1\n\n";
  85.             goto menu_return;
  86.         }
  87.  
  88.         changed_a = pow(entered_a, 2);
  89.         changed_b = pow(entered_b, 2);
  90.         changed_c = pow(entered_c, 2);
  91.  
  92.         if (changed_a + changed_b == changed_c)
  93.         {
  94.             cout << "\nThe set makes a correct pythagorean tri.\n";
  95.             cout << "SOLUTION: " << entered_a << " squared + " << entered_b << " is equal to " << entered_c << " squared\n\n";
  96.  
  97.             goto menu_return;
  98.  
  99.         }
  100.         else if (changed_a + changed_b > changed_c || changed_a + changed_b < changed_c)
  101.         {
  102.             cout << "\nThe set does not make a correct pythagorean tri.\n";
  103.             cout << "REASON: " << entered_a << " squared + " << entered_b << " squared does not equal " << entered_c << " squared\n\n";
  104.  
  105.             goto menu_return;
  106.         }
  107.  
  108.  
  109.         break;
  110.  
  111.     case 3:
  112.         cout << "Please enter the first leg value: ";
  113.         cin >> entered_a;
  114.  
  115.         cout << "Please enter the hypotenuse value: ";
  116.         cin >> entered_c;
  117.  
  118.         if (entered_a > entered_c || entered_a < LEAST_ACCEPTABLE_NUMBER || entered_c < LEAST_ACCEPTABLE_NUMBER) // Condition where if the leg value is more than the hypotenuse value
  119.         {
  120.             cout << "\nThe leg can not be longer than the hypotenuse.\n\n";
  121.             goto menu_return;
  122.         }
  123.  
  124.         changed_a = pow(entered_a, 2);
  125.         changed_c = pow(entered_c, 2);
  126.         changed_b = changed_c - changed_a;
  127.  
  128.         cout << "Length of missing leg: " << sqrt(changed_b);
  129.         cout << "REASON: " << entered_c << " squared - " << entered_a << " squared equals " << changed_b << " and then square root it to get " << sqrt(changed_b) << "\n\n";
  130.  
  131.  
  132.         break;
  133.  
  134.     case 4:
  135.         cout << "Please enter the first leg value: ";
  136.         cin >> entered_a;
  137.  
  138.         cout << "Please enter the second leg value: ";
  139.         cin >> entered_b;
  140.  
  141.         cout << "Please enter the hypotenuse value: ";
  142.         cin >> entered_c;
  143.  
  144.         if (entered_a < LEAST_ACCEPTABLE_NUMBER || entered_b < LEAST_ACCEPTABLE_NUMBER || entered_c < LEAST_ACCEPTABLE_NUMBER)
  145.         {
  146.             cout << "\nThe leg or hypotenuse value can not be less than 1.\n\n";
  147.             goto menu_return;
  148.         }
  149.         if (entered_a > entered_c || entered_b > entered_c)
  150.         {
  151.             cout << "\nThe leg values can not be larger than the hypotenuse value.\n\n";
  152.             goto menu_return;
  153.         }
  154.  
  155.         changed_a = pow(entered_a, 2);
  156.         changed_b = pow(entered_b, 2);
  157.         changed_c = pow(entered_c, 2);
  158.  
  159.         if (changed_a + changed_b > changed_c)
  160.         {
  161.             cout << "\nThe triangle is accute.\n";
  162.             cout << "REASON: " << entered_a << " squared + " << entered_b << " squared is larger than " << entered_c << " squared, therefor the triangle is accute.\n\n";
  163.             goto menu_return;
  164.  
  165.         }
  166.         else if (changed_a + changed_b < changed_c)
  167.         {
  168.             cout << "\nThe triangle is obtuse.\n";
  169.             cout << "REASON: " << entered_a << " squared + " << entered_b << " squared is less than " << entered_c << " squared, therefor the triangle is obtuse.\n\n";
  170.             goto menu_return;
  171.  
  172.         }
  173.         else if (changed_a + changed_b == changed_c)
  174.         {
  175.             cout << "\nThe triangle is a right triangle.\n";
  176.             cout << "REASON: " << entered_a << " squared + " << entered_b << " squared is equal to " << entered_c << " squared, therefor the triangle is a right triangle.\n\n";
  177.             goto menu_return;
  178.         }
  179.  
  180.         break;
  181.     }
  182.  
  183.     return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement