#include <iostream>
#include <math.h>
using namespace std;
class Rectangle
{
public:
Rectangle(float *x,float *y)
{
setCoordinate(x,y);
}
void setCoordinate(float *x,float *y) //设置坐标
{
for(int i=0;i<4;i++)
{
if((x[i]<=0.0)||(x[i]>20.0)||(y[i]<=0.0)||(y[i]>20.0))
{
cout<<"有错误,请重新输入\n";
break;
}
else
X[i]=x[i];
Y[i]=y[i];
}
if(x[3]-x[2]==x[1]-x[0]&&y[3]-y[2]==y[1]-y[0])
{
if((x[1]-x[0])*(x[1]-x[0])+(y[1]-y[0])*(y[1]-y[0])+(x[3]-x[0])*(x[3]-x[0])+(y[3]-y[0])*(y[3]-y[0])==
(x[3]-x[1])*(x[3]-x[1])+(y[3]-y[1])*(y[3]-y[1]))
cout<<"该四边形是长方形\n";
}
else
cout<<"该四边形不是长方形\n";
}
//获得长
float getLength()
{
float length,temp1,temp2;
temp1=pow((Y[1]-Y[0])*(Y[1]-Y[0])+(X[1]-X[0])*(X[1]-X[0]),0.5);
temp2=pow((Y[2]-Y[1])*(Y[2]-Y[1])+(X[2]-X[1])*(X[2]-X[1]),0.5);
if(temp1>temp2)
length=temp1;
else
length=temp2;
return length;
}
//获得宽
float getWidth()
{
float width,temp1,temp2;
temp1=pow((Y[1]-Y[0])*(Y[1]-Y[0])+(X[1]-X[0])*(X[1]-X[0]),0.5);
temp2=pow((Y[2]-Y[1])*(Y[2]-Y[1])+(X[2]-X[1])*(X[2]-X[1]),0.5);
if(temp1>temp2)
width=temp2;
else
width=temp1;
return width;
}
//计算周长
float getPerimeter()
{
return (2*getLength()+2*getWidth());
}
//计算面积
float getArea()
{
return (getLength()*getWidth());
}
//判断是否是正方形
void guessSquare()
{
if((getWidth()==0)||(getLength()==0))
cout<<"无法判断\n";
else if(getWidth()==getLength())
{
cout<<"该图是正方形\n";
}
else
{
cout<<"该图不是正方形\n";
}
}
private:
float X[4],Y[4],*d;
};
int main()
{
int i;
float x[4],y[4];
float *p=x,*q=y; //指针指向数组
cout<<"从长方形左上角开始顺时针输入坐标(坐标在第一象限且小于等于20\n";
for (i=0;i<4;i++)
{
cin>>x[i];//输入数组
cin>>y[i];
}
Rectangle nb(p,q);//给类成员
cout<<"长为:"<<nb.getLength()<<endl;
cout<<"宽为:"<<nb.getWidth()<<endl;
nb.guessSquare();
return 0;
}
运行结果:
是长方形的结果:
不是长方形的结果:
代码:
#include <iostream>
#include <math.h>
using namespace std;
class Rectangle
{
public:
Rectangle(double* x, double* y)
{
width = 0;
length = 0;
setCoordinate(x, y);
}
void setCoordinate(double* x, double* y) //设置坐标
{
for (int i = 0; i < 4; i++)
{
if ((x[i] <= 0.0) || (x[i] > 20.0) || (y[i] <= 0.0) || (y[i] > 20.0))
{
cout << "有错误,请重新输入\n";
break;
}
else
X[i] = x[i];
Y[i] = y[i];
}
//左顶点x[0],y[0],右顶点x[1],y[1] , 0 1 2节点构成直角三角形
double length1 = (x[1] - x[0]) * (x[1] - x[0]) + (y[1]-y[0]) * (y[1]-y[0]);
double width1 = (x[1] - x[2]) * (x[1] - x[2]) + (y[1] - y[2]) * (y[1] - y[2]);
double duijiaoxian = (x[2] - x[0]) * (x[2] - x[0]) + (y[2] - y[0]) * (y[2] - y[0]);
//0 2 3节点构成直角三角形
double width2 = (x[0] - x[3]) * (x[0] - x[3]) + (y[0] - y[3]) * (y[0] - y[3]);
double length2 = (x[2] - x[3]) * (x[2] - x[3]) + (y[2] - y[3]) * (y[2] - y[3]);
if (length1 == length2 && width1 == width2 && length1 + width1 == duijiaoxian)
{
isRectangle = 1;
if (length1 > width1)
{
length = sqrt(length1);
width = sqrt(width1);
}
else
{
length = sqrt(width1);
width = sqrt(length1);
}
cout << "该四边形是长方形\n";
}
else
{
isRectangle = 0;
cout << "该四边形不是长方形\n";
}
}
//获得长
double getLength()
{
return length;
}
//获得宽
double getWidth()
{
return width;
}
//计算周长
double getPerimeter()
{
return (2 * getLength() + 2 * getWidth());
}
//计算面积
double getArea()
{
return (getLength() * getWidth());
}
//判断是否是正方形
void guessSquare()
{
if ( width == 0 || length == 0)
cout << "无法判断\n";
else if ( length == width)
{
cout << "该图是正方形\n";
}
else
{
cout << "该图不是正方形\n";
}
}
private:
double X[4], Y[4], width,length;
int isRectangle; //是否是长方形标记
};
int main()
{
int i;
double x[4], y[4];
double* p = x, * q = y; //指针指向数组
cout << "从长方形左上角开始顺时针输入坐标(坐标在第一象限且小于等于20\n";
for (i = 0; i < 4; i++)
{
cin >> x[i];//输入数组
cin >> y[i];
}
Rectangle nb(p, q);//给类成员
cout << "长为:" << nb.getLength() << endl;
cout << "宽为:" << nb.getWidth() << endl;
nb.guessSquare();
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!