当前位置:网站首页>【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:

边栏推荐
- Multi dimensional monitoring: the data base of intelligent monitoring
- Bisection (integer bisection and floating point bisection)
- Cohere, a large model company, completed the round B financing of US $125million
- 2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
- Contract quantitative trading system development | contract quantitative app development (ready-made cases)
- 行业分析| 快对讲,楼宇对讲
- Daily practice of C language - day 4: find the sum of all even numbers within 100
- Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed
- New listing of operation light 3.0 - a sincere work of self subversion across the times!
- 开源项目维权成功案例: spug 开源运维平台成功维权
猜你喜欢

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

【北京航空航天大学】考研初试复试资料分享

【C语言】NextDay问题

What method is required for word, PDF and txt files to realize full-text content retrieval?

Class pattern and syntax in JS 2021.11.10

行业分析| 快对讲,楼宇对讲

多维度监控:智能监控的数据基础

js中this的默认指向及如何修改指向 2021.11.09

纯纯大怨种!那些年被劝退的考研专业

Day30 JS notes BOM and DOM 2021.09.24
随机推荐
How to deploy the software testing environment?
5. Sum of N numbers
Come on, yuanuniverse. Sure enough, the heat won't pass for a while
Why do many people want to change careers as programmers, while some programmers want to change careers as others?
【JS】斐波那契数列实现(递归与循环)
2022 开源软件安全状况报告:超41%的企业对开源安全没有足够的信心
Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed
网页提示此站点不安全解决方案
携手Cigent:群联为SSD主控固件引入高级网络安全防护特性
Multi dimensional monitoring: the data base of intelligent monitoring
AcWing 608. Poor (implemented in C language)
2018 joint examination of nine provinces & Merging of line segment trees
IO stream of file and Base64
Redis 原理 - List
Day39 prototype chain and page Fireworks Effect 2021.10.13
Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly
Unity screenshot function
fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
零基础C语言(一)
js中的class类模式及语法 2021.11.10