Zhejiang Edition "C Programming Language (3rd Edition)" Title Set Problem 3-5 triangular Analyzing (15 minutes)

Here Insert Picture Description
Solution: the sum of any two sides is greater than the third side to form a triangle. With three sides of a triangle area formula to seek called: Heron's formula.
Helen pushed to the following formula:
Here Insert Picture Description

#include <stdio.h>
#include <math.h>
int main(void)
{
    float x1, y1, x2, y2, x3, y3;
    float a, b, c, L, A, p;

    scanf("%f%f%f%f%f%f", &x1, &y1, &x2, &y2, &x3, &y3);
    a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    b = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
    c = sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
    if (a + b > c && a + c > b && b + c > a)
    {
        L = a + b + c;
        p = L / 2;
        A = sqrt(p * (p - a) * (p - b) * (p - c));
        printf("L = %.2f, A = %.2f\n", L, A);
    }
    else
        printf("Impossible\n");
    return 0;
}

Note: The middle of the side length abc into pow likely to cause errors.
Code Inspection:

#include <stdio.h>
#include <math.h>
int main(void)
{
    float x1, y1, x2, y2, x3, y3;
    float a, b, c, L, A, p;

    scanf("%f%f%f%f%f%f", &x1, &y1, &x2, &y2, &x3, &y3);
    a = sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
    b = sqrt(pow((x1 - x3), 2) + pow((y1 - y3), 2));
    c = sqrt(pow((x2 - x3), 2) + pow((y2 - y3), 2));
    if (a + b > c && a + c > b && b + c > a)
    {
        L = a + b + c;
        p = L / 2;
        A = sqrt(p * (p - a) * (p - b) * (p - c));
        printf("L = %.2f, A = %.2f\n", L, A);
    }
    else
        printf("Impossible\n");
    return 0;
}

Results are as follows:
Here Insert Picture Description
Analyze the reasons:
() function is used to find the x y power (power), x, and y are double type pow function value, which is a prototype: double pow (double x, double y);
likely caused the error:

  • If the base x is negative and y is not an integer index, it will lead to error domain error.
  • If the index base x and y are 0, it may result in an error domain error, or may not; this is related with the realization of the library.
  • If the base x is 0, y index is negative, could result in domain error pole error or error, it may not; this is related with the realization of the library.
  • If the return value ret is too big or too small, it will result in an error range error.
Published 165 original articles · won praise 117 · Views 7804

Guess you like

Origin blog.csdn.net/qq_44458489/article/details/105319481