using c++ to implement a class of trian gle
时间: 2024-05-03 07:20:47 浏览: 12
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.