当前位置:网站首页>【C语言】判断三角形
【C语言】判断三角形
2022-06-28 11:36:00 【贾璞】
本不想上传这个,为了保持CSDN勋章,希望理解。
没有技术含量,就是考一些逻辑,不喜勿喷。
输入三个数(支持浮点数据),判断是否是三角形以及类型,边长范围[1-200]。
注释完善,自行理解。
执行环境 Ubuntu18.04 GCC编译
注意链接math.h文件,完整命令如下:
# gcc filename.c -lm
Code:
/*********************************************************** 根据用户输入三个边,甄选出是什么三角形. 声明:冒泡排序与交换函数完全可以省略,但是也可以便于后期利用a^2+b^2=c^2计算 以及math.h文件,权当练习简单排序,可以当做多此一举 ***********************************************************/
#include <stdio.h>
#include <math.h>
//交换两个函数值
void swapValue(float *a, float *b) {
float temp = *a;
*a = *b;
*b = temp;
}
//冒泡升序,便于后期计算
//单独写出该函数略显麻烦,可以不写,嵌于其他函数内部亦可
void bubbleSort(float *array, int len) {
for (int i = 0; i < len - 1; ++i)
for (int j = 0; j < len - 1 - i; ++j)
if (array[j] > array[j + 1])
swapValue(&array[j], &array[j + 1]);
}
//判断是什么三角形
void judgeTriangle(float *edges) {
//判断是否有超范围值,有就直接return
for (int i = 0; i < 3; ++i) {
if (edges[i] > 200 || edges[i] < 1) {
printf("Edges value error\n");
return;
}
}
//判断是否构成三角形条件
if (edges[0] + edges[1] > edges[2]) {
//构成等腰三角形条件
if (edges[0] == edges[1] || edges[1] == edges[2] || edges[2] == edges[0]) {
//直角等腰三角形
if (pow(edges[0], 2) + pow(edges[1], 2) == pow(edges[2], 2)) {
printf("Isosceles right triangle.\n");
}
//等边三角形
else if (edges[0] == edges[1] && edges[1] == edges[2] && edges[2] == edges[0]) {
printf("Regular triangle.\n");
}
//等腰三角形
else {
printf("Isosceles triangle.\n");
}
}
//pow()平方,判断直角三角形
else if (pow(edges[0], 2) + pow(edges[1], 2) == pow(edges[2], 2)) {
printf("Right triangle.\n");
} else {
printf("Triangle.\n");
}
} else {
printf("Not triangle.\n");
}
}
int main(void) {
//定义数组,存放输入数据
float edges[3] = {
0};
printf("Input three edges:\n");
for (int i = 0; i < 3; ++i)
scanf("%f", &edges[i]);
//sizeof(array)/sizeof(array[0]))计算出来的是数组长度,在此假设数组长度不明,可以记住这个公式
bubbleSort(edges, sizeof(edges) / sizeof(edges[0]));
//printf("%d,%d,%d\n", array[0], array[1], array[2]);
judgeTriangle(edges);
return 0;
}
Picture:

边栏推荐
- Which programming language will attract excellent talents?
- Web page tips this site is unsafe solution
- Self use demo of basic component integration of fluent
- 【C语言】如何很好的实现复数类型
- Open3d manual clipping point cloud
- day39 原型链及页面烟花效果 2021.10.13
- day23 js笔记 2021.09.14
- Day36 JS notes ecma6 syntax 2021.10.09
- Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
- 2022 开源软件安全状况报告:超41%的企业对开源安全没有足够的信心
猜你喜欢

Day39 prototype chain and page fireworks effect 2021.10.13

Unity屏幕截图功能

【C语言】NextDay问题

Day39 prototype chain and page Fireworks Effect 2021.10.13

js中的数组方法 2021.09.18

智联招聘基于 Nebula Graph 的推荐实践分享

Day29 JS notes 2021.09.23

Training notice | special training notice on epidemic prevention and security prevention for overseas Chinese funded enterprises, institutions and personnel in 2022

赛尔号抽奖模拟求期望

无法重新声明块范围变量
随机推荐
day24 js笔记 2021.09.15
Industry analysis - quick intercom, building intercom
Day30 JS notes BOM and DOM 2021.09.24
[sciter]: how sciter uses i18 to realize multi language switching of desktop applications and its advantages and disadvantages
Prefix and (one dimension)
Fruit FL studio/cubase/studio one music host software comparison
recent developments
Chapter 2 do you remember the point, line and surface (2)
Bisection (integer bisection and floating point bisection)
JS foundation 8
ProCAST finite element casting process simulation software
Is it safe to buy stocks and open an account on the account QR code of the CICC securities manager? Ask the great God for help
【C语言】NextDay问题
Open3d manual clipping point cloud
Intranet penetration in the working group environment: some basic methods
FTP protocol for Wireshark packet capture analysis
Is it feasible to be a programmer at the age of 26?
AcWing 606. Average 1 (implemented in C language)
Random forest and poetry maker trained by AMR
Apache2 configuration denies access to the directory, but can access the settings of the files inside