1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
//This is a program that reads in 3 integers from an input file (triangle2.txt)
//that represent line lengths and determines whether or not the three segments
//form a right triangle. If they don't, it checks whether they form a valid
//triangle at all. Line lengths must be between 1 and 5000.
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
ifstream infile;
ofstream outfile;
int main()
{
int a, b, c, d, hyp, short1, short2 = 0; //Decalre and initialize variables a. b, c
infile.open("triangle2.txt"); //Open infile triangles.txt
if(!infile) //If infile does not open
{
cout << "Error opening input file." << endl; //Display error message
return 1; //Quit
}
outfile.open("answers.out"); //Open outfile answers.out
if(!outfile) //If outfile does not open
{
cout << "Error opening output file." << endl; //Display error message
return 1; //Quit
}
infile >> a >> b >> c; //Read in a, b, c
while( infile ) //While reading in info
{
outfile << setw(14) << "Three sides:" << a << " " << b << " " << c << endl; //Print to outfile a, b, c
cout << setw(14) << "Three sides:" << a << " " << b << " " << c << endl; //Display a, b, c
if(a && b && c <= 5000) //If a, b, and c are less than or equal to 5000
{
d = max(a,b); //Find the larger of the first 2 sides
hyp = max(d,c); //Determine longest side (hypotenuse)
short1 = min(a,b); //Determine short sides
short2 = min (short1, c);
if ( hyp == (sqrt(pow(short1,2)+ pow(short2,2)))) //Check to see if it is a right triangle
{
outfile << " -Above dimensions indicate a right triangle." << endl;//Display that it's a right triangle
}
else
{
if((a + b > c) || (b + c > a) || (a + c > b)) //If not, check to see if it is a valid triangle
{
outfile << " -Above dimensions indicate a valid triangle." << endl; //Display that it's a valid triangle
cout << " -Above dimensions indicate a valid triangle." << endl; //Echo valid triangle
}
else //If not valid triangle,
{
outfile << " -Above dimensions indicate that this is NOT a valid triangle." << endl; //This is NOT a triangle
cout << " -Above dimensions indicate that this is NOT a valid triangle." << endl; //Echo display
}
}
}
else //Or else, if it's not between 1 and 5000
{
outfile << " -This program only computes triangles with sides between 1 and 5000." << endl; //Distplay error
cout << " -This program only computes triangles with sides between 1 and 5000." << endl; //Echo error
}
infile >> a >> b >> c; //Read in the next line of data from infile
}
return 0; //Quit
}
|