Advertisement
gha890826

0521-1 三角形類別的異常處理

May 21st, 2021
1,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. /* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */  
  2.  
  3. #include <iostream>    
  4. #include<iomanip>    
  5. #include <cmath>  
  6. #include<string>    
  7. using namespace std;    
  8. const double TOL = 1e-10;  
  9. class ERR{    //異常處理
  10.   public:    
  11.     string msg(){    
  12.         return "retry";    
  13.     }    
  14. };    
  15. class Point{    //xy座標類別
  16.   private:    
  17.     double x, y;    
  18.   public:    
  19.     Point(double a = 0., double b = 0.):x(a), y(b){}    
  20.     double getx() {return x;}    
  21.     double gety() {return y;}    
  22.     friend istream& operator>> (istream& in, Point& pt);    
  23.        
  24. };    
  25.    
  26. istream& operator>> (istream& in , Point& pt){    
  27.     return in >> pt.x >> pt.y;    
  28. }    
  29.    
  30.    
  31. class Triangle{    //三角形
  32.   private:    
  33.     Point pt1, pt2, pt3;    
  34.   public:    
  35.     Triangle(Point& p, Point& q, Point& r) throw(ERR)    
  36.         :pt1(p), pt2(q), pt3(r){    
  37.         if(area() < TOL) throw ERR();    
  38.     }    
  39.     double area();  //待完成
  40.     double perimeter();  //待完成
  41.     friend ostream& operator<< (ostream& out, Triangle& foo);  //待完成
  42. };    
  43.    
  44. int main(){    
  45.     Point a, b, c;    
  46.     while(1){    
  47.         try{    
  48.             cin >> a >> b >> c;    
  49.             Triangle foo(a,b,c);    
  50.             cout << foo << endl;    
  51.             break;    
  52.         }    
  53.         catch (ERR& err){    
  54.             cout << err.msg() << endl;    
  55.         }    
  56.     }    
  57.     return 0;    
  58. }  
  59.  
  60. /* PRESET CODE END - NEVER TOUCH CODE ABOVE*/  
  61. double distance(double x1,double y1,double x2,double y2)
  62. {
  63.     double distance=sqrt(pow(x2-x1,2)+pow(y2-y1,2));
  64.     return distance;
  65. }
  66.  
  67. ostream& operator<< (ostream& out, Triangle& foo)
  68. {
  69.     cout<<fixed<<setprecision(2)<<"area = "<<foo.area()<<endl<<"perimeter = "<<foo.perimeter();
  70. }
  71.  
  72. double Triangle::area()
  73. {
  74.     double a=distance(this->pt1.getx(),this->pt1.gety(),this->pt2.getx(),this->pt2.gety());
  75.     double b=distance(this->pt1.getx(),this->pt1.gety(),this->pt3.getx(),this->pt3.gety());
  76.     double c=distance(this->pt3.getx(),this->pt3.gety(),this->pt2.getx(),this->pt2.gety());
  77.     double s= (a+b+c)/2;
  78.     return sqrt(s*(s-a)*(s-b)*(s-c));
  79. }
  80.  
  81. double Triangle::perimeter()
  82. {
  83.     double a=distance(this->pt1.getx(),this->pt1.gety(),this->pt2.getx(),this->pt2.gety());
  84.     double b=distance(this->pt1.getx(),this->pt1.gety(),this->pt3.getx(),this->pt3.gety());
  85.     double c=distance(this->pt3.getx(),this->pt3.gety(),this->pt2.getx(),this->pt2.gety());
  86.     return a+b+c;
  87. }
  88.  
  89. /*
  90. 輸入說明:
  91.          輸入三個點的x,y座標,x和y之間用空格隔開,每個點之間用空格隔開。
  92.  
  93. 輸出說明:
  94.          若三個點無法構成確切的三角形,則輸出 ”retry” (retry輸出後須換行),
  95.          並重新輸入三個點直到可以構成三角形。可構成三角形後輸出該三角形的面積與周長
  96.  
  97. 註:輸出結果用四捨五入取小數點第二位
  98. SAMPLE INPUT
  99.           1 1 2 2 3 3↵
  100.           1 1 1 2 2 2↵
  101. SAMPLE OUTPUT
  102.           retry↵
  103.           area = 0.50↵
  104.           perimeter = 3.41↵
  105. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement