c语言编写程序根据点的类构造线三角形类,并测试 【输入形式】输入三角形的3个坐标点 【输出形式】三角形的3个坐标点、3边长及周长和面积 【样例输入】 请输入点1的x的值:1 请输入点1的y的值:1 请输入点2的x的值:4 请输入点2的y的值:5 请输入点3的x的值:8 请输入点3的y的值:9 【样例输出】 点1的坐标为:(1,1) 点2的坐标为:(4,5) 点3的坐标为:(8,9) 边长1的长度:5 边长2的长度:10.6301 边长3的长度:5.65685 该三角形的周长为:21.287 该三角形的面积为:2 【样例说明】 【评分标准】3个评分点 //根据点的定义,写出线段和三角形的定义,并通过主程序进行验证 //mypoint.h class myPoint { public: myPoint(); myPoint(double x, double y); double getX(); double getY(); private: double mX,mY; };
时间: 2024-02-29 18:54:29 浏览: 82
//myline.h
#include "mypoint.h"
class myLine{
public:
myLine();
myLine(myPoint p1, myPoint p2);
double getLength();
private:
myPoint mP1,mP2;
};
//mytriangle.h
#include "mypoint.h"
#include "myline.h"
class myTriangle{
public:
myTriangle();
myTriangle(myPoint p1, myPoint p2, myPoint p3);
double getPerimeter(); //周长
double getArea(); //面积
private:
myPoint mP1,mP2,mP3;
myLine mLine1,mLine2,mLine3;
};
//main.cpp
#include "mytriangle.h"
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double x1,y1,x2,y2,x3,y3;
cout<<"请输入点1的x的值:";
cin>>x1;
cout<<"请输入点1的y的值:";
cin>>y1;
cout<<"请输入点2的x的值:";
cin>>x2;
cout<<"请输入点2的y的值:";
cin>>y2;
cout<<"请输入点3的x的值:";
cin>>x3;
cout<<"请输入点3的y的值:";
cin>>y3;
myPoint p1(x1,y1);
myPoint p2(x2,y2);
myPoint p3(x3,y3);
myTriangle tri(p1,p2,p3);
cout<<"点1的坐标为:("<<p1.getX()<<","<<p1.getY()<<")"<<endl;
cout<<"点2的坐标为:("<<p2.getX()<<","<<p2.getY()<<")"<<endl;
cout<<"点3的坐标为:("<<p3.getX()<<","<<p3.getY()<<")"<<endl;
cout<<"边长1的长度:"<<tri.mLine1.getLength()<<endl;
cout<<"边长2的长度:"<<tri.mLine2.getLength()<<endl;
cout<<"边长3的长度:"<<tri.mLine3.getLength()<<endl;
cout<<"该三角形的周长为:"<<tri.getPerimeter()<<endl;
cout<<"该三角形的面积为:"<<tri.getArea()<<endl;
return 0;
}
//mypoint.cpp
#include "mypoint.h"
myPoint::myPoint(){
mX = 0;
mY = 0;
}
myPoint::myPoint(double x, double y){
mX = x;
mY = y;
}
double myPoint::getX(){
return mX;
}
double myPoint::getY(){
return mY;
}
//myline.cpp
#include "myline.h"
#include <cmath>
myLine::myLine(){
mP1 = myPoint();
mP2 = myPoint();
}
myLine::myLine(myPoint p1, myPoint p2){
mP1 = p1;
mP2 = p2;
}
double myLine::getLength(){
return sqrt(pow(mP1.getX()-mP2.getX(),2)+pow(mP1.getY()-mP2.getY(),2));
}
//mytriangle.cpp
#include "mytriangle.h"
#include <cmath>
myTriangle::myTriangle(){
mP1 = myPoint();
mP2 = myPoint();
mP3 = myPoint();
mLine1 = myLine();
mLine2 = myLine();
mLine3 = myLine();
}
myTriangle::myTriangle(myPoint p1, myPoint p2, myPoint p3){
mP1 = p1;
mP2 = p2;
mP3 = p3;
mLine1 = myLine(mP1,mP2);
mLine2 = myLine(mP2,mP3);
mLine3 = myLine(mP3,mP1);
}
double myTriangle::getPerimeter(){
return mLine1.getLength()+mLine2.getLength()+mLine3.getLength();
}
double myTriangle::getArea(){
double a = mLine1.getLength();
double b = mLine2.getLength();
double c = mLine3.getLength();
double p = (a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
阅读全文