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 103 104 105 106 107 108 109 110
|
#include <iostream> // Used for cin and cout
#include <cmath> // Used for square root function
using namespace std;
void description();
//Postcondition: Description of the program is printed to the screen.
void get_input(double side_a, double side_b, double side_c);
//Precondition: User enters correct values.
//Postcondition: The length of sides a, b, and c of a triangle
//are assigned to variables side_a, side_b, and side_c
bool check_triangle(double side_a, double side_b, double side_c);
//Precondition: Side lengths a, b, and c are positive nonzero numbers.
//Postcondition: If the side lengths a, b, c can correctly form a triangle
//a function to calculate the semipeerimieter, perimeter, and area will be called.
double semiperimter(double side_a, double side_b, double side_c);
//Precondition: Side lengths a, b, and c are positive nonzero numbers.
//Postcondition: Returns the value of the semiperimeter of the triangle.
void area_perimeter(double side_a, double side_b, double side_c, double perimeter, double area);
//Precondition: Side lengths a, b, and c are positive nonzero numbers.
//Postcondition: Returns the perimeter and area of a triangle.
void show_output() //Write this function later on
int main ()
{
char answer;
do
{
description();
get_input(side_a, side_b, side_c);
check_triangle(side_a, side_b, side_c);
//I want the area_perimeter function to be called here is check_triangle returns true
//I assume I should should just write an output function that prints the perimeter and area to the screen
}
while (answer == 'y' || answer == 'Y');
return 0;
}
void description()
{
cout << "This program calculates the perimeter and area of a triangle";
<< "based on inputed values of three side lengths.\n";
}
void get_input(double side_a, double side_b, double side_c) //Should I ask the user to input values in
{ //ascending order and put the hypotenuse as the 3rd value?
cout << "Input the lengths of three sides of a triangle: ";
cin >> side_a;
while (side_a <= 0)
{
cout << "Enter a positive nonzero number for the length of side a";
cin >> side_a;
}
cout << ",";
cin >> side_b;
while (side_b <= 0)
{
cout << "Enter a positive nonzero number for the length of side b";
cin >> side_b;
}
cout << ",";
cin side_c;
while (side_c <= 0)
{
cout << "Enter a positive nonzero number for the length of side c";
cin >> side_c;
}
cout << endl;
}
bool check_triangle(double side_a, double side_b, double side_c)
{
if (side_a + side_b >= side_c) //I would like a way to take any sides lengths and reorder them
return true; //so that I can always check if the sum of two sides are greater
else //or equal to the longest side
return false;
cout << "Those side lengths cannot produce a triangle.\n";
cout << "Make sure
//If the boolean function is true I want a function to calculate area to be called.
//If the boolean function is false I want an error message to be printed to the screen.
//How do I do that?
double semiperimter(double side_a, double side_b, double side_c)
{
double s;
s = (side_a + side_b + side_c)/2.0 //Used 2.0 just in case all side lengths are integers.
return s; //If pass by reference is used does that also pass the data type?
}
void area_perimeter(double side_a, double side_b, double side_c, double& perimeter, double& area);
{
double perimeter, area;
perimeter = side_a + side_b + side_c
area = sqrt ( semiperimter(side_a, side_b, side_c) * (semiperimter(side_a, side_b, side_c) - side_a)
* (semiperimter(side_a, side_b, side_c) - side_b) * (semiperimter(side_a, side_b, side_c) -side_c) )
}
//The calculation above is too long. I'm not sure what the professor wants. Would it be best to define
// the semiperimeter function as double s(double a, doubleb, double c) so its s(a,b,c) when called?
// I've looked online and read a little about pointers but the professor doesn't want us to use anything we
//haven't learned in class.
| |