diff --git a/lectures/17_exceptions/README.md b/lectures/17_exceptions/README.md index 8215d77..bb61812 100644 --- a/lectures/17_exceptions/README.md +++ b/lectures/17_exceptions/README.md @@ -150,52 +150,68 @@ Here’s code using exceptions to sort a collection of lines by slope: #include #include #include -#include // for fabs() function +#include class Point { public: - Point(double x_, double y_) : x(x_),y(y_) {} - double x,y; + Point(double x_, double y_) : x(x_),y(y_) {} + double x,y; }; class Line { public: - Line(const Point &a_, const Point &b_) : a(a_),b(b_) {} - Point a,b; + Line(const Point &a_, const Point &b_) : a(a_),b(b_) {} + Point a,b; }; double compute_slope(const Point &a, const Point &b) { - double rise = b.y - a.y; - double run = b.x - a.x; - double epsilon = 0.00001; - if (fabs(run) < epsilon){ - throw -1; - } - return rise / run; + double rise = b.y - a.y; + double run = b.x - a.x; + double epsilon = 0.00001; + if (fabs(run) < epsilon){ + throw -1; + } + return rise / run; } double slope(const Line &ln) { - return compute_slope(ln.a,ln.b); + return compute_slope(ln.a,ln.b); } bool steeper_slope(const Line &m, const Line &n) { - double slope_m = slope(m); - double slope_n = slope(n); - return slope_m > slope_n; + double slope_m = slope(m); + double slope_n = slope(n); + return slope_m > slope_n; } void organize(std::vector &lines) { - std::sort(lines.begin(),lines.end(), steeper_slope); + std::sort(lines.begin(),lines.end(), steeper_slope); } int main () { - std::vector lines; - /* omitting code to initialize some data */ - try { - organize(lines); - /* omitting code to print out the results */ - } catch (int) { - std::cout << "error: infinite slope" << std::endl; - } + std::vector lines; + + // adding test lines + lines.push_back(Line(Point(0, 0), Point(1, 1))); // slope = 1 + lines.push_back(Line(Point(0, 0), Point(2, 3))); // slope = 1.5 + lines.push_back(Line(Point(0, 0), Point(5, 2))); // slope = 0.4 + // lines.push_back(Line(Point(3, 2), Point(3, 5))); // vertical line (should throw) + try { + organize(lines); + // Print the sorted lines based on steepness + std::cout << "Sorted lines by steepest slope:" << std::endl; + for (const Line &ln : lines) { + std::cout << "Line from (" << ln.a.x << ", " << ln.a.y << ") to (" + << ln.b.x << ", " << ln.b.y << ") - Slope: "; + try { + std::cout << slope(ln) << std::endl; + } catch (int) { + std::cout << "undefined (vertical line)" << std::endl; + } + } + } catch (int) { + std::cout << "error: infinite slope" << std::endl; + } + return 0; } ``` diff --git a/lectures/17_exceptions/slope.cpp b/lectures/17_exceptions/slope.cpp new file mode 100644 index 0000000..2ea0769 --- /dev/null +++ b/lectures/17_exceptions/slope.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +class Point { +public: + Point(double x_, double y_) : x(x_),y(y_) {} + double x,y; +}; +class Line { +public: + Line(const Point &a_, const Point &b_) : a(a_),b(b_) {} + Point a,b; +}; + +double compute_slope(const Point &a, const Point &b) { + double rise = b.y - a.y; + double run = b.x - a.x; + double epsilon = 0.00001; + if (fabs(run) < epsilon){ + throw -1; + } + return rise / run; +} + +double slope(const Line &ln) { + return compute_slope(ln.a,ln.b); +} + +bool steeper_slope(const Line &m, const Line &n) { + double slope_m = slope(m); + double slope_n = slope(n); + return slope_m > slope_n; +} + +void organize(std::vector &lines) { + std::sort(lines.begin(),lines.end(), steeper_slope); +} + +int main () { + std::vector lines; + + // adding test lines + lines.push_back(Line(Point(0, 0), Point(1, 1))); // slope = 1 + lines.push_back(Line(Point(0, 0), Point(2, 3))); // slope = 1.5 + lines.push_back(Line(Point(0, 0), Point(5, 2))); // slope = 0.4 + // lines.push_back(Line(Point(3, 2), Point(3, 5))); // vertical line (should throw) + try { + organize(lines); + // Print the sorted lines based on steepness + std::cout << "Sorted lines by steepest slope:" << std::endl; + for (const Line &ln : lines) { + std::cout << "Line from (" << ln.a.x << ", " << ln.a.y << ") to (" + << ln.b.x << ", " << ln.b.y << ") - Slope: "; + try { + std::cout << slope(ln) << std::endl; + } catch (int) { + std::cout << "undefined (vertical line)" << std::endl; + } + } + } catch (int) { + std::cout << "error: infinite slope" << std::endl; + } + return 0; +}