Sie sind auf Seite 1von 6

#include <iostream>

#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;

/**
* determines whether three numbers satisfies the triangle
* inequality; that is, can form the sides of a triangle.
* @param num1 a non-negative fixed-point value
* @param num2 a non-negative fixed-point value
* @param num3 a non-negative fixed-point value
* @return true if these numbers satisfy the triangle
* inequality; otherwise, false is returned. Also returns
* false if any of the numbers is non-positive.
*/
bool isTriangle(double num1, double num2, double num3)
{
bool status;

if(num2+num3 > num1 && num1+num3 > num2 && num1+num2 >
num3)
{
status = true;
}
else
{
status = false;
}
return status;
}

/**
* determines whether a triangle is a right-triangle
* @param sideA the length of the first side of the triangle
* @param sideB the length of the second side of the triangle
* @param sideC the length of the third side of the triangle
* @return true if the sum of squares of the lengths of two sides
* is equal to the third side; otherwise, false is returned.
*/
bool isRight(double sideA, double sideB, double sideC)
{
bool status;

if(pow(sideC, 2) == pow(sideA, 2)+pow(sideB, 2) || pow(sideB, 2) ==


pow(sideA, 2)+pow(sideC, 2) || pow(sideA, 2) == pow$
{
status = true;
}
else
{
status = false;
}
return status;
}

/**
* determines whether a triangle is an equilateral triangle
* @param sideA the length of the first side of the triangle
* @param sideB the length of the second side of the triangle
* @param sideC the length of the third side of the triangle
* @return true if the sides are equal; otherwise,
* false is returned.
*/
bool isEquilateral(double sideA, double sideB, double sideC)
{
bool status;

if(sideA == sideB == sideC)


{
status = true;
}
else
{
status = false;
}
return status;
}
/**
* determines whether a triangle is an isosceles triangle
* @param sideA the length of the first side of the triangle
* @param sideB the length of the second side of the triangle
* @param sideC the length of the third side of the triangle
* @return true if at least two sides are equal; otherwise,
* false is returned.
*/
bool isIsosceles(double sideA, double sideB, double sideC)
{
bool status;

if(sideA == sideB || sideA == sideC || sideB == sideC)


{
status = true;
}
else
{
status = false;
}
return status;
}

/**
* determines whether a triangle is a scalene triangle
* @param sideA the length of the first side of the triangle
* @param sideB the length of the second side of the triangle
* @param sideC the length of the third side of the triangle
* @return true if no two sides have the same lengths;
* otherwise, false is returned.
*/
bool isScalene(double sideA, double sideB, double sideC)
{
bool status;

if(sideA != sideB && sideA != sideC && sideB != sideC)


{
status = true;
}
else
{
status = false;
}
return status;
}

/**
* computes the area and perimeter of a triangle
* @param sideA the length of the first side of the triangle
* @param sideB the length of the second side of the triangle
* @param sideC the length of the third side of the triangle
* @param perimeter the perimeter of the triangle
* @param area the area of the triangle using Herron’s formula.
*/
void calcTriangle(double sideA, double sideB, double sideC, double&
perimeter, double S, double& area)
{
perimeter=sideA+sideB+sideC;
S=perimeter/2;
area=sqrt(S*(S-sideA)*(S-sideB)*(S-sideC));
}

int main()
{
double num1, num2, num3;
double sideA, sideB, sideC, perimeter, area, S;
string triple;
fstream inFile;

cout<<endl;
cout<<"Enter the name of the data file> ";
cin>>triple;
inFile.open("triple.in",ios::in);
if (inFile.fail())
{
cout<<"Unable to open triple.in for input."<<endl;
return 1;
}
cout<<endl;
cout<<" Triangle Program Report "<<endl;
cout<<"================================="<<endl;

while (inFile>>sideA)
{
inFile>>sideB>>sideC;

calcTriangle(sideA, sideB, sideC, perimeter, S, area);

if(isTriangle(sideA, sideB, sideC))


{
cout<<"The sides of the triangle are:"<<endl;
cout<<setprecision(3)<<showpoint<<"A = "<<sideA<<"; B =
"<<sideB<<"; C = "<<sideC<<endl;
cout<<endl;
cout<<"Perimeter =
"<<setprecision(7)<<showpoint<<setw(8)<<right<<perimeter<<endl;
cout<<"Area =
"<<setprecision(7)<<showpoint<<setw(8)<<right<<area<<endl;
cout<<endl;
cout<<"Classification:"<<endl;

if(isRight(sideA, sideB, sideC))


{
cout<<"Right: Y"<<endl;
}
else
{
cout<<"Right: N"<<endl;
}

if(isIsosceles(sideA, sideB, sideC))


{
cout<<"Isosceles: Y"<<endl;
}
else
{
cout<<"Isosceles: N"<<endl;
}

if(isScalene(sideA, sideB, sideC))


{
cout<<"scalene: Y"<<endl;
}
else
{
cout<<"scalene: N"<<endl;
}

if(isEquilateral(sideA, sideB, sideC))


{
cout<<"Equilateral: Y"<<endl;
}
else
{
cout<<"Equilateral: N"<<endl;
}

}
else
{
cout<<setprecision(3)<<"*** "<<sideA<<", "<<sideB<<",
"<<sideC<<" cannot form the sides of a triangle ***"<<endl;
}

cout<<"---------------------------------"<<endl;
}
cout<<"*** END OF REPORT ***"<<endl;
cout<<"================================="<<endl;
cout<<endl;

inFile.close();
return 0;
}

Das könnte Ihnen auch gefallen