123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- #include<iostream>
- #include<math.h>
- using namespace std;
- // 三角形类的定义
- class Tri {
- private:
- double side1, side2, side3;
- public:
- Tri(double s1, double s2, double s3) {
- side1 = s1;
- side2 = s2;
- side3 = s3;
- }
- double perimeter() {
- return side1 + side2 + side3;
- }
- double area() {
- double s = (side1 + side2 + side3) / 2;
- return sqrt(s * (s - side1) * (s - side2) * (s - side3));
- }
- void print() {
- if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) {
- cout << "三角形的边长:" << side1 << " " << side2 << " " << side3 << endl;
- cout << "三角形的周长:" << perimeter() << endl;
- cout << "三角形的面积:" << area() << endl;
- } else {
- cout << "不构成三角形!" << endl;
- }
- }
- };
- int main() {
- double s1, s2, s3;
- cin >> s1 >> s2 >> s3;
- Tri triangle(s1, s2, s3);
- triangle.print();
- return 0;
- }
|