formatting

This commit is contained in:
Jidong Xiao
2023-11-20 16:42:20 -05:00
parent ec497c8eb9
commit 960874cc38

View File

@@ -156,41 +156,45 @@ Heres code using exceptions to sort a collection of lines by slope:
```cpp ```cpp
class Point { class Point {
public: public:
Point(double x_, double y_) : x(x_),y(y_) {} Point(double x_, double y_) : x(x_),y(y_) {}
double x,y; double x,y;
}; };
class Line { class Line {
public: public:
Line(const Point &a_, const Point &b_) : a(a_),b(b_) {} Line(const Point &a_, const Point &b_) : a(a_),b(b_) {}
Point a,b; Point a,b;
}; };
double compute_slope(const Point &a, const Point &b) throw(int) { double compute_slope(const Point &a, const Point &b) throw(int) {
double rise = b.y - a.y; double rise = b.y - a.y;
double run = b.x - a.x; double run = b.x - a.x;
double epsilon = 0.00001; double epsilon = 0.00001;
if (fabs(run) < epsilon) throw -1; if (fabs(run) < epsilon) throw -1;
return rise / run; return rise / run;
} }
double slope(const Line &ln) { 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) { bool steeper_slope(const Line &m, const Line &n) {
double slope_m = slope(m); double slope_m = slope(m);
double slope_n = slope(n); double slope_n = slope(n);
return slope_m > slope_n; return slope_m > slope_n;
} }
void organize(std::vector<Line> &lines) { void organize(std::vector<Line> &lines) {
std::sort(lines.begin(),lines.end(), steeper_slope); std::sort(lines.begin(),lines.end(), steeper_slope);
} }
int main () { int main () {
std::vector<Line> lines; std::vector<Line> lines;
/* omitting code to initialize some data */ /* omitting code to initialize some data */
try { try {
organize(lines); organize(lines);
/* omitting code to print out the results */ /* omitting code to print out the results */
} catch (int) { } catch (int) {
std::cout << "error: infinite slope" << std::endl; std::cout << "error: infinite slope" << std::endl;
} }
} }
``` ```
@@ -206,19 +210,20 @@ type from the exception class, and overwrite the what() member function
```cpp ```cpp
class myexception: public std::exception { class myexception: public std::exception {
virtual const char* what() const throw() { virtual const char* what() const throw() {
return "My exception happened"; return "My exception happened";
} }
}; };
int main () { int main () {
myexception myex; myexception myex;
try { try {
throw myex; throw myex;
} }
catch (std::exception& e) { catch (std::exception& e) {
std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;
} }
return 0; return 0;
} }
``` ```
- The STL library throws several different types of exceptions (all derived from the STL exception class): - The STL library throws several different types of exceptions (all derived from the STL exception class):
@@ -239,10 +244,10 @@ allocate sufficient memory resources for the object, the bad alloc exception is
```cpp ```cpp
try { try {
int* myarray= new int[1000]; int* myarray= new int[1000];
} }
catch (std::exception& e) { catch (std::exception& e) {
std::cout << "Standard exception: " << e.what() << std::endl; std::cout << "Standard exception: " << e.what() << std::endl;
} }
``` ```
- It can also be useful to have the constructor for a custom class throw a descriptive exception if the arguments - It can also be useful to have the constructor for a custom class throw a descriptive exception if the arguments