Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream> // to use cin and cout
- #include <typeinfo> // to be able to use operator typeid
- #include <fstream>
- #include <cmath>
- #include <iomanip>
- #include <string>
- // Include here the libraries that your program needs to compile
- using namespace std;
- // Ignore this; it's a little function used for making tests
- inline void test(const char* expression, const char* file, int line)
- {
- cerr << "test(" << expression << ") failed in file " << file;
- cerr << ", line " << line << "." << endl << endl;
- }
- // This goes along with the above function...don't worry about it
- #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : test(#EXPRESSION, __FILE__, __LINE__))
- // Insert here the prototypes of the functions
- double round_off(double value, int places);
- double square(double x);
- double calcLength(double x1, double x2, double y1, double y2);
- double semiPerimeter(double ab, double bc, double ca);
- double calcArea(double ab, double bc, double ca);
- void getPoint(ifstream & iFile, double& q, double& w);
- void printDistance(ostream & oFile, double& e, double& r, double& t, double& y, double& z);
- int main()
- {
- ifstream inFile;
- ofstream outFile;
- double value;
- double num1 = 0., num2 = 0., num3 = 0., num4 = 0., num5 = 0., num6 = 0.;
- inFile.open("inputhw2.txt", ios::in);
- if (!inFile)
- {
- cout << "Input file not found!" << endl;
- system("pause");
- return 1;
- }
- outFile.open("outputhw2.txt", ios::out);
- if (!outFile)
- {
- cout << "Output file not found " << endl;
- system("pause");
- return 1;
- }
- getPoint(inFile, num1, num2);
- getPoint(inFile, num3, num4);
- getPoint(inFile, num5, num6);
- printDistance(outFile, num1, num2, num3, num4, value);
- outFile << calcLength(num2, num4, num1, num3);
- printDistance(outFile, num3, num4, num5, num6, value);
- outFile << calcLength(num3, num5, num4, num6);
- printDistance(outFile, num1, num2, num5, num6, value);
- outFile << calcLength(num1, num5, num2, num6);
- value = calcArea(num1, num2, num3);
- system("pause");
- // Do NOT remove or modify the following statements
- cout << endl << "Testing your solution" << endl << endl;
- test(fabs(round_off(calcLength(1.0, 1.2, 6.0, 6.1), 2) - 7.00) < .001); // Incorrect calculation of length
- test(fabs(round_off(calcLength(6.0, 6.1, 3.2, 6.5), 2) - 2.83) < .001); // Incorrect calculation of length
- test(fabs(round_off(calcLength(1.0, 1.2, 3.2, 6.5), 2) - 5.74) < .001); // Incorrect calculation of length
- 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
- test(fabs(round_off(calcLength(1.2, 1.2, 7.6, 4.3), 2) - 7.11) < .001); // Incorrect calculation of length
- test(fabs(round_off(calcLength(7.6, 4.3, 9.2, 3.4), 2) - 1.84) < .001); // Incorrect calculation of length
- test(fabs(round_off(calcLength(1.2, 1.2, 9.2, 3.4), 2) - 8.30) < .001); // Incorrect calculation of length
- 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
- test(fabs(round_off(calcLength(1.0, 1.0, 5.0, 5.0), 2) - 5.66) < .001); // Incorrect calculation of length
- test(fabs(round_off(calcLength(5.0, 5.0, 9.0, 9.0), 2) - 5.66) < .001); // Incorrect calculation of length
- test(fabs(round_off(calcLength(1.0, 1.0, 9.0, 9.0), 2) - 11.31) < .001); // Incorrect calculation of length
- 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
- system("pause");
- return 0;
- }
- //******** Function definition *********
- // Read the handout carefully for detailed description of the functions that you have to implement
- void getPoint(ifstream & iFile, double& q, double& w)
- {
- iFile >> q >> w;
- round_off(q, 1);
- round_off(w, 1);
- }
- void printDistance(ostream & oFile, double& e, double& r, double& t, double& y, double& z)
- {
- oFile << "The distance between " << e << "," << r;
- }
- double round_off(double value, int places)
- {
- return floor((value * pow(10, places)) + .5) / pow(10, places);
- }
- double square(double x)
- {
- return (x * x);
- }
- double calcLength(double x1, double x2, double y1, double y2)
- {
- double r1, r2, sqr1, sqr2, add, result;
- r1 = (x1 - x2);
- r2 = (y1 - y2);
- sqr1 = square(r1);
- sqr2 = square(r2);
- add = (sqr1 + sqr2);
- result = sqrt(add);
- return round_off(result, 10);
- }
- double semiPerimeter(double ab, double bc, double ca)
- {
- double resultsp;
- resultsp = (1 / 2) * (ab + bc + ca);
- round_off(resultsp, 10);
- return resultsp;
- }
- double calcArea(double ab, double bc, double ca)
- {
- double area, l1, l2, l3;
- l1 = semiPerimeter(ab, bc, ca);
- l2 = semiPerimeter(ab, bc, ca);
- l3 = semiPerimeter(ab, bc, ca);
- area = semiPerimeter(ab, bc, ca) * l1 * l2 * l3;
- area = sqrt(area);
- return round_off(area, 2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement