using c++ to implement a class of triangle
时间: 2024-05-26 07:10:00 浏览: 103
Sure, to implement a class of triangle in C, you can create a structure for the triangle with three variables for the three sides, and then define functions for computing the perimeter, area, and angles of the triangle. The functions can use the formulae for calculating these quantities based on the sides of the triangle.
相关问题
Using c++ to implement a class of Triangle.
Sure, I can help you with that! Creating a class for Triangle in C is fairly simple. You'll need to define the class with three properties for the three sides of the triangle and some methods to calculate its area and perimeter. Is there anything specific you need help with?
using c++ to implement a class of trian gle
Sure, to implement a class of triangle using C, you can define a struct for the triangle with three sides as its properties. Then, you can write functions for calculating the perimeter, area or any other operations you need. Here's an example code:
```
#include <stdio.h>
#include <math.h>
struct Triangle {
double side1;
double side2;
double side3;
};
double calculatePerimeter(struct Triangle t) {
return t.side1 + t.side2 + t.side3;
}
double calculateArea(struct Triangle t) {
double p = calculatePerimeter(t) / 2.0;
return sqrt(p * (p - t.side1) * (p - t.side2) * (p - t.side3));
}
int main() {
struct Triangle t1 = { 3.0, 4.0, 5.0 };
printf("Perimeter of triangle t1 = %.2lf\n", calculatePerimeter(t1));
printf("Area of triangle t1 = %.2lf\n", calculateArea(t1));
return 0;
}
```
Please note that this is just an example implementation and there may be other ways to implement a class of triangle in C.
阅读全文