1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
double getDouble(string prompt, double min, double max);
bool isValidTriangle(double s1, double s2, double s3);
double calcArea(double s1, double s2, double s3);
int main() {
cout << "This program calculates the area of a triangle." << endl;
double s1 = getDouble("Enter the length of side 1 (1-100): ",1,100);
double s2 = getDouble("Enter the length of side 2 (1-100): ", 1, 100);
double s3 = getDouble("Enter the length of side 3 (1-100): ", 1, 100);
isValidTriangle(s1, s2, s3);
calcArea(s1, s2, s3);
if (calcArea < 0) cout << "Sorry, that is not a valid triangle.";
else cout << "The area of the triangle is " << fixed << showpoint << setprecision(1) << calcArea;
system("pause");
return 0;
}
double getDouble(string prompt, double min, double max) {
double guess;
cout << prompt;
cin >> guess;
return guess;
}
bool isValidTriangle(double s1, double s2, double s3) {
double largest = s1;
if (s2 > largest) largest = s2;
if (s3 > largest) largest = s3;
if (largest * 2 < s1 + s2 + s3) {
return true;
}
return false;
}
double calcArea(double s1, double s2, double s3) {
if (isValidTriangle == false) return -1;
double perimeter = s1 + s2 + s3;
double s = perimeter / 2;
double area = sqrt(s * ((s - s1)*(s - s2)*(s - s3)));
return area;
}
|