完整的C++程序:
#include <iostream>
#include <math.h>
#include<iomanip>
using namespace std
class Triangle {
public:
Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
double getArea()
double getPerimeter()
private:
double _x1
double _y1
double _x2
double _y2
double _x3
double _y3
double _l1
double _l2
double _l3
}
//在下面的空格实现Triangle类的成员函数
Triangle::Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
{
this->_x1 = x1
this->_y1 = y1
this->_x2 = x2
this->_y2 = y2
this->_x3 = x3
this->_y3 = y3
this->_l1 = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2))
this->_l2 = sqrt(pow(x3 - x2, 2) + pow(y3 - y2, 2))
this->_l3 = sqrt(pow(x1 - x3, 2) + pow(y1 - y3, 2))
}
double Triangle::getArea()
{
double p = (_l1 + _l2 + _l3) / 2
return sqrt(p * (p - _l1) * (p - _l2) * (p - _l3))
}
double Triangle::getPerimeter()
{
return _l1 + _l2 + _l3
}
int main()
{
double x1, x2, x3, y1, y2, y3
cin >>x1>>y1>>x2>>y2>>x3>>y3
Triangle t(x1, y1, x2, y2, x3, y3)
//在下面的空格按题目要求输出结果
cout <<setiosflags(ios::fixed)<<setprecision(2)<<t.getArea()<<endl
cout <<setiosflags(ios::fixed)<<setprecision(2)<<t.getPerimeter()<<endl
return 0
}
运行测试:
就是说只用写几个函数就可以了对吧!建议看我给我好评哦,亲!//声明
Rectangle(double x1,double y1,double x2,double y2)
Rectangle&operator=(const Rectangle&obj)
int GetLength()
int GetArea()
//实现
Rectangle::Rectangle(double x1,double y1,double x2,double y2) : _x1(x1),_y1(y1),_x2(x2),_y2(y2){
_width = x2 - x1
_height = y2-y1
}
Rectangle&operator=(const Rectangle&obj){
this._x1 = obj._x1
this._y1 = obj._y1
this._x2 = obj._x2
this._y2 = obj._y2
this._width = obj._width
this._height = obj._height
}
double Rectangle::GetLength(){ return 2*(_width + _height) }
double Rectangle::GetArea(){ return _width * _height }
//使用
cout<<fixed<<setprecision(2) <<r1.GetLength() <<endl <<r1.GetArea()<<endl
<<r1.GetLength() + r2.GetLength() <<endl
<< r1.GetArea() + r2.GetArea() <<endl
因为一个字符数组,必须包含'\n'的部分,你定义了长度为8的,最多输入7个字符,如果输入8个字符‘\n’就没了,但是还是可以俺你的要求输出,可是如果更多的话就不对了。你可以把那个长度改长点,求采纳欢迎分享,转载请注明来源:内存溢出
评论列表(0条)