用C语言编写程序实现以下功能:从键盘输入任意三个数a,b,c,若a,b,c的值能构成三角形,则计算并输出三角形的面积,否则打印输出不是三角形。已知计算三角形面积的公式为: s=(a+b+c)/2 area=sqrt(s(s-a)(s-b)(s-c)) ***输入提示信息:"Input a,b,c:" ***输入数据格式:"%f,%f,%f" ***输出格式要求: 若是三角形,则输出数据格式为:"area=%.4f\n" 若不是三角形,则输出数据格式为:"It is not a triangle\n"
时间: 2024-02-06 11:13:05 浏览: 46
以下是实现该功能的C语言代码:
```c
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c;
float s, area;
printf("Input a,b,c:");
scanf("%f,%f,%f", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a) {
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("area=%.4f\n", area);
} else {
printf("It is not a triangle\n");
}
return 0;
}
```
代码说明:
1. 定义三个浮点型变量a、b、c,用于存储输入的三角形三边的长度。
2. 定义两个浮点型变量s、area,分别用于存储三角形的半周长和面积。
3. 使用`printf()`函数输出输入提示信息。
4. 使用`scanf()`函数从键盘输入三角形三边的长度。
5. 使用条件语句判断输入的三边长度是否能构成三角形。
6. 如果能构成三角形,则计算三角形的面积,并使用`printf()`函数输出面积值。
7. 如果不能构成三角形,则使用`printf()`函数输出提示信息。
相关问题
编写C++程序实现以下功能:从键盘输入任意三个数a,b,c,若a,b,c的值能构成三角形,则计算并输出三角形的面积,否则打印输出不是三角形。
可以直接回答,以下是C语言实现代码:
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, s, area;
printf("请输入三角形的三条边长:\n");
scanf("%f%f%f", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a) {
// 计算半周长
s = (a + b + c) / 2;
// 计算面积
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("三角形的面积为:%.2f\n", area);
} else {
printf("这不是一个三角形!\n");
}
return 0;
}
用c语言编写一个程序,由键盘输入任意三个整数,按降序将其输出
### 回答1:
以下是用 C 语言编写一个从键盘输入任意三个整数并按照降序输出的程序:
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c) {
if (b >= c) {
printf("降序输出为:%d %d %d\n", a, b, c);
} else {
printf("降序输出为:%d %d %d\n", a, c, b);
}
} else if (b >= a && b >= c) {
if (a >= c) {
printf("降序输出为:%d %d %d\n", b, a, c);
} else {
printf("降序输出为:%d %d %d\n", b, c, a);
}
} else {
if (a >= b) {
printf("降序输出为:%d %d %d\n", c, a, b);
} else {
printf("降序输出为:%d %d %d\n", c, b, a);
}
}
return 0;
}
```
程序中首先定义了三个变量 a、b、c,用于存储从键盘输入的三个整数。然后通过 `printf()` 函数向用户输出提示信息,要求用户输入三个整数。接着,使用 `scanf()` 函数从键盘读取三个整数,并将它们分别存储在 a、b、c 变量中。
最后,程序使用一系列的判断语句,判断三个整数中的最大值和最小值,并按照降序输出。如果您输入的三个数是 2、7、4,则程序将输出 “降序输出为:7 4 2”。
### 回答2:
下面是一个用C语言编写的程序,可以输入任意三个整数,并按降序将它们输出:
```c
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void sortDescending(int *arr) {
for (int i = 0; i < 2; i++) {
for (int j = i+1; j < 3; j++) {
if (arr[i] < arr[j]) {
swap(&arr[i], &arr[j]);
}
}
}
}
int main() {
int nums[3];
printf("请输入三个整数:\n");
for (int i = 0; i < 3; i++) {
scanf("%d", &nums[i]);
}
sortDescending(nums);
printf("降序输出结果为:\n");
for (int i = 0; i < 3; i++) {
printf("%d ", nums[i]);
}
printf("\n");
return 0;
}
```
使用上述程序,你可以通过键盘输入三个整数,程序将按降序排序并输出这三个整数。
### 回答3:
要用C语言编写一个程序,实现从键盘输入任意三个整数,并按降序将它们输出,可以使用以下代码:
```c
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int a, b, c;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
// 对三个整数按降序进行排序
if (a < b) {
swap(&a, &b);
}
if (a < c) {
swap(&a, &c);
}
if (b < c) {
swap(&b, &c);
}
printf("降序输出为:%d, %d, %d\n", a, b, c);
return 0;
}
```
运行程序后,首先会要求输入三个整数,例如输入:5, 2, 7。然后,程序会对输入的三个整数进行降序排序,最终输出:7, 5, 2。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.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)