// Zeinalipour-Yazti Demetrios // csyiazti // cs10xx - // Lab Sec#24 // lab9 /* Triangle Calculations WITH a highlight on call-by-reference & call-by-value * This program finds the missing information of a right triangle many times. * A) Input: length of side X, angle A1 * B) Restrictions: inputs must be positive * C) Output: side Y, hypotenuse Z, Angle A2. * D) Pythagorean Theorem.==> * Z^2 = X^2 + Y^2 * */ #include #include #include #include using namespace std; #define PI 3.14159265 void readData(double &a1, double &x) { string filename; while (true) { /* get the filename */ cout << "Give me the name of the file: " ; cin >> filename; cout << "\nOpening \'" << filename << "\'..."; /* open file for input */ ifstream infilestream; infilestream.open(filename.c_str(), ios::in); if (infilestream) /* overload ! operator*/ { cout << "done\n"; infilestream >> x >> a1; break; } else { cout << "failed\n"; cerr << "File could not be opened\n" << endl; infilestream.close(); } } } void readData2(double *a1, double *x) { ifstream infilestream; string filename; while (true) { /* get the filename */ cout << "Give me the name of the file: " ; cin >> filename; cout << "\nOpening " << filename << "..."; /* open file for input */ ifstream infilestream; infilestream.open(filename.c_str(), ios::in); if (infilestream) /* overload ! operator*/ { cout << "done\n"; infilestream >> *x >> *a1; break; } else { cout << "failed\n"; cerr << "File could not be opened\n" << endl; infilestream.close(); } } } void calculateAll(double a1, double &a2, double x, double &y, double &z) { a2 = 90 - a1; /* calculates the other angle */ y = tan(a1 * PI / 180) * x; /* the length of y */ z = sqrt(x*x + y*y); /* the length of the hypotenuse with the Pythagorean Theorem. */ } /* This procedure outputs graphically the triangle. */ void printTriangle(const double &a1, const double &a2, const double &x, const double &y, const double &z) { cout << "\n\nPrinting the right triangle" << "\n" << "================================" << "\n\n" << "a1=" << a1 << "\n" << "|\\" << '\n' << "| \\" << '\n' << "| \\" << '\n' << "| \\" << '\n' << "| \\" << '\n' << "| \\" << '\n' << "| \\" << '\n' << "|x=" << x << " \\ " << "z=" << z << '\n' << "| \\" << '\n' << "| \\" << '\n' << "| \\" << '\n' << "| \\" << '\n' << "|__90 a2=" << a2 << " \\" << '\n' << "|__|__________\\" << '\n' << " y=" << y << endl; } void main() { /* define variables */ double x; /* The length of side x */ double y; /* The length of side y */ double z; /* The length of side z */ double a1; /* The angle a1 */ double a2; /* The angle a2 */ /* define functions & procedures*/ /* TWO ways to achieve the same task, reading parameters */ void readData(double &a1, double &x); void readData2(double *a1, double *x); /* 'a1' and 'x' are passed as const because we don't want to change their value. */ void calculateAll(const double a1, double &a2, const double x, double &y, double &z); /* we are passing all parameters by reference in order to avoid unecessary data copying */ void printTriangle(const double &a1, const double &a2, const double &x, const double &y, const double &z); /* print intro */ cout << "Lab 9 - Right Triangle Calculation with call-by-refernce & call-by-value.\n" << "by Demetris Zeinalipour : cs10 #24.\n\n" << "NOTE:Download \'triangle.txt\' and place it to same directory as this executable!!\n\n"; /* get Values from user */ readData(a1, x); //readData2(&a1, &x); /* Calculating all the missing values */ calculateAll(a1, a2, x, y, z); /* Printing out the triangle (graphic) */ printTriangle(a1, a2, x, y, z); }