首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

已知三角形三点通过类求三角形的周长解决办法

2012-02-22 
已知三角形三点通过类求三角形的周长求救:已知三角形三点,通过类用c++求三角形的周长?[解决办法]#include

已知三角形三点通过类求三角形的周长
求救:已知三角形三点,通过类用c++求三角形的周长?

[解决办法]
#include <iostream>
#include <math.h>

using namespace std;

class Triangle {
public:
struct Coor {
int x;
int y;

Coor() :
x(0), y(0) {
}

Coor(int x, int y) :
x(x), y(y) {
}
};

Triangle(const Coor& d1, const Coor& d2, const Coor& d3) {
this-> d1 = d1;
this-> d2 = d2;
this-> d3 = d3;
}

double GetZhouChang() {
return GetLength(d1, d2) + GetLength(d2, d3) + GetLength(d3, d1);
}

private:
double GetLength(const Coor& d1, const Coor& d2) {
return pow(pow(abs(d1.x - d2.x), 2) + pow(abs(d1.y - d2.y), 2), 0.5);
}

Coor d1;
Coor d2;
Coor d3;
};

int main(int argc, char* argv[]) {
Triangle triangle(Triangle::Coor(1, 2), Triangle::Coor(3, 5), Triangle::Coor(2, 4));
cout < < triangle.GetZhouChang() < < endl;
}

[解决办法]
#include "iostream.h "
#include "math.h "
class dian{
public:
dian(float xx=0,float yy=0){
x=xx;y=yy;}
float getx(){return x;}
float gety(){return y;}
private:
float x; //x坐标
float y; //y坐标
};

class bianchang1
{
public:
bianchang1(dian xd1,dian xd2);
double getbc1(void){return bc1;}
private:
dian d1,d2;
double bc1;
};
bianchang1::bianchang1(dian xd1,dian xd2):d1(xd1),d2(xd2)
{
double x1=double(d1.getx()-d2.getx());
double y1=double(d1.gety()-d2.gety());
bc1=sqrt(x1*x1+y1*y1);
}
class bianchang2
{
public:
bianchang2(dian xd1,dian xd3);
double getbc2(void){return bc2;}
private:
dian d1,d3;
double bc2;
};

bianchang2::bianchang2(dian xd1,dian xd3):d1(xd1),d3(xd3)
{
double x2=double(d1.getx()-d3.getx());
double y2=double(d1.gety()-d3.gety());
bc2=sqrt(x2*x2+y2*y2);
}
class bianchang3
{
public:
bianchang3(dian xd2,dian xd3);
double getbc3(void){return bc3;}
private:
dian d2,d3;
double bc3;
};

bianchang3::bianchang3(dian xd2,dian xd3):d2(xd2),d3(xd3)
{
double x3=double(d2.getx()-d3.getx());
double y3=double(d2.gety()-d3.gety());
bc3=sqrt(x3*x3+y3*y3);
}

float pd();
double zhouchang();
double a,b,c;
float jieguo = 0 ;
void main()
{
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int nPoint1x, nPoint1y, nPoint2x, nPoint2y, nPoint3x, nPoint3y;
cout < < "请输入顶点1的x坐标: " < < endl;
cin > > nPoint1x;
cout < < "请输入顶点1的y坐标: " < < endl;
cin > > nPoint1y;
cout < < "请输入顶点2的x坐标: " < < endl;
cin > > nPoint2x;
cout < < "请输入顶点2的y坐标: " < < endl;
cin > > nPoint2y;
cout < < "请输入顶点3的x坐标: " < < endl;
cin > > nPoint3x;
cout < < "请输入顶点3的y坐标: " < < endl;
cin > > nPoint3y;
dian d1(nPoint1x, nPoint1y),d2(nPoint2x, nPoint2y),d3(nPoint3x, nPoint3y);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


bianchang1 bc1(d1,d2);a=bc1.getbc1();
bianchang2 bc2(d1,d3);b=bc2.getbc2();
bianchang3 bc3(d2,d3);c=bc3.getbc3();
cout < < "a= " < <bc1.getbc1() < <endl;
cout < < "b= " < <bc2.getbc2() < <endl;
cout < < "c= " < <bc3.getbc3() < <endl;
jieguo = pd();
zhouchang();
}

float pd()
{
cout < < "判断是否能构成三角形 " < <endl;
if(a+b> c && a+c> b && b+c> a)
{
cout < < "此三点能构成三角形!!! ";
return jieguo = 1;
}
else {
cout < < "此三点不能构成三角形. " < <endl;
return jieguo = 0;
}

}

double zhouchang()
{
double zc;
if(jieguo == 1 )
{
zc=a+b+c;
cout < < "此三角形的周长为: " < <zc < <endl;
}
return 0;
}

热点排行