关于C++设计三角形
设计一个三角形类triangle,私有数据成员为高和底.要求定义重载运算符">",以比较两个三角形面积的大小.谢谢大家了......
设计一个三角形类triangle,私有数据成员为高和底.要求定义重载运算符">",以比较两个三角形面积的大小.
谢谢大家了... 展开
谢谢大家了... 展开
3个回答
展开全部
#include<iostream.h>
#include<cmath>
class Triangle
{
public:
friend double operator + (const Triangle& t1,const Triangle& t2);
Triangle(double t_length,double t_width,double t_high);
Triangle();
void area();
void input();
void output();
private:
double length;
double width;
double high;
double areas;
};
int main()
{
Triangle tri1,tri2;
double tri;
cout<<"Please input the edges of the triangle one!(press enter to continue)\n";
tri1.input();
tri1.area();
cout<<"Please input the edges of the triangle two!(press enter to continue)\n";
tri2.input();
tri2.area();
tri=tri1+tri2;
cout << "The area of triangle one add triangle two is:\n" << tri << endl;
return 0;
}
double operator + (const Triangle& t1,const Triangle& t2)
{
double temp;
temp=t1.areas+t2.areas;
return temp;
}
Triangle::Triangle(double t_length,double t_width,double t_high)
{
length=t_length;
width=t_width;
high=t_high;
}
Triangle::Triangle():length(0),width(0),high(0),areas(0)
{
}
void Triangle::area()
{
double s;
s=(length+width+high)/2;
areas=sqrt(s*(s-length)*(s-width)*(s-high));
}
void Triangle::input()
{
cin >> length >> width >> high;
while(((length+width)<high)||((length+high)<width)||((width+high)<length))
{
cout<<"The data what you input is wrong,please input it again!\n";
cin >> length >> width >> high;
}
}
void Triangle::output()
{
cout << areas << endl;
}
分给我吧,打字好累的
#include<cmath>
class Triangle
{
public:
friend double operator + (const Triangle& t1,const Triangle& t2);
Triangle(double t_length,double t_width,double t_high);
Triangle();
void area();
void input();
void output();
private:
double length;
double width;
double high;
double areas;
};
int main()
{
Triangle tri1,tri2;
double tri;
cout<<"Please input the edges of the triangle one!(press enter to continue)\n";
tri1.input();
tri1.area();
cout<<"Please input the edges of the triangle two!(press enter to continue)\n";
tri2.input();
tri2.area();
tri=tri1+tri2;
cout << "The area of triangle one add triangle two is:\n" << tri << endl;
return 0;
}
double operator + (const Triangle& t1,const Triangle& t2)
{
double temp;
temp=t1.areas+t2.areas;
return temp;
}
Triangle::Triangle(double t_length,double t_width,double t_high)
{
length=t_length;
width=t_width;
high=t_high;
}
Triangle::Triangle():length(0),width(0),high(0),areas(0)
{
}
void Triangle::area()
{
double s;
s=(length+width+high)/2;
areas=sqrt(s*(s-length)*(s-width)*(s-high));
}
void Triangle::input()
{
cin >> length >> width >> high;
while(((length+width)<high)||((length+high)<width)||((width+high)<length))
{
cout<<"The data what you input is wrong,please input it again!\n";
cin >> length >> width >> high;
}
}
void Triangle::output()
{
cout << areas << endl;
}
分给我吧,打字好累的
展开全部
class CTriangle
{
public:
CTriangle(int low, int height);
int GetArea();
bool operator>(CTriangle& a);
private:
int m_low;
int m_height;
};
CTriangle::CTriangle(int low, int height)
{
m_low = low;
m_height = height;
}
int CTriangle::GetArea()
{
return m_low*m_height/2;
}
bool CTriangle::operator>(CTriangle& a)
{
if(GetArea() > a.GetArea())
return true;
return false;
}
{
public:
CTriangle(int low, int height);
int GetArea();
bool operator>(CTriangle& a);
private:
int m_low;
int m_height;
};
CTriangle::CTriangle(int low, int height)
{
m_low = low;
m_height = height;
}
int CTriangle::GetArea()
{
return m_low*m_height/2;
}
bool CTriangle::operator>(CTriangle& a)
{
if(GetArea() > a.GetArea())
return true;
return false;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询