Describe
Given the two-dimensional geometry of triangles, rectangles, and circles, sort from large to small in terms of area.
The code in the main function is given, please fill it out, and do not include the code that you have given when committing.
int main () {vector<cshape*> VEC;//In order to use polymorphism, use the pointer array string name;int num[3]= {}; Used to store 3 shapes of Idwhile (cin>>name) {if (name== "rectangle") {CPoint P1, P2;cin>>p1>>p2;vec.push_back ( New Crectangle (name, ++num[0], p1, p2));} else if (name== "triangle") {CPoint P1, p2, P3;cin>>p1>>p2>>p3;vec.push_back (new Ctriangle (name, + +) Num[1], p1, p2, p3);} Else{cpoint center;double R;cin>>center>>r;vec.push_back (New ccircle (name, ++num[2], center, R));}} Sort (Vec.begin (), Vec.end (), CMP), for (int i=0;i<vec.size (); i++) {cout<<vec[i]->getname () << "" < <setiosflags (ios::fixed) <<setprecision (3) <<vec[i]->area () <<endl;delete vec[i];} return 0;}
Input
The input data includes multiple lines, one geometry per behavior (no more than 100 geometries). The input formats for the various geometries are as follows:
Triangles (X1,y1,x2,y2,x3,y3 are triangle vertex coordinates, respectively):
Triangle x1 y1 x2 y2 x3 y3
Rectangle (X1,y1,x2,y2 is the end of a diagonal of a rectangle, and the edge of the rectangle is parallel to the axis)
Rectangle x1 Y1 X2 y2
Circle (X1,y1 is the center coordinate, R is the radius)
Circle X1 Y1 R
Output
The individual figures are numbered, the triangles are named Triangle1, Triangle2 ..., the rectangles are named Rectangle1, Rectangle2 ..., the circle is named Circle1, Circle2 ...
Then sort by area from large to small, and if the area is the same, sort by name in dictionary order. Each row outputs a shape's name and area, separated by a space, and the area retains 3 decimal places.
Sample input
Rectangle 0.0 0.0 1.0 2.0
Circle 0.0 0.0 1.0
Triangle 0.0 0.0 1.0 1.0 1.0 0.0
Rectangle 0.0 0.0 1.0 1.0
Circle 0.0 0.0 2.0
Sample output
Circle2 12.566
Circle1 3.142
Rectangle1 2.000
Rectangle2 1.000
Triangle1 0.500
Exercises
Since the calculation of the Helen formula will have errors, we use the second-order determinant to calculate the area of the triangle:
Set A (X1,y1), B (X2,y2), C (X3,Y3) in the coordinate system in order three points counter-clockwise
s=1/2[(x1y2-x2y1) + (x2y3-x3y2) + (X3Y1-X1Y3)]
PS: It is better to add an absolute value to the area, sometimes negative
#include <bits/stdc++.h>#definePi ACOs (-1)using namespacestd;stringChstringSintx) {StringStream ss; SS<<s<<x; SS>>s; returns;}classcpoint{ Public: Doublex, y; CPoint (Doublex=0,Doubley=0): X (x), Y (y) {} friend IStream&operator>> (IStream & is,cpoint&p) { is>>p.x>>p.y; return is; }};classcshape{ Public: Doublem; stringname; CShape (stringNameDoublem): M (m), name (name) {}DoubleArea () {returnm;} stringGetName () {returnname;};intCMP (CShape *a,cshape *b) { if(a->m>b->m)return 1; if(a->m==b->m) {if(a->name<b->name)return 1; Else return 0; } return 0;}classCtriangle: Publiccshape{Private: CPoint p1, p2, p3; intnum; Public: Ctriangle (stringNameintnum,cpoint p1,cpoint p2,cpoint p3):p 1 (p1), p2 (p2), p3 (P3), CShape (CH (name,num), Fabs (0.5* ((P1.X*P2.Y-P2.X*P1.Y) + (P2.X*P3.Y-P3.X*P2.Y) + (P3.X*P1.Y-P1.X*P3.Y))), num (+ +)num) {}};classCrectangle: Publiccshape{Private: CPoint p1, p2; Public: intnum; Crectangle (stringNameintnum,cpoint p1,cpoint p2): CShape (CH (name,num), Fabs ((p1.x-p2.x) (P1.Y-P2.Y)), num (+ +)num), p1 (p1), p2 (p2) {}};classCcircle: Publiccshape{Private: CPoint p; intnum; DoubleR; Public: Ccircle (stringNameintNum,cpoint p,Doubler): CShape (CH (name,num), Pi*r*r), num (+ +num), p (p), R (r) {}};intmain () {vector<CShape*> VEC;//to use polymorphism, use a pointer array stringname; intnum[3]= {};//ID used to store 3 types of shapes while(cin>>name) { if(name=="Rectangle") {CPoint P1, p2; CIN>>p1>>P2; Vec.push_back (NewCrectangle (Name, ++num[0], p1, p2); } Else if(name=="Triangle") {CPoint P1, p2, p3; CIN>>p1>>p2>>P3; Vec.push_back (NewCtriangle (Name, ++num[1], p1, p2, p3); } Else{CPoint Center; DoubleR; CIN>>center>>R; Vec.push_back (NewCcircle (Name, ++num[2], center, R); }} sort (Vec.begin (), Vec.end (), CMP); for(intI=0; I<vec.size (); i++) {cout<<vec[i]->getname () <<" "<<setiosflags (iOS::fixed) <<setprecision (3) <<vec[i]->area () <<Endl; DeleteVec[i]; } return 0;}
"TOJ 2034" C + + Experiment: Area sorting (known as three points, using second-order determinant to find triangular area)