Advertisement
rado_dimitrov66

Calculate Area&Perimeter

Nov 28th, 2023
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. bool isNumeric(string strChoose, bool isDouble)
  8. {
  9.     for (int i = 0; i < strChoose.length(); i++) {
  10.  
  11.         if (!isdigit(strChoose[i]) && isDouble && strChoose[i] == '.')
  12.         {
  13.             if (strChoose[i + 1] < '0' || strChoose[i + 1] > '9') { return false; }
  14.  
  15.         }
  16.         else if (!isdigit(strChoose[i])) {
  17.             return false;
  18.         }
  19.  
  20.  
  21.     }
  22.  
  23.     return true;
  24. }
  25.  
  26. void calculateTriangle()
  27. {
  28.     string strInput;
  29.     double sideA, sideB, sideC, halfP;
  30.  
  31.     cout << "Calculate perimeter and area of TRIANGLE" << endl;
  32.     cout << "" << endl;
  33.  
  34.     do
  35.     {
  36.         cout << "Enter value of side A(base) in cm: ";
  37.         cin >> strInput;
  38.  
  39.  
  40.         isNumeric(strInput, true) ? sideA = stod(strInput) : sideA = 0;
  41.  
  42.     } while (sideA <= 0);
  43.  
  44.     cout << "" << endl;
  45.  
  46.     do
  47.     {
  48.         cout << "Enter value of side B(left hit) in cm: ";
  49.         cin >> strInput;
  50.  
  51.  
  52.         isNumeric(strInput, true) ? sideB = stod(strInput) : sideB = 0;
  53.  
  54.     } while (sideB <= 0);
  55.  
  56.     cout << "" << endl;
  57.  
  58.     do
  59.     {
  60.         cout << "Enter value of side C(right hit) in cm: ";
  61.         cin >> strInput;
  62.  
  63.  
  64.         isNumeric(strInput, true) ? sideC = stod(strInput) : sideC = 0;
  65.  
  66.     } while (sideC <= 0);
  67.  
  68.     cout << "" << endl;
  69.  
  70.  
  71.     halfP = (sideA + sideB + sideC) / 2;
  72.  
  73.     cout << "Perimeter is: " << sideA + sideB + sideC << " cm" << endl;
  74.     cout << "Area is: " << sqrt(halfP * (halfP - sideA) * (halfP - sideB) * (halfP - sideC)) << " cm2" << endl;
  75. }
  76.  
  77. void calculateTrapezoid()
  78. {
  79.     string strInput;
  80.     double sideA, sideB, sideC, sideD, heigth;
  81.  
  82.     cout << "Calculate perimeter and area of TRAPEZOID" << endl;
  83.     cout << "" << endl;
  84.  
  85.     do
  86.     {
  87.         cout << "Enter value of side A(big base) in cm: ";
  88.         cin >> strInput;
  89.  
  90.  
  91.         isNumeric(strInput, true) ? sideA = stod(strInput) : sideA = 0;
  92.  
  93.     } while (sideA <= 0);
  94.  
  95.     cout << "" << endl;
  96.  
  97.     do
  98.     {
  99.         cout << "Enter value of side B(small base) in cm: ";
  100.         cin >> strInput;
  101.  
  102.  
  103.         isNumeric(strInput, true) ? sideB = stod(strInput) : sideB = 0;
  104.  
  105.     } while (sideB <= 0);
  106.  
  107.     cout << "" << endl;
  108.  
  109.     do
  110.     {
  111.         cout << "Enter value of side C(left hit) in cm: ";
  112.         cin >> strInput;
  113.  
  114.  
  115.         isNumeric(strInput, true) ? sideC = stod(strInput) : sideC = 0;
  116.  
  117.     } while (sideC <= 0);
  118.  
  119.     cout << "" << endl;
  120.  
  121.     do
  122.     {
  123.         cout << "Enter value of side D(right hit) in cm: ";
  124.         cin >> strInput;
  125.  
  126.  
  127.         isNumeric(strInput, true) ? sideD = stod(strInput) : sideD = 0;
  128.  
  129.     } while (sideD <= 0);
  130.  
  131.     cout << "" << endl;
  132.  
  133.     do
  134.     {
  135.         cout << "Enter value of heigth in cm: ";
  136.         cin >> strInput;
  137.  
  138.  
  139.         isNumeric(strInput, true) ? heigth = stod(strInput) : heigth = 0;
  140.  
  141.     } while (heigth <= 0);
  142.  
  143.     cout << "" << endl;
  144.  
  145.     cout << "" << endl;
  146.     cout << "Perimeter is: " << sideA + sideB + sideC + sideD << " cm" << endl;
  147.     cout << "Area is: " << ((sideA + sideB) * heigth) / 2 << " cm2" << endl;
  148. }
  149.  
  150. int main()
  151. {
  152.     string strChoose;
  153.     unsigned int choose;
  154.  
  155.     do
  156.     {
  157.         cout << "Enter value of figure\n[1]Triangle\n[2]Trapezoid\n: ";
  158.         cin >> strChoose;
  159.         cout << "" << endl;
  160.  
  161.  
  162.         isNumeric(strChoose, false) ? choose = stoul(strChoose) : choose = 0;
  163.  
  164.         if (choose == 1) {
  165.             calculateTriangle();
  166.  
  167.         }
  168.         else if (choose == 2) {
  169.             calculateTrapezoid();
  170.         }
  171.  
  172.         if (choose >= 1 && choose <= 2) {
  173.  
  174.             cout << "" << endl;
  175.             do
  176.             {
  177.                 cout << "New calculation? [y/Y - Yes or n/N - No]: ";
  178.                 cin >> strChoose;
  179.  
  180.                 if (strChoose == "Y" || strChoose == "y" || strChoose == "N" || strChoose == "n") {
  181.                     break;
  182.                 }
  183.                 else {
  184.                     strChoose == "countine";
  185.                     continue;
  186.                 }
  187.  
  188.             } while (true);
  189.  
  190.  
  191.             if (strChoose == "Y" || strChoose == "y") {
  192.                 system("CLS");
  193.                 choose = 0;
  194.             }
  195.             else {
  196.                 exit(0);
  197.             }
  198.  
  199.         }
  200.  
  201.  
  202.     } while (choose < 1 || choose > 2);
  203.  
  204.     return 0;
  205. }
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement