This repository has been archived by the owner on Oct 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathtriangle_area.cpp
77 lines (63 loc) · 2.43 KB
/
triangle_area.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <cmath>
double basicFormula(double b, double h) {
return b*h/2;
}
double heronsFormula(double a, double b, double c) {
double s = (a + b + c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
double trigonometry(double a, double b, double angle) {
double theta = angle * M_PI / 180;
return a*b*sin(theta)/2;
}
int main() {
int selection;
std::cout << "Triangle Area Calculator" << std::endl
<< "Which formula do you want to use?" << std::endl
<< "1] Basic Formula (given base and height)" << std::endl
<< "2] Heron's Formula (given all sides)" << std::endl
<< "3] With Trigonometry (given two adjacent sides and the angle between them)" << std::endl
<< "(Type 1 / 2 / 3 to select): ";
std::cin >> selection;
switch (selection) {
case 1: {
double base, height;
std::cout << "Basic Formula:" << std::endl
<< "Give me the base of the triangle: ";
std::cin >> base;
std::cout << "Give me the height of the triangle: ";
std::cin >> height;
std::cout << "The area of the triangle with a base of " << base
<< " and a height of " << height << " is " << basicFormula(base, height);
break;
}
case 2: {
double a, b, c;
std::cout << "Heron's Formula:" << std::endl
<< "Give me all the sides (space separated): ";
std::cin >> a >> b >> c;
std::cout << "The area of the triangle with sides (" << a << ", " << b << ", " << c << ") is "
<< heronsFormula(a, b, c);
break;
}
case 3: {
double a, b, angle;
std::cout << "Using Trigonometry:" << std::endl
<< "Give me the two adjacent sides (space separated): ";
std::cin >> a >> b;
std::cout << "Give me the angle between them (in degrees): ";
std::cin >> angle;
std::cout << "The area of the triangle with two adjacent sides (" << a << ", " << b
<< ") and an angle of "
<< angle << " degrees between them is " << trigonometry(a, b, angle);
break;
}
default: {
std::cout << "Invalid Choice!";
break;
}
}
std::cout << std::endl;
return 0;
}