三角形类.cpp 1010 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4. // 三角形类的定义
  5. class Tri {
  6. private:
  7. double side1, side2, side3;
  8. public:
  9. Tri(double s1, double s2, double s3) {
  10. side1 = s1;
  11. side2 = s2;
  12. side3 = s3;
  13. }
  14. double perimeter() {
  15. return side1 + side2 + side3;
  16. }
  17. double area() {
  18. double s = (side1 + side2 + side3) / 2;
  19. return sqrt(s * (s - side1) * (s - side2) * (s - side3));
  20. }
  21. void print() {
  22. if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) {
  23. cout << "三角形的边长:" << side1 << " " << side2 << " " << side3 << endl;
  24. cout << "三角形的周长:" << perimeter() << endl;
  25. cout << "三角形的面积:" << area() << endl;
  26. } else {
  27. cout << "不构成三角形!" << endl;
  28. }
  29. }
  30. };
  31. int main() {
  32. double s1, s2, s3;
  33. cin >> s1 >> s2 >> s3;
  34. Tri triangle(s1, s2, s3);
  35. triangle.print();
  36. return 0;
  37. }