C language applet how to judge triangle type

  • 2020-04-02 01:12:41
  • OfStack


#include <stdio.h>
#include <stdlib.h>
#define EPSINON  1e-3
#define ABS(a)  (((a)>0)?(a):(-a)) //? : nesting of expressions is not supported
#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(" Enter the values of three edges: ");
 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(" Equilateral triangle. ");
    else if(equal(max_square, tmp))
     puts(" An isosceles right triangle. ");
    else if(max_square < tmp)
     puts(" An acute isosceles triangle. ");
    else 
     puts(" Isosceles obtuse triangle. ");
   }
   else
   {
    if(equal(min, mid))
     puts(" Equilateral triangle. ");
    else
     puts(" An acute isosceles triangle. ");
   }
  }
  else if(equal(max_square, tmp)) 
   puts(" Right triangle. ");
  else if(max_square < tmp)
   puts(" An acute triangle. ");
  else 
   puts(" An obtuse triangle. ");
 }
}
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(" Typing error, a b c Must be greater than zero, whether new input (y/n) : ");
  }
  else if(judge_err_flag)
  {
   judge_err_flag = 0;
   while((cs=getchar())!='n' && (cs=getchar())!=EOF);
   printf(" Unformed triangle , Whether or not to retype (y/n) : ");
  }
  else
  {
   while((cs=getchar())!='n' && (cs=getchar())!=EOF);
   printf(" Whether to enter another set of data (y/n) : ");
  }
  ch = getchar();
 }
 while(ch=='y' || ch=='Y' || ch=='n');
 puts("Goodbye!");
 return 0;
}


Related articles: