提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、题目
二、代码
8
代码如下:
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
int x,y;
public:
Point(int a=0,int b=0)
{
x=a;
y=b;
}
void Getdata(int&c,int&d)
{
c=x;
d=y;
}
void getx(int x)
{
Point::x=x;
}
void gety(int y)
{
this->y=y;
}
int Getx()
{
return x;
}
int Gety()
{
return y;
}
};
class Triangle
{
Point p[3];
public:
void setone(int x,int y)
{
p[0].getx(x);
p[0].gety(y);
}
void setsecond(int x,int y)
{
p[1].getx(x);
p[1].gety(y);
}
void setthird(int x,int y)
{
p[2].getx(x);
p[2].gety(y);
}
Triangle(int x1,int y1,int x2,int y2,int x3,int y3)
{
setone(x1,y1);
setsecond(x2,y2);
setthird(x3,y3);
}
double distance(int i,int j)
{
return sqrt((p[i-1].Getx()-p[j-1].Getx())*(p[i-1].Getx()-p[j-1].Getx())+(p[i-1].Gety()-p[j-1].Gety())*(p[i-1].Gety()-p[j-1].Gety()));
}
double zc()
{
double r,s,t;
r=distance(1,2);
s=distance(1,3);
t=distance(2,3);
return (r+s+t);
}
double S()
{
double r,s,t,c;
r=distance(1,2);
s=distance(1,3);
t=distance(2,3);
c=zc()/2;
return (sqrt(c*(c-r)*(c-s)*(c-t)));
}
void show()
{
cout<<"第一个点的坐标:("<<p[0].Getx()<<","<<p[0].Gety()<<")"<<endl;
cout<<"第二个点的坐标:("<<p[1].Getx()<<","<<p[1].Gety()<<")"<<endl;
cout<<"第三个点的坐标:("<<p[2].Getx()<<","<<p[2].Gety()<<")"<<endl;
cout<<"第一条边的长度:"<<distance(1,2)<<endl;
cout<<"第二条边的长度:"<<distance(1,3)<<endl;
cout<<"第三条边的长度:"<<distance(2,3)<<endl;
cout<<"该三角形的周长为:"<<zc()<<endl;
cout<<"该三角形的面积为:"<<S()<<endl;
}
void move(int xoff,int yoff)
{
p[0].getx(p[0].Getx()+xoff);
p[0].gety(p[0].Gety()+yoff);
p[1].getx(p[1].Getx()+xoff);
p[1].gety(p[1].Gety()+yoff);
p[2].getx(p[2].Getx()+xoff);
p[2].gety(p[2].Gety()+yoff);
}
};
int main()
{
Triangle T(0,0,1,1,3,6);
T.show();
T.move(3,-1);
T.show();
return 0;
}
9
代码如下:
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
int x,y;
public:
Point(int a=0,int b=0)
{
x=a;
y=b;
}
Point(Point &p)
{
x=p.x;
y=p.y;
}
void setdata(int c,int d)
{
x=c;
y=d;
}
void get(int*p1,int*p2)
{
*p1=x;
*p2=y;
}
};
class Triangle
{
Point p1,p2,p3;
public:
Triangle(int x1=0,int y1=0,int x2=0,int y2=0,int x3=0,int y3=0):p1(x1,y1),p2(x2,y2),p3(x3,y3){}
Triangle(Point &p6,Point &p4,Point &p5)
{
p1=p6;
p2=p4;
p3=p5;
}
void set1(int z1,int z2)
{
p1.setdata(z1,z2);
}
void set2(int z3,int z4)
{
p2.setdata(z3,z4);
}
void set3(Point &q)
{
p3=q;
}
void get1(int*r1,int*r2)
{
p1.get(r1,r2);
}
void get2(int*r3,int*r4)
{
p2.get(r3,r4);
}
void get3(Point&l)
{
l=p3;
}
double distance(Point j1,Point j2)
{
int q1,q2,q3,q4;
int*l1=&q1,*l2=&q2,*l3=&q3,*l4=&q4;
j1.get(l1,l2);
j2.get(l3,l4);
return sqrt((q1-q3)*(q1-q3)+(q2-q4)*(q2-q4));
}
double zc()
{
double r,s,t;
r=distance(p1,p2);
s=distance(p1,p3);
t=distance(p2,p3);
return (r+s+t);
}
double S()
{
double r,s,t,c;
r=distance(p1,p2);
s=distance(p1,p3);
t=distance(p2,p3);
c=zc()/2;
return (sqrt(c*(c-r)*(c-s)*(c-t)));
}
void show()
{
int q1,q2,q3,q4,q5,q6;
int*l1=&q1,*l2=&q2,*l3=&q3,*l4=&q4,*l5=&q5,*l6=&q6;
p1.get(l1,l2);
p2.get(l3,l4);
p3.get(l5,l6);
cout<<"第一个点的坐标:("<<q1<<","<<q2<<")"<<endl;
cout<<"第二个点的坐标:("<<q3<<","<<q4<<")"<<endl;
cout<<"第三个点的坐标:("<<q5<<","<<q6<<")"<<endl;
cout<<"第一条边的长度:"<<distance(p1,p2)<<endl;
cout<<"第二条边的长度:"<<distance(p1,p3)<<endl;
cout<<"第三条边的长度:"<<distance(p2,p3)<<endl;
cout<<"该三角形的周长为:"<<zc()<<endl;
cout<<"该三角形的面积为:"<<S()<<endl;
}
};
int main()
{
Triangle T;
Point p1(1,1),p2,p3(3,4);
Triangle q(p1,p2,p3);
T.show();
q.show();
return 0;
}
总结
如有帮助,还望点赞