Write a C++ program that will read sets of three pairs of coordinates from a tex
ID: 645064 • Letter: W
Question
Write a C++ program that will read sets of three pairs of coordinates from a text file. If these numbers represent three collinear points, your program will print the points and a message indicating that they are collinear. If these numbers represent three non-collinear points, your program will print a report showing the the points, the lengths of the sides of the triangle formed by the three points, its area and perimeter, and classify the triangle according to the definitions below.
An isosceles triangle is a triangle with (at least) two equal sides.
A triangle with all sides equal is called an equilateral triangle.
A triangle with no two sides equal is called a scalene triangle.
A right triangle is a triangle whose sides, a, b, and c, satisfy the equation c2 = a2 + b2.
A triangle with all sides equal is called an equilateral triangle. A triangle with no two sides equal is called a scalene triangle. A right triangle is a triangle whose sides, a, b, and c, satisfy the equation c2 = a2 + b2. The program should prompt the user for the name of the input file. If the input file was not successfully opened, your program displays a message in- dicating so and terminate. If the input file was successfully opened, your program should then generate a report following the format in the sample run. Ensure that numeric values in the report are formatted as shown in the report and displayed to the nearest thousandths (three decimal places). The report should indicate which sets of three points in the file represent the vertices of a triangle and which sets of three points are collinear. If a set of three points on a line are non-collinear, your program should calculate and display the area and perimeter of the triangle formed using those points as vertices and then classify the triangle, indicating whether it is a right, isosceles, equilateral and scalene triangle. Once the data from the input file have been processed, be sure to close the file. Each line in the input file has six numbers representing x1, y1, x2, y2, x3 and y3, in that order, where the x and y are the coordinates of three points. A typical program interaction would be:Explanation / Answer
#include <iostream>
#include<math.h>
#include<fstream>
using namespace std;
bool istriangle(double,double,double);
void calcTriangle(double, double, double ,double& , double& );
bool isRight(double , double , double );
bool isEquilateral(double , double , double );
bool isIsosceles(double , double , double );
bool isScalene(double , double , double );
int main()
{double a,b,c,area,perimeter;
ifstream input;
input.open("triple.in"); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
return 1;
}
cout<<"Triangle Program Report ======================== ";
input>>a;
while(input)
{input>>b>>c;
if(istriangle(a,b,c))
{cout<<"the sides of the triangle are: A="<<a<<";B= "<<b<<";C= "<<c<<endl;
calcTriangle(a,b,c,perimeter,area);
cout<<"Perimeter = "<<perimeter<<" Area = "<<area<<" Classification: ";
cout<<"Right: ";
if(isRight(a,b,c))
cout<<"Y ";
else
cout<<"N ";
cout<<"Isosceles: ";
if(isIsosceles(a,b,c))
cout<<"Y ";
else
cout<<"N ";
cout<<"Scalene: ";
if(isScalene(a,b,c))
cout<<"Y ";
else
cout<<"N ";
cout<<"Equilateral: ";
if(isEquilateral(a,b,c))
cout<<"Y ";
else
cout<<"N ";
}
else
cout<<"***"<<a<<", "<<b<<", and "<<c<<" cannot form the sides of a triangle *** ";
cout<<"--------------------------- ";
input>>a;
}
cout<<"*** END OF REPORT *** ======================== ";
system("pause");
return 0;
}
bool isRight(double sideA, double sideB, double sideC)
{double a,b,c;
a=sideA*sideA;
b=sideB*sideB;
c=sideC*sideC;
if(a+b==c||a+c==b||b+c==a)
return true;
else
return false;
}
bool isEquilateral(double a, double b, double c)
{if(a==b&&b==c)
return true;
else
return false;
}
bool isIsosceles(double a, double b, double c)
{if(a==b||b==c||a==c)
return true;
else
return false;
}
bool isScalene(double a, double b, double c)
{if(a!=b&&a!=c&&b!=c)
return true;
else
return false;
}
void calcTriangle(double A, double B, double C,double& perimeter, double& area)
{perimeter=A+B+C;
double S=perimeter/2;
area=sqrt(S*(S-A)*(S-B)*(S-C));
}
bool istriangle(double a,double b,double c)
{
if(a>=b+c||b>=a+c||c>=a+b)
return false;
else
return true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.