using c++ to implement a class of trian gle
时间: 2024-06-06 11:11:45 浏览: 20
Sure, to implement a class of triangle using C, you can define a structure with three variables representing the length of each side of the triangle. Then, you can write functions to compute the area and perimeter of the triangle based on these side lengths. Would you like me to provide sample code for this?
相关问题
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.
using c++ to implement a class of triangle
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.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)