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 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
bool isatriangle(double side1, double side2, double side3);
//Determine if the sides do form a triange, the sum of any two sides must be greater than the third side.
double semiperimeter(double side1, double side2, double side3);
double triperimeter(double side1, double side2, double side3);
//Calculate the perimeter.
double triarea(double side1, double side2, double side3);
//Calculate the area of a triange, using Heron's formula.
int main()
{
double area, perimeter, semiperimeter, side1, side2, side3;
bool isatriangle(double side1, double side2, double side3);
ifstream infile("c:\ExternalFiles\exfileTriangleSides.txt");
if (!infile) {
cerr << "Input file could not be opened" << endl;
exit(1);
}
ofstream outfile("c:\ExternalFiles\exfileTriOut.txt");
if (!outfile){
cerr << "Output file could not be opened" << endl;
exit(1);
}
infile >> side1 >> side2 >> side3;
//Create table
outfile << "\t\t\t Sides \t\t\t Triangle? \t\t\t Perimeter \t\t\t Area" << endl;
while (infile >> side1 >> side2 >> side3)
{
outfile << "\t\t\t Sides \t\t\t Triangle? \t\t\t Perimeter \t\t\tArea" << endl;
outfile << fixed << setprecision(4) << side1 << side2 << side3 << "\t\t\t" << isatriangle << "\t\t\t" << triperimeter << "\t\t\t" << triarea << endl;
}
infile.close();
outfile.close();
system("pause");
return 0;
}
bool isatriangle(double side1, double side2, double side3)
{
if (side1 + side2 >= side3 && side1 + side3 >= side2 && side2 + side3 >= side1)
cout << "True" ;
else
cout << "False" ;
}
double triperimeter(double side1, double side2, double side3)
{
double perimeter,
perimeter = side1 + side2 + side3;
cout << fixed << setprecision(3) << perimeter << endl;
}
double semiperimeter(double side1, double side2, double side3)
{
double s;
s = (side1 + side2 + side3) / 2.0;
cout << fixed << setprecision(3) << s << endl;
}
double triarea(double side1, double side2, double side3)
{
double area;
area = sqrt(semiperimeter(side1, side2, side3) * (semiperimeter(side1, side2, side3) - side1) * (semiperimeter(side1, side2, side3) - side2) * (semiperimeter(side1, side2, side3) - side3));
cout << fixed << setprecision(3) << area << endl;
}
|