使用c语言2.编写程序,读入3维空间3个点的坐标(实数),输出这三个点所构成的三角形面积(保留小数点后2位)。
时间: 2024-05-02 20:19:16 浏览: 17
```c
#include <stdio.h>
#include <math.h>
int main()
{
float x1, y1, z1, x2, y2, z2, x3, y3, z3;
float a, b, c, s, area;
printf("请输入第1个点的坐标(x1,y1,z1):");
scanf("%f,%f,%f", &x1, &y1, &z1);
printf("请输入第2个点的坐标(x2,y2,z2):");
scanf("%f,%f,%f", &x2, &y2, &z2);
printf("请输入第3个点的坐标(x3,y3,z3):");
scanf("%f,%f,%f", &x3, &y3, &z3);
a = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2));
b = sqrt(pow(x3 - x2, 2) + pow(y3 - y2, 2) + pow(z3 - z2, 2));
c = sqrt(pow(x3 - x1, 2) + pow(y3 - y1, 2) + pow(z3 - z1, 2));
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("三角形的面积为:%.2f\n", area);
return 0;
}
```
相关问题
c语言读入+3+个字符,判断这+3+个字符所对应的+ASCII+码值能否构成三角形
要判断三个字符所对应的ASCII码值能否构成三角形,需要进行以下步骤:
1. 首先,使用C语言的输入函数(如scanf)读入三个字符。
2. 然后,通过ASCII码表将这三个字符转换为对应的ASCII码值。
3. 对这三个ASCII码值进行排序,以便后续判断。
4. 判断是否满足构成三角形的条件,即最小的两个边之和大于第三个边。如果满足条件,则可以构成三角形;否则,不能构成三角形。
以下是一个示例代码,用于实现上述功能:
```c
#include <stdio.h>
int main() {
char c1, c2, c3;
int ascii1, ascii2, ascii3;
printf("请输入三个字符:\n");
scanf("%c%c%c", &c1, &c2, &c3);
// 转换为ASCII码值
ascii1 = (int)c1;
ascii2 = (int)c2;
ascii3 = (int)c3;
// 排序
if (ascii1 > ascii2) {
int temp = ascii1;
ascii1 = ascii2;
ascii2 = temp;
}
if (ascii2 > ascii3) {
int temp = ascii2;
ascii2 = ascii3;
ascii3 = temp;
}
if (ascii1 > ascii2) {
int temp = ascii1;
ascii1 = ascii2;
ascii2 = temp;
}
// 判断是否能构成三角形
if (ascii1 + ascii2 > ascii3) {
printf("可以构成三角形\n");
} else {
printf("不能构成三角形\n");
}
return 0;
}
```
请注意,上述代码中的输入函数为scanf,通过%c格式符读入字符。在运行代码时,需要输入三个字符,然后程序会判断这三个字符所对应的ASCII码值能否构成三角形,并输出结果。
用c语言编写程序键盘输入三个整数判断是否构成三角形如果能构成求面积
您好,我是ChitGPT,一个类ChatGPT的AI。谢谢您的提问。以下是用C语言编写程序键盘输入三个整数判断是否构成三角形,如果能构成则求面积的例子:
```
#include <stdio.h>
#include <math.h>
int main()
{
int a, b, c;
float area;
printf("请输入三个整数:");
scanf("%d%d%d", &a, &b, &c);
if(a+b>c && b+c>a && c+a>b)
{
float p = (a+b+c)/2.0; //计算半周长
area = sqrt(p*(p-a)*(p-b)*(p-c)); //海伦公式计算面积
printf("能构成三角形,面积为 %.2f", area);
}
else
{
printf("不能构成三角形");
}
return 0;
}
```
注意:浮点数计算可能存在误差,所以在计算面积时需要保留足够的小数位数,例如这里保留了2位小数。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)