C language programming (third edition) He Qinming exercises 3-5
Article Directory
topic
Triangle judgment: Input the coordinates (x1, y1), (x2, y2), (x3, y3) of any three points on the plane, and check whether they can form a triangle.
If these 3 points can form a triangle, output the perimeter and area (with 2 decimal places);
otherwise, output "Impossble". Try to write the corresponding program.
Tip: In a triangle, the sum of any two sides is greater than the third side. The formula for calculating the area of a triangle is as follows:
area=
where s=(a+b+c)/2
Analysis process
enter
Condition: Input the coordinates (x1, y1), (x2, y2), (x3, y3) of any three points on the plane
Output
Condition: If these 3 points can form a triangle, output the perimeter and area (with 2 decimal places); otherwise, output "Impossble"
Code
#include <stdio.h>
#include <math.h>
int main () {
/*定义变量*/
double x1, y1, x2, y2, x3, y3; /*定义变量,存储输入的三个点的坐标(x1,y1)、(x2,y2)、(x3,y3)*/
double a, b, c, s, perimeter, area; /*定义变量,计算结果边长a, b, c, s 以及周长和面积*/
/*赋值*/
printf("请输入三个点的坐标(x1,y1)、(x2,y2)、(x3,y3):\n"); /*输入提示*/
scanf("%lf %lf %lf %lf %lf %lf \n", &x1, &y1, &x2, &y2, &x3, &y3); /*输入并赋给变量*/
/*计算*/
a = sqrt((pow(fabs(x1-x2), 2) + pow(fabs(y1-y2), 2))); /*计算结果边长a*/
b = sqrt((pow(fabs(x2-x3), 2) + pow(fabs(y2-y3), 2))); /*计算结果边长b*/
c = sqrt((pow(fabs(x1-x3), 2) + pow(fabs(y1-y3), 2))); /*计算结果边长c*/
if(a+b>c && a+c>b && b+c>a) {
/*符合任意两边之和大于第三边,则可构成三角形*/
s = (a+b+c)/2;
perimeter = a+b+c;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("周长 = %0.2lf, 面积 = %0.2lf", perimeter, area);
} else
printf("Impossble"); /*不能构成三角形*/
return 0;
}