You will write a class to help young geometry enthusiast to solve the side and a
ID: 3707574 • Letter: Y
Question
You will write a class to help young geometry enthusiast to solve the side and angle problem given to all geometry students by their teachers. This class will be able to calculate all three angles and all three sides (if possible) given three bits of information about the triangle Specifications: The input combinations will be either 3 sides, 3 angles, 2 sides/1 angle, 2 angles/1 side, or only 1 or 2 inputs. If a solution is not possible, the class will notify the student The class should allow the student to output the solution (all side and angle values) or state if no solution is possible The program should also state what type of problem it is that it is solving (AAA, AAS, ASA, SAS, SSA, or SSS). For a description of these, please see the website: https: //www.mathsisfun.com/algebra/triq-solving-triangles.html The class shouïd allow the student to set and get any side?r angle The class should allow the student to get the type of problem to be solved (once three bits of information have been given) . The class should allow the student to initiate a solving of the triangle problem All numbers should be printed in a fixed decimal format with only one decimal place always showing A basic program is given to test your class. Please be aware that this will not test ALL possibilities. It is your responsibility to test all possibilities Assumptions: 1) If two solutions exist, only report one 2) The inputs will always to valid for a triangle 3) There will not be more than 3 inputs (there may be less)Explanation / Answer
#include <math.h> /// Input/output library
#include <iostream>
#include <iomanip>
using namespace std; /// Set namespace to std
class Triangle
{
double a,b,c,A,B,C; // a,b,c for sides & A,B,C for angle of triangle
int enough=1; // variable keep track for enough input. 0 value means not enough input
string prob; // varible store the type of triangle problem
public:
//Declaration of class function
Triangle(); //Constructor function
void setSideA(double s);
void setSideB(double s);
void setSideC(double s);
void setAngleA(double x);
void setAngleB(double x);
void setAngleC(double x);
void solve();
void printSolution();
void clearTriangle();
}; // End of class defination
// function defination of class Triangle
Triangle::Triangle()
{a=b=c=A=B=C=0;}
void Triangle::setSideA(double s)
{a=s;}
void Triangle::setSideB(double s)
{b=s;}
void Triangle::setSideC(double s)
{c=s;}
void Triangle::setAngleA(double x)
{A=x;}
void Triangle::setAngleB(double x)
{B=x;}
void Triangle::setAngleC(double x)
{C=x;}
void Triangle::solve()
{ double radianA,radianB,radianC,toradian,todegree;
toradian=3.1415/180; // used to convert from degree to radian
todegree=180/3.1415; // used to convert from radian to degree
// converting A,B,C angles to radian
radianA=A*toradian;
radianB=B*toradian;
radianC=C*toradian;
prob="";
if(a!=0 && b!=0 && c!=0)
{ prob="SSS";
A = acos((b*b + c*c - a*a) / (2*b*c))*todegree;
B = acos((a*a + c*c - b*b) / (2*a*c))*todegree;
C = acos((a*a + b*b - c*c) / (2*a*b))*todegree;
}
else
if(a!=0 && b!=0 && A!=0)
{prob="SSA";
B=asin(b*sin(radianA)/a)*180/3.1415;
C=180-A-B;
radianC=C*toradian;
c=(sin(radianC)*todegree)*a/(sin(radianA)*todegree);
}
else
if(a!=0 && b!=0 && C!=0)
{prob="SAS";
radianC=C*toradian;
c=sqrt(a*a+b*b-2*a*b*cos(radianC));
A=asin((a*(sin(radianC)))/c)*180/3.1415;
B=180-A-C;}
else
if(A!=0 && b!=0 && C!=0)
{ prob="ASA";
B=180-A-C;
radianB=B*toradian;
a= (sin(radianA)*todegree)*b/(sin(radianB)*todegree);
c= (sin(radianC)*todegree)*b/(sin(radianB)*todegree);
}
else
if(A!=0 && B!=0 && a!=0)
{prob="ASA";
C=180-A-B;
radianC=C*toradian;
b= ((sin(radianB)*todegree)*a)/(sin(radianA)*todegree);
c= ((sin(radianC)*todegree)*b)/(sin(radianB)*todegree);
}
else
if((A!=0 && B!=0 && C!=0) && (a==0 && b==0 && c==0))
{prob="AAA";enough=0;}
else
enough=0; // not enough input
} //End of solve() function
void Triangle::printSolution()
{if(enough==1) // condition for enough input & right problem selection
{ cout<<endl<<"This is a "<<prob<<" problem..."<<endl;
cout <<fixed<<setprecision(1);
cout<<"side a="<<a<<endl;
cout<<"side b="<<b<<endl;
cout<<"side c="<<c<<endl;
cout<<"angle A="<<A<<" degree"<<endl;
cout<<"angle B="<<B<<" degree"<<endl;
cout<<"angle C="<<C<<" degree"<<endl<<endl;
}
else
if(enough==0 && prob=="") // not enough information
{
cout<<"Not enough information to solve!"<<endl;
enough=1;
}
if(prob=="AAA" && enough==0) // if all the inputs are angles
{ cout<<endl<<"This is a "<<prob<<" problem..."<<endl;
cout<<"The sides can not be detemined from just from angles"<<endl;
enough=1;
}
} //End of printSolution() function
void Triangle::clearTriangle() // Clear the value of all the sides & angles
{a=b=c=A=B=C=0;}
/// This is a program to test the Traingle Solver class
int main()
{
/// Declare variables
Triangle triangle; /// Create a Triangle object
/// Let’s test SSS
cout << "Setting side a to 3" << endl;
triangle.setSideA(3);
cout << "Setting side b to 4" << endl;
triangle.setSideB(4);
cout << "Setting side c to 6" << endl;
triangle.setSideC(6);
/// Solve the SSS
cout << "Solving the SSS problem" << endl;
triangle.solve();
/// Printing the solution
cout << "Printing the SSS problem" << endl;
triangle.printSolution();
/// Clear Triangle
cout << "Clearing triangle" << endl;
cout << endl;
triangle.clearTriangle();
/// Let’s test SSA
cout << "Setting side a to 17" << endl;
triangle.setSideA(17);
cout << "Setting side b to 19" << endl;
triangle.setSideB(19);
cout << "Setting angle A to 49" << endl;
triangle.setAngleA(49);
/// Solve the SSA
cout << "Solving the SSA problem" << endl;
triangle.solve();
/// Printing the solution
cout << "Printing the SSA problem" << endl;
triangle.printSolution();
/// Clear Triangle
cout << "Clearing triangle" << endl;
cout << endl;
triangle.clearTriangle();
/// Let’s test SAS
cout << "Setting side a to 17" << endl;
triangle.setSideA(17);
cout << "Setting side b to 19" << endl;
triangle.setSideB(19);
cout << "Setting angle C to 49" << endl;
triangle.setAngleC(49);
/// Solve the SAS
cout << "Solving the SSA problem" << endl;
triangle.solve();
/// Printing the solution
cout << "Printing the SAS problem" << endl;
triangle.printSolution();
/// Clear Triangle
cout << "Clearing triangle" << endl;
cout << endl;
triangle.clearTriangle();
/// Let’s test ASA
cout << "Setting angle A to 24" << endl;
triangle.setAngleA(24);
cout << "Setting side b to 19" << endl;
triangle.setSideB(19);
cout << "Setting angle C to 49" << endl;
triangle.setAngleC(49);
/// Solve the ASA
cout << "Solving the ASA problem" << endl;
triangle.solve();
/// Printing the solution
cout << "Printing the ASA problem" << endl;
triangle.printSolution();
/// Clear Triangle
cout << "Clearing triangle" << endl;
cout << endl;
triangle.clearTriangle();
/// Let’s test AAS
cout << "Setting angle A to 89" << endl;
triangle.setAngleA(89);
cout << "Setting angle B to 47" << endl;
triangle.setAngleB(47);
cout << "Setting side a to 34" << endl;
triangle.setSideA(34);
/// Solve the AAS
cout << "Solving the AAS problem" << endl;
triangle.solve();
/// Printing the solution
cout << "Printing the AAS problem" << endl;
triangle.printSolution();
/// Clear Triangle
cout << "Clearing triangle" << endl;
cout << endl;
triangle.clearTriangle();
/// Let’s test AAA
cout << "Setting angle A to 89" << endl;
triangle.setAngleA(89);
cout << "Setting angle B to 47" << endl;
triangle.setAngleB(47);
cout << "Setting angle C to 44" << endl;
triangle.setAngleC(44);
/// Solve the AAA
cout << "Solving the AAA problem" << endl;
triangle.solve();
/// Printing the solution
cout << "Printing the AAA problem" << endl;
triangle.printSolution();
/// Clear Triangle
cout << "Clearing triangle" << endl;
cout << endl;
triangle.clearTriangle();
/// Let’s test too few inputs
cout << "Setting angle A to 89" << endl;
triangle.setAngleA(89);
cout << "Setting angle B to 47" << endl;
triangle.setAngleB(47);
/// Solve the too few inputs
cout << "Solving with too few inputs" << endl;
triangle.solve();
/// Printing the solution
cout << "Printing the too few inputs problem" << endl;
triangle.printSolution();
return 0;
} /// end main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.