1/2

# 类与对象例子-长方形.doc

（1）

（2）

#include<iostream>
#include<cmath>
using namespace std;
class Rectangle{
private:
double x1,x2,x3,x4,y1,y2,y3,y4;
public:
Rectangle(double a,double b,double c,double d,double e,double f,double g,double h);
void show(){
cout<<"坐标是：("<<x1<<","<<y1<<")"<<" ("<<x2<<","<<y2<<")"
<<" ("<<x3<<","<<y3<<")"<<" ("<<x4<<","<<y4<<")" <<endl;
cout<<"该长方形的周长是："<<
2*sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))+2*sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4))<<endl;
cout<<"该长方形的面积是："<< sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) * sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4))<<endl;
}
};
Rectangle::Rectangle(double a,double b,double c,double d,double e,double f,double g,double h){
x1=a;x2=b;x3=c;x4=d;
y1=e;y2=f;y3=g;y4=h;
}
int main(){
double a,b,c,d,e,f,g,h,x11,x12,x13,x14,y11,y12,y13,y14;
cin>>a>>b>>c>>d>>e>>f>>g>>h; //输入8个数作为4个点的坐标
x11=a;x12=b;x13=c;x14=d; //换成xy好清楚地写以下公式
y11=e;y12=f;y13=g;y14=h;
double len1,len2,wid1,wid2,djiao1,djiao2; //为了判断四边形是否为矩形，x，y是坐标
len1=sqrt((x11-x12)*(x11-x12)+(y11-y12)*(y11-y12)); //计算

• 页数2
• 收藏数0 收藏
• 顶次数0
• 上传人phl19870121
• 文件大小61 KB
• 时间2021-04-17