C語言小程序 如何判斷三角型類型
更新時間:2013年07月22日 11:37:23 作者:
第一個判斷三角形的類型,兩個浮點型數(shù)據(jù)不能直接判斷相等,為了輸入方便一些,自己設置的精度比較低,10^(-3)
復制代碼 代碼如下:
#include <stdio.h>
#include <stdlib.h>
#define EPSINON 1e-3
#define ABS(a) (((a)>0)?(a):(-a)) //?:不支持表達式嵌套
#define ZERO(x) ((x)>-EPSINON && (x)<EPSINON)
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
float a, b, c;
float max, mid, min;
char input_err_flag = 0;
char judge_err_flag = 0;
int equal(float a, float b)
{
float tmp;
tmp = a - b;
tmp = ZERO(ABS(tmp));
return tmp;
}
void input(void)
{
a = b = c = 0;
printf("輸入三條邊的值:");
scanf("%f %f %f",&a, &b, &c);
if(!(a>0) || !(b>0) || !(c>0))
{
input_err_flag = 1;
}
}
void sort(void)
{
max = MAX(MAX(a,b),c);
min = MIN(MIN(a,b),c);
if(MAX(a,b) < c)
mid = MAX(a,b);
else
mid = MAX(MIN(a,b),c);
}
void judge(void)
{
float max_square, mid_square, min_square, tmp;
if(max >= (mid+min))
{
judge_err_flag = 1;
}
else
{
max_square = max * max;
mid_square = mid * mid;
min_square = min * min;
tmp = mid_square + min_square;
if(equal(mid,min) || equal(max, mid))
{
if(equal(mid, min))
{
if(mid == max)
puts("等邊三角形。");
else if(equal(max_square, tmp))
puts("等腰直角三角形。");
else if(max_square < tmp)
puts("等腰銳角三角形。");
else
puts("等腰鈍角三角形。");
}
else
{
if(equal(min, mid))
puts("等邊三角形。");
else
puts("等腰銳角三角形。");
}
}
else if(equal(max_square, tmp))
puts("直角三角形。");
else if(max_square < tmp)
puts("銳角三角形。");
else
puts("鈍角三角形。");
}
}
int main(void)
{
char cs, ch;
do
{
input();
sort();
judge();
if(input_err_flag)
{
input_err_flag = 0;
while((cs=getchar())!='\n' && (cs=getchar())!=EOF);
printf("輸入錯誤,a b c必須大于零,是否新輸入(y/n):");
}
else if(judge_err_flag)
{
judge_err_flag = 0;
while((cs=getchar())!='\n' && (cs=getchar())!=EOF);
printf("組不成三角形,是否重新輸入(y/n):");
}
else
{
while((cs=getchar())!='\n' && (cs=getchar())!=EOF);
printf("是否再輸入一組數(shù)據(jù)(y/n):");
}
ch = getchar();
}
while(ch=='y' || ch=='Y' || ch=='\n');
puts("Goodbye!");
return 0;
}
相關文章
C++11 學習筆記之std::function和bind綁定器
這篇文章主要介紹了C++11 學習筆記之std::function和bind綁定器,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07
C語言的數(shù)組學習入門之對數(shù)組初始化的操作
這篇文章主要介紹了C語言的數(shù)組學習入門之數(shù)組初始化的操作,是C語言入門學習中的基礎知識,需要的朋友可以參考下2015-12-12
C/C++中數(shù)據(jù)類型轉(zhuǎn)換詳解及其作用介紹
這篇文章主要介紹了C/C++中數(shù)據(jù)類型轉(zhuǎn)換詳解及其作用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
詳解設計模式中的Command命令模式及相關C++實現(xiàn)
這篇文章主要介紹了詳解設計模式中的Command命令模式及相關C++實現(xiàn),命令模式強調(diào)調(diào)用操作的對象和操作的具體實現(xiàn)者之間的解耦,需要的朋友可以參考下2016-03-03

