Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Colin Fitzpatrick
- Date: 12/1/2019
- Filename: Triangle.cpp
- Purpose: Given 3 integer values representing the sides of a
- triangle from an input file, will determine properties
- Error Checking: N/A
- Assumptions: A valid input file is named
- Input/Output: Output sent to screen and results.out or error.log.
- */
- #include <iostream> //Input/Output stream
- #include <iomanip> //Use for precision and layout
- #include<fstream> //Use for inputting and outputting from a file
- #include <cmath> //Use for square root
- #include <string> //Use for strings
- using namespace std;
- // Function prototypes
- bool IsTriangle(int sideA, int sideB, int sideC); //IsTriangle Prototype
- bool IsRight(int sideA, int sideB, int sideC); //IsRight Prototype
- bool IsScalene(int sideA, int sideB, int sideC);//IsScalene Prototype
- bool IsIsosceles(int sideA, int sideB, int sideC);//IsIsosceles Prototype
- bool IsEquilateral(int sideA, int sideB, int sideC);//IsEquilateral Prototype
- void CalcPerimeter(int sideA, int sideB, int sideC, float& perimeter, float& sumPerimeter);//CalcPerimeter Prototype
- void CalcArea(int sideA, int sideB, int sideC, float perimeter, float& sumArea);//CalcArea Protoype
- void CalcAverages(float sumArea, float sumPerimeter, int totalTri, float& areaAverage, float& perAverage);
- void PrintErrorHeading(ofstream& dout, string outputError);
- void PrintHeading(ofstream& dout, string outputFileRes, int totalTri, int totalRight, int totalScal, int totalIso, int totalEqu,
- float areaAverage, float perAverage);//PrintHeading Prototype
- void PrintErrorLog(ofstream& dout, string outputError, int sideA,
- int sideB, int sideC, int& pass);//PrintErrorLog Prototype
- void GetStatistics(int triResult, int rightResult, int scalResult, int isoResult, int equResult, int& totalTri,
- int& totalRight, int& totalScal, int& totalIso, int& totalEqu); //GetStats Prototype
- void PrintStatistics(ofstream& dout, string outputFileRes, int totalTri, int totalRight, int totalScal, int totalIso, int totalEqu,
- float areaAverage, float perAverage); //PrintStats Prototype
- int main()
- {
- int sideA; //Side 1
- int sideB; //Side 2
- int sideC; //Side 3
- int totalTri = 0; //Total amount of triangles
- int totalRight = 0; //Total amount of right triangles
- int totalScal = 0; //Total amount of scalene triangles
- int totalIso = 0; //Total amount of isosceles triangles
- int totalEqu = 0; //Total amount of equilateral triangles
- int pass = 0; // Used to count how many times Error log heading is printed
- bool triResult; //IsTriangle result
- bool scalResult; //IsScalene result
- bool rightResult; //IsRight result
- bool isoResult; //IsIsoscoles result
- bool equResult;//Is Equilateral result
- float sumArea = 0; //Sum of all triangles
- float sumPerimeter = 0; //Sum of perimeter of all triangles
- float perimeter = 0; //Perimeter of each triangle
- float perAverage = 0; //Average perimeter
- float areaAverage = 0; //Average area
- string inputFile; //Intput file name
- string outputFileRes = "results.out"; //results file name
- string outputError = "error.log"; //error log file name
- ifstream din; //File input
- ofstream dout; //File output
- //Gets user input and checks validity
- do
- {
- cout << "Enter a file name below:" << endl;
- cin >> inputFile;
- din.open(inputFile);
- } while (!din);
- din >> sideA >> sideB >> sideC;
- while (din)
- {
- triResult = IsTriangle(sideA, sideB, sideC);//Saves results of IsTriangle in triResult
- rightResult = IsRight(sideA, sideB, sideC);//Saves results of IsRight in rightResult
- scalResult = IsScalene(sideA, sideB, sideC);//Saves results of IsScalene in scalResult
- isoResult = IsIsosceles(sideA, sideB, sideC);//Saves results of IsIsosceles in isoResult
- equResult = IsEquilateral(sideA, sideB, sideC); //Saves results of IsEquilateral in equResult
- //For if it does not form a triangle
- if (triResult == false)
- {
- PrintErrorLog(dout, outputError, sideA, sideB, sideC, pass);
- }
- else
- {
- //For if it does form a triangle
- GetStatistics(triResult, rightResult, scalResult, isoResult, equResult, totalTri, totalRight, totalScal, totalIso, totalEqu);
- CalcArea(sideA, sideB, sideC, perimeter, sumArea);
- CalcPerimeter(sideA, sideB, sideC, perimeter, sumPerimeter);
- }
- din >> sideA >> sideB >> sideC;
- }
- //Calls CalcAverages
- CalcAverages(sumArea, sumPerimeter, totalTri, areaAverage, perAverage);
- //Calls PrintHeading
- PrintHeading(dout, outputFileRes, totalTri, totalRight, totalScal,
- totalIso, totalEqu, areaAverage, perAverage);
- //Calls PrintStatistics
- PrintStatistics(dout, outputFileRes, totalTri, totalRight, totalScal,
- totalIso, totalEqu, areaAverage, perAverage);
- din.close(); //Closes din
- dout.close();//Closes dout
- system("pause");
- return 0;
- }
- //Function definitions
- bool IsTriangle(int sideA, int sideB, int sideC)
- {
- //Pre: inputs the three sides from main
- //Post: returns true or false to the caller
- //Purpose: returns true if the sides form a triangle, false otherwise
- if (sideA + sideB > sideC && sideB + sideC > sideA && sideC + sideA > sideB)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool IsRight(int sideA, int sideB, int sideC)
- {
- //Pre: Inputs three sides from main
- //Post: returns true or false to the caller
- //Purpose: Returns true if the triangle is a right triangle, false otherwise
- if ((sideB * sideB) + (sideC * sideC) == (sideA * sideA))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool IsScalene(int sideA, int sideB, int sideC)
- {
- //Pre: Inputs three sides from main
- //Post: returns true or false to the caller
- //Purpose: returns true if the triangle is a scalene triangle, false otherwise
- if (sideA != sideB && sideB != sideC && sideA != sideC)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool IsIsosceles(int sideA, int sideB, int sideC)
- {
- //Pre: Inputs three sides from main
- //Post: returns true or false to the caller
- //Purpose: returns true if the triangle is a isosceles triangle, false otherwise
- if (sideA == sideB || sideB == sideC || sideC == sideA)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool IsEquilateral(int sideA, int sideB, int sideC)
- {
- //Pre: Inputs three sides from main
- //Post: returns true or false to the caller
- //Purpose: returns true if the triangle is a equilateral triangle, false otherwise
- if (sideA == sideB && sideB == sideC)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- void CalcArea(int sideA, int sideB, int sideC, float perimeter, float& sumArea)
- {
- //Pre: Inputs three sides from main
- //Post: returns the area to the caller
- //Purpose: if gicen the sides of a triangle, it calculates the area and returns it
- float total;
- float perHalf;
- perHalf = perimeter / 2.0f;
- total = sqrt(perHalf * (perHalf - (float)sideA) * (perHalf - (float)sideB) * (perHalf - (float)sideC));
- sumArea = sumArea + total;
- }
- void CalcPerimeter(int sideA, int sideB, int sideC, float& perimeter, float& sumPerimeter)
- {
- //Pre: Inputs parameters from main
- //Post: returns perimeter and sum to the caller
- //Purpose: If parameters are given, returns perimeter and sum
- perimeter = (float)sideA + (float)sideB + (float)sideC;
- sumPerimeter = perimeter + sumPerimeter;
- }
- void GetStatistics(int triResult, int rightResult, int scalResult, int isoResult, int equResult, int& totalTri,
- int& totalRight, int& totalScal, int& totalIso, int& totalEqu)
- {
- // Pre: Parameters inputted from main
- // Post: Determines whether or not the sides form the different triangle types
- // Purpose: Determines if the sides form any triangle types and increment counters by one
- //totalTri counter
- if (triResult == true)
- {
- totalTri++;
- }
- //totalRight counter
- if (rightResult == true)
- {
- totalRight++;
- }
- //totalScal counter
- if (scalResult == true)
- {
- totalScal++;
- }
- //totalIso counter
- if (isoResult == true)
- {
- totalIso++;
- }
- //totalEqu counter
- if (equResult == true)
- {
- totalEqu++;
- }
- }
- void PrintHeading(ofstream& dout, string outputFileRes, int totalTri, int totalRight, int totalScal, int totalIso,
- int totalEqu, float areaAverage, float perAverage)
- {
- // Pre: Parameters inputted from main
- // Post: Results header printed
- // Purpose: Prints the results file heading
- dout.open(outputFileRes);
- //Prints heading
- dout << fixed << showpoint << setprecision(2);
- dout << "**********************************************************" << endl;
- dout << "*" << setw(57) << "*" << endl;
- dout << "*" << setw(38) << "Triangle Statistics" << setw(19) << "*" << endl;
- dout << "*" << setw(31) << "Output file name: " << outputFileRes << setw(15) << "*" << endl;
- dout << "*" << setw(57) << "*" << endl;
- dout << "**********************************************************" << endl;
- //Calls PrintStatistics
- PrintStatistics(dout, outputFileRes, totalTri, totalRight, totalScal, totalIso, totalEqu, areaAverage, perAverage);
- dout.close(); //Closes dout
- }
- void PrintStatistics(ofstream& dout, string outputFileRes, int totalTri, int totalRight,
- int totalScal, int totalIso, int totalEqu, float areaAverage, float perAverage)
- {
- // Pre: Parameters inputted from main
- // Post: Triangle statistics are printed
- // Purpose: Prints triangle statistics to file
- dout << endl;
- dout << "The file has the following statistics:" << endl;
- dout << setw(5) << totalTri << " total # of triangle(s)" << endl;
- dout << setw(5) << totalIso << " isosceles triangle(s)" << endl;
- dout << setw(5) << totalEqu << " equilateral triangle(s)" << endl;
- dout << setw(5) << totalScal << " scalene traingle(s)" << endl;
- dout << setw(5) << totalRight << " right triangle(s)" << endl;
- dout << setw(8) << areaAverage << " is the average area of all triangles" << endl;
- dout << setw(8) << perAverage << " is the average perimeter of all triangles" << endl;
- }
- void PrintErrorHeading(ofstream& dout, string outputError)
- {
- // Pre: Parameters inputted from main
- // Post: Error log heading outputted
- // Purpose: Prints the error log heading
- dout.open(outputError);
- dout << fixed << showpoint << setprecision(2);
- dout << "**********************************************************" << endl;
- dout << "*" << setw(57) << "*" << endl;
- dout << "*" << setw(38) << "Triangle Statistics" << setw(19) << "*" << endl;
- dout << "*" << setw(30) << "Output file name: " << outputError << setw(18) << "*" << endl;
- dout << "*" << setw(57) << "*" << endl;
- dout << "**********************************************************" << endl;
- dout << endl;
- dout << "The following sides are invalid:" << setw(5) << endl;
- dout.close();
- }
- void PrintErrorLog(ofstream& dout, string outputError, int sideA, int sideB, int sideC, int& pass)
- {
- // Pre: Parameters inputted from main
- // Post: Sides that create errors are outputted to error.log
- // Purpose: Outputs sides that create errors to error log with the reason for the error
- while (pass == 0)
- {
- PrintErrorHeading(dout, outputError);
- pass++;
- }
- dout.open(outputError, ios::app); //Found on google, I couldn't get it to work without it.
- dout << setw(5);
- if (sideA < 0 || sideB < 0 || sideC < 0)
- {
- dout << sideA << setw(3) << sideB << setw(3) << sideC
- << setw(3) << " - side(s) can not be negative" << endl;
- }
- else if (sideA == 0 || sideB == 0 || sideC == 0)
- {
- dout << sideA << setw(3) << sideB << setw(3) << sideC
- << setw(3) << " - side(s) can not be zero" << endl;
- }
- else
- {
- dout << sideA << setw(3) << sideB << setw(3) << sideC
- << setw(3) << " - side(s) can not form triangle" << endl;
- }
- dout.close();
- }
- void CalcAverages(float sumArea, float sumPerimeter, int totalTri, float& areaAverage, float& perAverage)
- {
- // Pre: paramaters are inputted from main
- // Post: Calculates average area and perimeter
- // Purpose: Calculates average area and perimeter and returns results
- areaAverage = sumArea / (float)totalTri;
- perAverage = sumPerimeter / (float)totalTri;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement