Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
- #include <iostream>
- #include<iomanip>
- #include <cmath>
- #include<string>
- using namespace std;
- const double TOL = 1e-10;
- class ERR{ //異常處理
- public:
- string msg(){
- return "retry";
- }
- };
- class Point{ //xy座標類別
- private:
- double x, y;
- public:
- Point(double a = 0., double b = 0.):x(a), y(b){}
- double getx() {return x;}
- double gety() {return y;}
- friend istream& operator>> (istream& in, Point& pt);
- };
- istream& operator>> (istream& in , Point& pt){
- return in >> pt.x >> pt.y;
- }
- class Triangle{ //三角形
- private:
- Point pt1, pt2, pt3;
- public:
- Triangle(Point& p, Point& q, Point& r) throw(ERR)
- :pt1(p), pt2(q), pt3(r){
- if(area() < TOL) throw ERR();
- }
- double area(); //待完成
- double perimeter(); //待完成
- friend ostream& operator<< (ostream& out, Triangle& foo); //待完成
- };
- int main(){
- Point a, b, c;
- while(1){
- try{
- cin >> a >> b >> c;
- Triangle foo(a,b,c);
- cout << foo << endl;
- break;
- }
- catch (ERR& err){
- cout << err.msg() << endl;
- }
- }
- return 0;
- }
- /* PRESET CODE END - NEVER TOUCH CODE ABOVE*/
- double distance(double x1,double y1,double x2,double y2)
- {
- double distance=sqrt(pow(x2-x1,2)+pow(y2-y1,2));
- return distance;
- }
- ostream& operator<< (ostream& out, Triangle& foo)
- {
- cout<<fixed<<setprecision(2)<<"area = "<<foo.area()<<endl<<"perimeter = "<<foo.perimeter();
- }
- double Triangle::area()
- {
- double a=distance(this->pt1.getx(),this->pt1.gety(),this->pt2.getx(),this->pt2.gety());
- double b=distance(this->pt1.getx(),this->pt1.gety(),this->pt3.getx(),this->pt3.gety());
- double c=distance(this->pt3.getx(),this->pt3.gety(),this->pt2.getx(),this->pt2.gety());
- double s= (a+b+c)/2;
- return sqrt(s*(s-a)*(s-b)*(s-c));
- }
- double Triangle::perimeter()
- {
- double a=distance(this->pt1.getx(),this->pt1.gety(),this->pt2.getx(),this->pt2.gety());
- double b=distance(this->pt1.getx(),this->pt1.gety(),this->pt3.getx(),this->pt3.gety());
- double c=distance(this->pt3.getx(),this->pt3.gety(),this->pt2.getx(),this->pt2.gety());
- return a+b+c;
- }
- /*
- 輸入說明:
- 輸入三個點的x,y座標,x和y之間用空格隔開,每個點之間用空格隔開。
- 輸出說明:
- 若三個點無法構成確切的三角形,則輸出 ”retry” (retry輸出後須換行),
- 並重新輸入三個點直到可以構成三角形。可構成三角形後輸出該三角形的面積與周長
- 註:輸出結果用四捨五入取小數點第二位
- SAMPLE INPUT
- 1 1 2 2 3 3↵
- 1 1 1 2 2 2↵
- SAMPLE OUTPUT
- retry↵
- area = 0.50↵
- perimeter = 3.41↵
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement