Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.95 KB | None | 0 0
  1. /*
  2. Author: Colin Fitzpatrick
  3. Date: 12/1/2019
  4. Filename: Triangle.cpp
  5. Purpose: Given 3 integer values representing the sides of a
  6. triangle from an input file, will determine properties
  7. Error Checking: N/A
  8. Assumptions: A valid input file is named
  9. Input/Output: Output sent to screen and results.out or error.log.
  10. */
  11.  
  12. #include <iostream> //Input/Output stream
  13. #include <iomanip> //Use for precision and layout
  14. #include<fstream> //Use for inputting and outputting from a file
  15. #include <cmath> //Use for square root
  16. #include <string> //Use for strings
  17. using namespace std;
  18.  
  19.  
  20. // Function prototypes
  21. bool IsTriangle(int sideA, int sideB, int sideC); //IsTriangle Prototype
  22.  
  23. bool IsRight(int sideA, int sideB, int sideC); //IsRight Prototype
  24.  
  25. bool IsScalene(int sideA, int sideB, int sideC);//IsScalene Prototype
  26.  
  27. bool IsIsosceles(int sideA, int sideB, int sideC);//IsIsosceles Prototype
  28.  
  29. bool IsEquilateral(int sideA, int sideB, int sideC);//IsEquilateral Prototype
  30.  
  31. void CalcPerimeter(int sideA, int sideB, int sideC, float& perimeter, float& sumPerimeter);//CalcPerimeter Prototype
  32.  
  33. void CalcArea(int sideA, int sideB, int sideC, float perimeter, float& sumArea);//CalcArea Protoype
  34.  
  35. void CalcAverages(float sumArea, float sumPerimeter, int totalTri, float& areaAverage, float& perAverage);
  36.  
  37. void PrintErrorHeading(ofstream& dout, string outputError);
  38.  
  39. void PrintHeading(ofstream& dout, string outputFileRes, int totalTri, int totalRight, int totalScal, int totalIso, int totalEqu,
  40. float areaAverage, float perAverage);//PrintHeading Prototype
  41.  
  42. void PrintErrorLog(ofstream& dout, string outputError, int sideA,
  43. int sideB, int sideC, int& pass);//PrintErrorLog Prototype
  44.  
  45. void GetStatistics(int triResult, int rightResult, int scalResult, int isoResult, int equResult, int& totalTri,
  46. int& totalRight, int& totalScal, int& totalIso, int& totalEqu); //GetStats Prototype
  47.  
  48. void PrintStatistics(ofstream& dout, string outputFileRes, int totalTri, int totalRight, int totalScal, int totalIso, int totalEqu,
  49. float areaAverage, float perAverage); //PrintStats Prototype
  50.  
  51.  
  52. int main()
  53. {
  54.  
  55.  
  56. int sideA; //Side 1
  57. int sideB; //Side 2
  58. int sideC; //Side 3
  59. int totalTri = 0; //Total amount of triangles
  60. int totalRight = 0; //Total amount of right triangles
  61. int totalScal = 0; //Total amount of scalene triangles
  62. int totalIso = 0; //Total amount of isosceles triangles
  63. int totalEqu = 0; //Total amount of equilateral triangles
  64. int pass = 0; // Used to count how many times Error log heading is printed
  65.  
  66. bool triResult; //IsTriangle result
  67. bool scalResult; //IsScalene result
  68. bool rightResult; //IsRight result
  69. bool isoResult; //IsIsoscoles result
  70. bool equResult;//Is Equilateral result
  71.  
  72. float sumArea = 0; //Sum of all triangles
  73. float sumPerimeter = 0; //Sum of perimeter of all triangles
  74. float perimeter = 0; //Perimeter of each triangle
  75. float perAverage = 0; //Average perimeter
  76. float areaAverage = 0; //Average area
  77.  
  78. string inputFile; //Intput file name
  79. string outputFileRes = "results.out"; //results file name
  80. string outputError = "error.log"; //error log file name
  81. ifstream din; //File input
  82. ofstream dout; //File output
  83.  
  84. //Gets user input and checks validity
  85. do
  86. {
  87. cout << "Enter a file name below:" << endl;
  88. cin >> inputFile;
  89. din.open(inputFile);
  90. } while (!din);
  91.  
  92. din >> sideA >> sideB >> sideC;
  93.  
  94. while (din)
  95. {
  96. triResult = IsTriangle(sideA, sideB, sideC);//Saves results of IsTriangle in triResult
  97.  
  98. rightResult = IsRight(sideA, sideB, sideC);//Saves results of IsRight in rightResult
  99.  
  100. scalResult = IsScalene(sideA, sideB, sideC);//Saves results of IsScalene in scalResult
  101.  
  102. isoResult = IsIsosceles(sideA, sideB, sideC);//Saves results of IsIsosceles in isoResult
  103.  
  104. equResult = IsEquilateral(sideA, sideB, sideC); //Saves results of IsEquilateral in equResult
  105.  
  106. //For if it does not form a triangle
  107. if (triResult == false)
  108. {
  109. PrintErrorLog(dout, outputError, sideA, sideB, sideC, pass);
  110. }
  111. else
  112. {
  113. //For if it does form a triangle
  114. GetStatistics(triResult, rightResult, scalResult, isoResult, equResult, totalTri, totalRight, totalScal, totalIso, totalEqu);
  115. CalcArea(sideA, sideB, sideC, perimeter, sumArea);
  116. CalcPerimeter(sideA, sideB, sideC, perimeter, sumPerimeter);
  117.  
  118. }
  119.  
  120. din >> sideA >> sideB >> sideC;
  121. }
  122.  
  123. //Calls CalcAverages
  124. CalcAverages(sumArea, sumPerimeter, totalTri, areaAverage, perAverage);
  125.  
  126. //Calls PrintHeading
  127. PrintHeading(dout, outputFileRes, totalTri, totalRight, totalScal,
  128. totalIso, totalEqu, areaAverage, perAverage);
  129.  
  130. //Calls PrintStatistics
  131. PrintStatistics(dout, outputFileRes, totalTri, totalRight, totalScal,
  132. totalIso, totalEqu, areaAverage, perAverage);
  133.  
  134. din.close(); //Closes din
  135. dout.close();//Closes dout
  136.  
  137. system("pause");
  138. return 0;
  139. }
  140.  
  141.  
  142.  
  143. //Function definitions
  144. bool IsTriangle(int sideA, int sideB, int sideC)
  145. {
  146. //Pre: inputs the three sides from main
  147. //Post: returns true or false to the caller
  148. //Purpose: returns true if the sides form a triangle, false otherwise
  149.  
  150. if (sideA + sideB > sideC && sideB + sideC > sideA && sideC + sideA > sideB)
  151. {
  152. return true;
  153. }
  154. else
  155. {
  156. return false;
  157. }
  158. }
  159.  
  160. bool IsRight(int sideA, int sideB, int sideC)
  161. {
  162. //Pre: Inputs three sides from main
  163. //Post: returns true or false to the caller
  164. //Purpose: Returns true if the triangle is a right triangle, false otherwise
  165.  
  166. if ((sideB * sideB) + (sideC * sideC) == (sideA * sideA))
  167. {
  168. return true;
  169. }
  170. else
  171. {
  172. return false;
  173. }
  174. }
  175.  
  176. bool IsScalene(int sideA, int sideB, int sideC)
  177. {
  178. //Pre: Inputs three sides from main
  179. //Post: returns true or false to the caller
  180. //Purpose: returns true if the triangle is a scalene triangle, false otherwise
  181.  
  182. if (sideA != sideB && sideB != sideC && sideA != sideC)
  183. {
  184. return true;
  185. }
  186. else
  187. {
  188. return false;
  189. }
  190. }
  191.  
  192. bool IsIsosceles(int sideA, int sideB, int sideC)
  193. {
  194. //Pre: Inputs three sides from main
  195. //Post: returns true or false to the caller
  196. //Purpose: returns true if the triangle is a isosceles triangle, false otherwise
  197.  
  198. if (sideA == sideB || sideB == sideC || sideC == sideA)
  199. {
  200. return true;
  201. }
  202. else
  203. {
  204. return false;
  205. }
  206. }
  207.  
  208. bool IsEquilateral(int sideA, int sideB, int sideC)
  209. {
  210. //Pre: Inputs three sides from main
  211. //Post: returns true or false to the caller
  212. //Purpose: returns true if the triangle is a equilateral triangle, false otherwise
  213.  
  214. if (sideA == sideB && sideB == sideC)
  215. {
  216. return true;
  217. }
  218. else
  219. {
  220. return false;
  221. }
  222. }
  223.  
  224. void CalcArea(int sideA, int sideB, int sideC, float perimeter, float& sumArea)
  225. {
  226. //Pre: Inputs three sides from main
  227. //Post: returns the area to the caller
  228. //Purpose: if gicen the sides of a triangle, it calculates the area and returns it
  229.  
  230. float total;
  231. float perHalf;
  232.  
  233. perHalf = perimeter / 2.0f;
  234.  
  235. total = sqrt(perHalf * (perHalf - (float)sideA) * (perHalf - (float)sideB) * (perHalf - (float)sideC));
  236.  
  237. sumArea = sumArea + total;
  238. }
  239.  
  240. void CalcPerimeter(int sideA, int sideB, int sideC, float& perimeter, float& sumPerimeter)
  241. {
  242. //Pre: Inputs parameters from main
  243. //Post: returns perimeter and sum to the caller
  244. //Purpose: If parameters are given, returns perimeter and sum
  245.  
  246. perimeter = (float)sideA + (float)sideB + (float)sideC;
  247.  
  248. sumPerimeter = perimeter + sumPerimeter;
  249. }
  250.  
  251. void GetStatistics(int triResult, int rightResult, int scalResult, int isoResult, int equResult, int& totalTri,
  252. int& totalRight, int& totalScal, int& totalIso, int& totalEqu)
  253. {
  254. // Pre: Parameters inputted from main
  255. // Post: Determines whether or not the sides form the different triangle types
  256. // Purpose: Determines if the sides form any triangle types and increment counters by one
  257.  
  258. //totalTri counter
  259. if (triResult == true)
  260. {
  261. totalTri++;
  262. }
  263.  
  264. //totalRight counter
  265. if (rightResult == true)
  266. {
  267. totalRight++;
  268. }
  269.  
  270. //totalScal counter
  271. if (scalResult == true)
  272. {
  273. totalScal++;
  274. }
  275.  
  276. //totalIso counter
  277. if (isoResult == true)
  278. {
  279. totalIso++;
  280. }
  281.  
  282. //totalEqu counter
  283. if (equResult == true)
  284. {
  285. totalEqu++;
  286. }
  287. }
  288.  
  289. void PrintHeading(ofstream& dout, string outputFileRes, int totalTri, int totalRight, int totalScal, int totalIso,
  290. int totalEqu, float areaAverage, float perAverage)
  291. {
  292. // Pre: Parameters inputted from main
  293. // Post: Results header printed
  294. // Purpose: Prints the results file heading
  295.  
  296. dout.open(outputFileRes);
  297.  
  298. //Prints heading
  299. dout << fixed << showpoint << setprecision(2);
  300. dout << "**********************************************************" << endl;
  301. dout << "*" << setw(57) << "*" << endl;
  302. dout << "*" << setw(38) << "Triangle Statistics" << setw(19) << "*" << endl;
  303. dout << "*" << setw(31) << "Output file name: " << outputFileRes << setw(15) << "*" << endl;
  304. dout << "*" << setw(57) << "*" << endl;
  305. dout << "**********************************************************" << endl;
  306.  
  307. //Calls PrintStatistics
  308. PrintStatistics(dout, outputFileRes, totalTri, totalRight, totalScal, totalIso, totalEqu, areaAverage, perAverage);
  309.  
  310. dout.close(); //Closes dout
  311. }
  312.  
  313. void PrintStatistics(ofstream& dout, string outputFileRes, int totalTri, int totalRight,
  314. int totalScal, int totalIso, int totalEqu, float areaAverage, float perAverage)
  315. {
  316. // Pre: Parameters inputted from main
  317. // Post: Triangle statistics are printed
  318. // Purpose: Prints triangle statistics to file
  319.  
  320. dout << endl;
  321. dout << "The file has the following statistics:" << endl;
  322. dout << setw(5) << totalTri << " total # of triangle(s)" << endl;
  323. dout << setw(5) << totalIso << " isosceles triangle(s)" << endl;
  324. dout << setw(5) << totalEqu << " equilateral triangle(s)" << endl;
  325. dout << setw(5) << totalScal << " scalene traingle(s)" << endl;
  326. dout << setw(5) << totalRight << " right triangle(s)" << endl;
  327. dout << setw(8) << areaAverage << " is the average area of all triangles" << endl;
  328. dout << setw(8) << perAverage << " is the average perimeter of all triangles" << endl;
  329. }
  330.  
  331. void PrintErrorHeading(ofstream& dout, string outputError)
  332. {
  333. // Pre: Parameters inputted from main
  334. // Post: Error log heading outputted
  335. // Purpose: Prints the error log heading
  336.  
  337. dout.open(outputError);
  338.  
  339. dout << fixed << showpoint << setprecision(2);
  340. dout << "**********************************************************" << endl;
  341. dout << "*" << setw(57) << "*" << endl;
  342. dout << "*" << setw(38) << "Triangle Statistics" << setw(19) << "*" << endl;
  343. dout << "*" << setw(30) << "Output file name: " << outputError << setw(18) << "*" << endl;
  344. dout << "*" << setw(57) << "*" << endl;
  345. dout << "**********************************************************" << endl;
  346. dout << endl;
  347. dout << "The following sides are invalid:" << setw(5) << endl;
  348.  
  349. dout.close();
  350. }
  351.  
  352. void PrintErrorLog(ofstream& dout, string outputError, int sideA, int sideB, int sideC, int& pass)
  353. {
  354. // Pre: Parameters inputted from main
  355. // Post: Sides that create errors are outputted to error.log
  356. // Purpose: Outputs sides that create errors to error log with the reason for the error
  357.  
  358. while (pass == 0)
  359. {
  360. PrintErrorHeading(dout, outputError);
  361. pass++;
  362. }
  363.  
  364. dout.open(outputError, ios::app); //Found on google, I couldn't get it to work without it.
  365.  
  366. dout << setw(5);
  367.  
  368. if (sideA < 0 || sideB < 0 || sideC < 0)
  369. {
  370. dout << sideA << setw(3) << sideB << setw(3) << sideC
  371. << setw(3) << " - side(s) can not be negative" << endl;
  372. }
  373. else if (sideA == 0 || sideB == 0 || sideC == 0)
  374. {
  375. dout << sideA << setw(3) << sideB << setw(3) << sideC
  376. << setw(3) << " - side(s) can not be zero" << endl;
  377. }
  378. else
  379. {
  380. dout << sideA << setw(3) << sideB << setw(3) << sideC
  381. << setw(3) << " - side(s) can not form triangle" << endl;
  382. }
  383.  
  384. dout.close();
  385. }
  386.  
  387. void CalcAverages(float sumArea, float sumPerimeter, int totalTri, float& areaAverage, float& perAverage)
  388. {
  389. // Pre: paramaters are inputted from main
  390. // Post: Calculates average area and perimeter
  391. // Purpose: Calculates average area and perimeter and returns results
  392.  
  393. areaAverage = sumArea / (float)totalTri;
  394.  
  395. perAverage = sumPerimeter / (float)totalTri;
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement