/*This code was written by A. El-Gadi of Tripoli University for educational purposes. The code illustrates function overloading in C++. It uses the several ways of determining a triangle as an example. For any questions use the comment section below.*/
#include<iostream>
#include<cmath>
using namespace std;
class Angle{public: float ang;};
class Triangle
{
public:
float a,b,c;
float area();
float perimeter();
void setSides(float s1,float s2,float s3){a=s1;b=s2;c=s3;}//SSS
void setSides(float s1,Angle alpha,float s2){a=s1;b=sqrt(s1*s1+s2*s2-s1*s2*cos(alpha.ang));c=s2;}//SAS
void setSides(Angle alpha,float s,Angle beta){a=s*sin(alpha.ang)/sin(beta.ang);c=s;b=s*sin(beta.ang)/sin(alpha.ang);}//ASA
void setSides(Angle alpha,Angle gamma,float s){
float beta;
beta=3.14159-alpha.ang-gamma.ang;
a=s*sin(alpha.ang)/sin(beta);c=s;b=s*sin(beta)/sin(alpha.ang);}//AAS
void setSides(float s,Angle alpha,Angle gamma){setSides(alpha,gamma,s);}//SAA
};
float Triangle::area(){
float s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
float Triangle::perimeter(){return a+b+c;}
int main(){
Triangle tSSS,tSAS,tASA,tAAS; Angle alpha,beta;
alpha.ang=0.7; beta.ang=3.14159/7;
float a,b,c;
a=10;
b=15;
c=20;
tSSS.setSides(a,b,c);
tSAS.setSides(a,alpha,c);
tASA.setSides(alpha,a,beta);
tAAS.setSides(alpha,beta,b);
cout<<tSSS.area()<<endl;
cout<<tSAS.area()<<endl;
cout<<tASA.area()<<endl;
cout<<tAAS.area()<<endl;
return 0;
}
#include<cmath>
using namespace std;
class Angle{public: float ang;};
class Triangle
{
public:
float a,b,c;
float area();
float perimeter();
void setSides(float s1,float s2,float s3){a=s1;b=s2;c=s3;}//SSS
void setSides(float s1,Angle alpha,float s2){a=s1;b=sqrt(s1*s1+s2*s2-s1*s2*cos(alpha.ang));c=s2;}//SAS
void setSides(Angle alpha,float s,Angle beta){a=s*sin(alpha.ang)/sin(beta.ang);c=s;b=s*sin(beta.ang)/sin(alpha.ang);}//ASA
void setSides(Angle alpha,Angle gamma,float s){
float beta;
beta=3.14159-alpha.ang-gamma.ang;
a=s*sin(alpha.ang)/sin(beta);c=s;b=s*sin(beta)/sin(alpha.ang);}//AAS
void setSides(float s,Angle alpha,Angle gamma){setSides(alpha,gamma,s);}//SAA
};
float Triangle::area(){
float s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
float Triangle::perimeter(){return a+b+c;}
int main(){
Triangle tSSS,tSAS,tASA,tAAS; Angle alpha,beta;
alpha.ang=0.7; beta.ang=3.14159/7;
float a,b,c;
a=10;
b=15;
c=20;
tSSS.setSides(a,b,c);
tSAS.setSides(a,alpha,c);
tASA.setSides(alpha,a,beta);
tAAS.setSides(alpha,beta,b);
cout<<tSSS.area()<<endl;
cout<<tSAS.area()<<endl;
cout<<tASA.area()<<endl;
cout<<tAAS.area()<<endl;
return 0;
}
No comments:
Post a Comment