/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:test.cpp *作者: 武聪 *完成日期:2016年3月10日 *版本号:v1.0 *问题描述:求三角形周长和面积 16.*输入描述: 输入三角形的三边长 17. *程序输出: 三角形的周长和面积 #include <iostream> #include<Cmath> using namespace std; class Triangle { public: void setA(double x) { a=x; } void setB(double y) { b=y; } void setC(double z) { c=z; } double getA() { return a; } double getB() { return b; } double getC() { return c; } bool isTriangle(); double perimeter(); double area(); private: double a,b,c; }; bool Triangle::isTriangle() { if(a+b>c&&a+c>b&&b+c>a) return 1; else return 0; } double Triangle::perimeter() { return a+b+c; } double Triangle::area() { double p=(a+b+c)/2; return sqrt(p*(p-a)*(p-b)*(p-c)); } int main() { Triangle tri1; double x,y,z; cout<<"请输入三角形的三边"<<endl; 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; }
运行结果:- 知识点总结:
- 函数的使用
- 学习心得:
- 自己学的太差了,练习又少,以后要挤出来时间上机练习
三角形2
最新推荐文章于 2020-04-08 15:44:09 发布