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 111 112 113
|
#include "Exception.h"
//Includes interface file which contains function declarations/prototypes
//NOTE: when moving code into embedded compiler, you will need to change the file location string
#include <cmath> //Needed for pow and sqrt functions
#include <iostream>
using namespace std;
//*******************************************
//Function to get number of sides from user *
//*******************************************
int get_sides() {
int side_count;
//Obtain number of sides
cout << "Enter the number of sides: ";
cin >> side_count;
//If answer is zero, assign 1 as number of sides; this will cause the SIDE loop further down to correctly obtain the number of sides
if (side_count == 0)
side_count = 1;
//Throw an exception if the side count is not 1, 3, or 4
//Note, this exception will also capture 0 or negative values, so no separate exception is needed for those values
if (side_count != 1 && side_count != 3 && side_count != 4)
throw Exception::BadSide(side_count); //Use parameterized constructor to store bad value within exception object
return side_count;
}
//***********************************************
//Function to get dimensions of sides from user *
//***********************************************
float *get_dimensions(int side_count) {
//Try/catch construct to catch a bad_alloc as a result of allocating a dynamic array
try {
//Allocate an array equal to the number of sides entered
float *ptr = new float[side_count];
//SIDE loop to obtain length of each side
for (int count = 0; count <= (side_count - 1); count++) //Loop will iterate as many times as there are sides, to obtain length of all sides
{
//Obtain dimensions of number of sides indicated by user
cout << "Enter the dimension of side " << count + 1 << ": ";
cin >> ptr[count];
}
return ptr;
} //end of try block
catch (bad_alloc) {
//Because this try/catch construct is a nested try/catch inside of the test driver file that calls this function, this catch will rethrow
//the exception to be handled in the outer catch block in the test driver; this way, all exceptions are handled in main for simplicity
throw; //Rethrow exception to outer catch block
}
}
//*****************************************************************************************
//Function to test array of dimensions and throw exception depending on issue encountered *
//*****************************************************************************************
void check_dimensions(float *arrptr, int size) {
//****************** Invalid value exception ******************
//Check array of sides for invalid values (0 or negative)
for (int i = 0; i < size; i++) {
if (arrptr[i] <= 0)
throw Exception::InvalidVal(arrptr[i]);
}
//****************** Illegal triangle exception ******************
bool illegal = false; //Flag used to validate dimensions in ensuing code
//If user indicated 3 sides (triangle), and any of the side dimensions are illegal (substracting side from the half perimeter yields a negative number),
//throw an exception
if (size == 3) {
//Calculate half perimeter as sum of all sides divided by 2
float half_peri = 0;
for (int i = 0; i < size; i++)
half_peri += arrptr[i];
//Check that no side substracted from the half_perimeter yields 0 or a negative number
illegal = false; //Initialize illegal flag to false
for (int i = 0; i < size; i++) {
if (((half_peri / 2.0f) - arrptr[i]) <= 0)
illegal = true;
}
//Throw exception if the illegal flag is true
if (illegal)
throw Exception::BadTriangle(arrptr, size); //Pass address of array containing bad dimensions to exception object
}
//****************** Illegal square exception ******************
//If user indicated 4 sides (square), and any sides don't match, throw an exception
if (size == 4)
{
//Confirm that dimensions of all sides are equal
//IMPT: upper bound must be the number of sides - 1, in order to avoid an off by one error
for (int i = 0; i < (size - 1); i++) {
if (arrptr[i] != arrptr[i + 1])
illegal = true;
}
//Throw exception if the illegal flag is true
if (illegal)
throw Exception::BadSquare(arrptr, size); //Pass address of array containing bad dimensions to exception object
}
}
|