I must create a program with the triangle class and then create a class with the following exceptions:
- An error message is issued if one of the sides has a value of zero.
- An error message is issued (at its own decision) if one of the sides of the triangle is negative.
- An error message is issued if a triangle could not be built (violation of the triangular inequality).
- Suppose there is a triangle resize (itn factor) {... .code ...} method; in the class triangle that allows us to scale a triangle object according to the introduced factor, ie: if the factor = -2 means that we are going to reduce the triangle 2 units (sides / 2), on the contrary if factor = 2, it means that let's increase the triangle 2 units (sides * 2). Control exceptions for when the triangle can no longer be reduced and for a possible division by zero (factor = 0).
This is the code that I have developed so far:
#pragma once
#ifndef Triangle6_h
#define Triangle6_h
#include <iostream>
#include <cmath>
using namespace std;
class Triangle {
protected:
int lado1, lado2, lado3;
public:
Triangle() {
}
Triangle(int lado1, int lado2, int lado3) {
this->lado1 = lado1;
this->lado2 = lado2;
this->lado3 = lado3;
}
void setLado1(int l1) {
lado1 = l1;
}
int getLado1() {
return lado1;
}
void setLado2(int l2) {
lado2 = l2;
}
int getLado2() {
return lado2;
}
void setLado3(int l3) {
lado3 = l3;
}
int getLado3() {
return lado3;
}
double area() {
double s, area;
s = (lado1 + lado2 + lado3) / 2;
area = sqrt(s*(s - lado1)*(s - lado2)*(s - lado3));
return area;
}
int perimetro() {
return (lado1 + lado2 + lado3);
}
Triangle resize(int x) {
int l1 = lado1;
int l2 = lado2;
int l3 = lado3;
switch (x) {
case(2):
lado1 = (l1 * x);
lado2 = (l2 * x);
lado3 = (l3 * x);
cout << "Lado 1: " << lado1 << "\nLado 2: " << lado2 << "\nLado 3: " << lado3 << endl;
break;
case(-2):
lado1 = (l1 / x);
lado2 = (l2 / x);
lado3 = (l3 / x);
cout << "Lado 1: " << lado1 << "\nLado 2: " << lado2 << "\nLado 3: " << lado3 << endl;
break;
}
}
};
#endif /*Triangle6_h*/
#pragma once
#ifndef myexception1_h
#define myexception1_h
#include <iostream>
#include <exception>
using namespace std;
class myexception : public exception
{
virtual const char* what(int factor) const throw(){
switch (factor) {
case(0):
return "Error: Uno de los lados tiene valor 0.";
break;
case(1):
return "Error: Uno de los lados tiene valor negativo.";
break;
case(2):
return "Error: Violacion de la desigualdad triangular.";
break;
case(3):
return "Error: No se puede reducir mas el triangulo.";
break;
case(4):
return "Error: Division por 0.";
}
}
};
#endif /*myexception1_h*/
#include <iostream>
#include "Triangle6.h"
#include "myexception1.h"
using namespace std;
int main() {
Triangle Tri(2,2,2);
myexception myex;
int a, b, c;
cout << "Introduzca los tres lados de un triangulo: ";
cin >> a >> b >> c;
bool flag = true;
do {
try {
if (a == 0) {
throw myex;
}
else if (b == 0) {
throw myex;
}
else if (c == 0) {
throw myex;
}
else {
Tri.setLado1(a);
Tri.setLado2(b);
Tri.setLado3(c);
}
}
catch (exception& e1) {
cout << e1.what(0) << "Uno de los lados es 0." << '\n';
flag = false;
}
try {
if (a < 0) {
throw myex;
}
else if (b < 0) {
throw myex;
}
else if (c < 0) {
throw myex;
}
else {
Tri.setLado1(a);
Tri.setLado2(b);
Tri.setLado3(c);
}
}
catch (exception& e2) {
cout << e2.what(1) << "Uno de los lados es negativo." << '\n';
flag = false;
}
try {
if (a > b + c) {
throw myex;
}
else if (b > a + c) {
throw myex;
}
else if (c > a + b) {
throw myex;
}
else {
Tri.setLado1(a);
Tri.setLado2(b);
Tri.setLado3(c);
}
}
catch (exception& e3) {
cout << e3.what(2) << "La desigualdad triangular no se cumple." << '\n';
flag = false;
}
} while (flag == true);
return 0;
}
As far as I know what () can not be passed parameters, but I can not think of another way to print the different exceptions with switch.