Advertisement
Sirallens

Untitled

Oct 14th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.94 KB | None | 0 0
  1. #include <iostream>             // to use cin and cout
  2. #include <typeinfo>             // to be able to use operator typeid
  3. #include <fstream>
  4. #include <cmath>
  5. #include <iomanip>
  6. #include <string>
  7.  
  8. // Include here the libraries that your program needs to compile
  9.  
  10.  
  11. using  namespace  std;
  12.  
  13. // Ignore this; it's a little function used for making tests
  14. inline void test(const char* expression, const char* file, int line)
  15. {
  16.     cerr << "test(" << expression << ") failed in file " << file;
  17.     cerr << ", line " << line << "." << endl << endl;
  18. }
  19. // This goes along with the above function...don't worry about it
  20. #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : test(#EXPRESSION, __FILE__, __LINE__))
  21.  
  22. // Insert here the prototypes of the functions
  23. double round_off(double value, int places);
  24. double square(double x);
  25. double calcLength(double x1, double x2, double y1, double y2);
  26. double semiPerimeter(double ab, double bc, double ca);
  27. double calcArea(double ab, double bc, double ca);
  28. void getPoint(ifstream & iFile, double& q, double& w);
  29. void printDistance(ostream & oFile, double& e, double& r, double& t, double& y, double& z);
  30. int main()
  31. {
  32.     ifstream inFile;
  33.     ofstream outFile;
  34.     double value;
  35.     double num1 = 0., num2 = 0., num3 = 0., num4 = 0., num5 = 0., num6 = 0.;
  36.  
  37.     inFile.open("inputhw2.txt", ios::in);
  38.     if (!inFile)
  39.     {
  40.         cout << "Input file not found!" << endl;
  41.         system("pause");
  42.         return 1;
  43.     }
  44.  
  45.  
  46.  
  47.     outFile.open("outputhw2.txt", ios::out);
  48.     if (!outFile)
  49.     {
  50.         cout << "Output file not found " << endl;
  51.         system("pause");
  52.         return 1;
  53.     }
  54.  
  55.     getPoint(inFile, num1, num2);
  56.     getPoint(inFile, num3, num4);
  57.     getPoint(inFile, num5, num6);
  58.  
  59.  
  60.     printDistance(outFile, num1, num2, num3, num4, value);
  61.     outFile << calcLength(num2, num4, num1, num3);
  62.  
  63.     printDistance(outFile, num3, num4, num5, num6, value);
  64.     outFile << calcLength(num3, num5, num4, num6);
  65.  
  66.     printDistance(outFile, num1, num2, num5, num6, value);
  67.     outFile << calcLength(num1, num5, num2, num6);
  68.  
  69.     value = calcArea(num1, num2, num3);
  70.  
  71.  
  72.  
  73.     system("pause");
  74.  
  75.     // Do NOT remove or modify the following statements
  76.     cout << endl << "Testing your solution" << endl << endl;
  77.  
  78.  
  79.     test(fabs(round_off(calcLength(1.0, 1.2, 6.0, 6.1), 2) - 7.00) < .001);         // Incorrect calculation of length
  80.     test(fabs(round_off(calcLength(6.0, 6.1, 3.2, 6.5), 2) - 2.83) < .001);         // Incorrect calculation of length
  81.     test(fabs(round_off(calcLength(1.0, 1.2, 3.2, 6.5), 2) - 5.74) < .001);         // Incorrect calculation of length
  82.     test(fabs(calcArea(calcLength(1.0, 1.2, 6.0, 6.1), calcLength(6.0, 6.1, 3.2, 6.5), calcLength(1.0, 1.2, 3.2, 6.5)) - 7.86) < .001);         // Incorrect calculation of area
  83.  
  84.     test(fabs(round_off(calcLength(1.2, 1.2, 7.6, 4.3), 2) - 7.11) < .001);         // Incorrect calculation of length
  85.     test(fabs(round_off(calcLength(7.6, 4.3, 9.2, 3.4), 2) - 1.84) < .001);         // Incorrect calculation of length
  86.     test(fabs(round_off(calcLength(1.2, 1.2, 9.2, 3.4), 2) - 8.30) < .001);         // Incorrect calculation of length
  87.     test(fabs(calcArea(calcLength(1.2, 1.2, 7.6, 4.3), calcLength(7.6, 4.3, 9.2, 3.4), calcLength(1.2, 1.2, 9.2, 3.4)) - 5.36) < .001);         // Incorrect calculation of area
  88.  
  89.     test(fabs(round_off(calcLength(1.0, 1.0, 5.0, 5.0), 2) - 5.66) < .001);         // Incorrect calculation of length
  90.     test(fabs(round_off(calcLength(5.0, 5.0, 9.0, 9.0), 2) - 5.66) < .001);         // Incorrect calculation of length
  91.     test(fabs(round_off(calcLength(1.0, 1.0, 9.0, 9.0), 2) - 11.31) < .001);            // Incorrect calculation of length
  92.     test(fabs(calcArea(calcLength(1.0, 1.0, 5.0, 5.0), calcLength(5.0, 5.0, 9.0, 9.0), calcLength(1.0, 1.0, 9.0, 9.0)) - 0.00) < .001);         // Incorrect calculation of area
  93.  
  94.     system("pause");
  95.     return 0;
  96. }
  97.  
  98. //********  Function definition  *********
  99. // Read the handout carefully for detailed description of the functions that you have to implement
  100. void getPoint(ifstream & iFile, double& q, double& w)
  101. {
  102.     iFile >> q >> w;
  103.     round_off(q, 1);
  104.     round_off(w, 1);
  105.  
  106. }
  107.  
  108. void printDistance(ostream & oFile, double& e, double& r, double& t, double& y, double& z)
  109. {
  110.     oFile << "The distance between " << e << "," << r;
  111.  
  112.  
  113. }
  114.  
  115.  
  116. double round_off(double value, int places)
  117. {
  118.  
  119.     return floor((value * pow(10, places)) + .5) / pow(10, places);
  120. }
  121.  
  122. double square(double x)
  123. {
  124.     return (x * x);
  125. }
  126.  
  127. double calcLength(double x1, double x2, double y1, double y2)
  128. {
  129.     double r1, r2, sqr1, sqr2, add, result;
  130.  
  131.     r1 = (x1 - x2);
  132.     r2 = (y1 - y2);
  133.     sqr1 = square(r1);
  134.     sqr2 = square(r2);
  135.     add = (sqr1 + sqr2);
  136.     result = sqrt(add);
  137.     return round_off(result, 10);
  138. }
  139.  
  140. double semiPerimeter(double ab, double bc, double ca)
  141. {
  142.     double resultsp;
  143.     resultsp = (1 / 2) * (ab + bc + ca);
  144.     round_off(resultsp, 10);
  145.     return resultsp;
  146.  
  147. }
  148.  
  149. double calcArea(double ab, double bc, double ca)
  150. {
  151.     double area, l1, l2, l3;
  152.     l1 = semiPerimeter(ab, bc, ca);
  153.     l2 = semiPerimeter(ab, bc, ca);
  154.     l3 = semiPerimeter(ab, bc, ca);
  155.     area = semiPerimeter(ab, bc, ca) * l1 * l2 * l3;
  156.  
  157.     area = sqrt(area);
  158.     return round_off(area, 2);
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement