C language example--determining the type of triangle

Question: Determine the type of the triangle based on the three sides of the input triangle, and output its area and type.

Think first:

Step 1: First determine whether the input three sides can form a triangle. (a+b>c && a+c>b && b+c>a).

Step 2: Calculate the area of ​​the triangle: According to Qin Jiuzhao's formula: first calculate half of the perimeter of the triangle, and then calculate the area of ​​the triangle according to the formula (area=sqrt(s*(sa)*(sb)*(sc))); sart is a mathematical function to find the square root.

Step 3: Determine the type of triangle:

               Equilateral triangle (a==b && a==c);

               Isosceles triangle (a==b||b==c||b==c)

                Right triangle (a*a+b*b==c*c || a*a+c*c==b*b || b*b+c*c==a*a)

                Otherwise it is an ordinary triangle.

code show as below:

#include <stdio.h> //Reference the header file
#include <math.h> //Math formula function
main()
{
	float a,b,c; //define the three sides of the triangle
	float s,area; //Define the perimeter and area of ​​the triangle
	printf("Please enter the three sides of the triangle you want to judge:");        
	scanf("%f%f%f",&a,&b,&c); //Enter three sides
												//first step:
	if(a+b>c && a+c>b && b+c>a) //Condition to determine whether the triangle holds
	{ //Second step:			
		s=(a+b+c)/2; //Calculate the perimeter of half of the triangle
		area=sqrt(s*(sa)*(sb)*(sc)); //Calculate the area formula of the triangle: Qin Jiuzhao's formula (I don't know if it can be Baidu Yibo)
		printf("The area of ​​this triangle is: %f\n",area);
												//third step:
		if(a==b && a==c) //First determine whether the triangle is an equilateral triangle
		{
			printf("This triangle is an equilateral triangle\n");
		}
		else
			if(a==b ||a==c || b==c) // Then judge whether the triangle is an isosceles triangle
			{
				printf("This triangle is an isosceles triangle\n");
			}
			else
				if(a*a+b*b==c*c || a*a+c*c==b*b || b*b+c*c==a*a) //Determine whether the triangle is a right angle triangle
				{
					printf("This triangle is a right triangle\n");
				}
				else
				{
					printf("This triangle is a normal triangle\n"); //If it is not a normal triangle
				}
	}
	else
	{
		printf("The three sides cannot form a triangle!!!\n"); //The three sides cannot form a triangle
	}
}

The output is as follows:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324488477&siteId=291194637