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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#include <vector>
using std::vector;
#include <algorithm>
using std::sort;
// note the "triangle::" part. We need to specify the function's
// FULL name to avoid confusion. Else, the compiler will think we
// are just defining a new function called "perimeter"
unsigned long triangle::perimeter() {
return s1+s2+s3;
}
unsigned long triangle::area() {
// TODO: write this function.
// Note: why is it okay to return an integer here? Recall that
// all of our triangles have integer sides, and are right triangles...
// put the sides in an array:
unsigned long sides[3] = {s1,s2,s3};
// sort the array:
sort(sides,sides+3);
// at this point, sides[0] <= sides[1] <= sides[2]
unsigned long b = sides[0];
unsigned long h = sides[1];
return (b*h)/2;
}
void triangle::print() {
cout << "[" << s1 << "," << s2 << "," << s3 << "]";
}
bool congruent(triangle t1, triangle t2) {
// TODO: write this function.
int a, b, c;
a = t1.s1;
b = t1.s2;
c = t1.s3;
unsigned long tr1[3] = {a,b,c};
// sort the array:
sort(tr1,tr1+3);
// at this point, tr1[0]<= tr1[1] <= tr1[2]
int d,e,f;
d = t2.s1;
e = t2.s2;
f = t2.s3;
unsigned long tr2[3] = {d,e,f};
// sort the array:
sort(tr2,tr2+3);
// at this point, tr2[0] <= tr2[1] <= tr2[2]
if(tr1[0] == tr2[0] && tr1[1] == tr2[1] && tr1[2] == tr2[2]){
true;
}
else false;
}
bool similar(triangle t1, triangle t2) {
// TODO: write this function.
int a, b, c;
a = t1.s1;
b = t1.s2;
c = t1.s3;
unsigned long tr1[3] = {a,b,c};
// sort the array:
sort(tr1,tr1+3);
// at this point, tr1[0]<= tr1[1] <= tr1[2]
int d,e,f;
d = t2.s1;
e = t2.s2;
f = t2.s3;
unsigned long tr2[3] = {d,e,f};
// sort the array:
sort(tr2,tr2+3);
// at this point, tr2[0] <= tr2[1] <= tr2[2
if(tr1[0]%tr2[0] == 0 && tr1[1]%tr2[1] == 0 && tr1[2]%tr2[2] == 0){
true;
}
else false;
}
vector<triangle> findRightTriangles(unsigned long l, unsigned long h) {
// TODO: find all the right triangles with integer sides,
// subject to the perimeter bigger than l and less than h
vector<triangle> retval; // storage for return value.
triangle t1;
t1.s1=l;
t1.s3=h;
for (unsigned long p = 0; p < t1.s3; p++) {
t1.s2=p;
if ( p >= t1.s1 && p <= t1.s3 && (((t1.s1*t1.s1)+(p*p)) == (t1.s3*t1.s3))){
retval.push_back(t1);
break;
}
}
return retval;
}
|