*/
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名:sanjiaoxing.cpp
* 作者:常轩
* 微信公众号:Worldhello
* 完成日期:2016年3月23日
* 版本号:V1.2
* 问题描述:三角形类雏形
* 程序输入:三角形三边
* 程序输出:见运行结果
*/
#include<iostream>
#include<cmath>
using namespace std;
class Triangle
{
public:
double perimeter();
double area();
void setA(double x);
void setB(double y);
void setC(double z);
double getA();
double getB();
double getC();
bool isTriangle();
private:
double a,b,c;
};
inline void Triangle::setA(double x)
{
a=x;
}
inline void Triangle::setB(double y)
{
b=y;
}
inline void Triangle::setC(double z)
{
c=z;
}
inline double Triangle::getA()
{
return a;
}
inline double Triangle::getB()
{
return b;
}
inline double Triangle::getC()
{
return c;
}
double Triangle::perimeter()
{
return a+b+c;
}
double Triangle::area()
{
double p;
p=(a+b+c)/2.0;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
运行结果:
<img src="//img-blog.csdn.net/20160330183056599?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> bool Triangle::isTriangle()
心得:
多做呗,就顺手了,就熟练了
{
if(a+b>c&&b+c>a&&a+c>b)
return 1;
else
return 0;
}
int main()
{
Triangle tri1;
double x,y,z;
cout<<"请输入三角形的三边:";
cin>>x>>y>>z;
tri1.setA(x);
tri1.setB(y);
tri1.setC(z);
if(tri1.isTriangle())
{
cout<<"三条边为:"<<tri1.getA()<<','<<tri1.getB()<<','<<tri1.getC()<<endl;
cout<<"三角形的周长为:"<<tri1.perimeter()<<'\t'<<"面积为:"<<tri1.area()<<endl;
}
else
cout<<"不能构成三角形"<<endl;
return 0;
}