Advertisement
vencinachev

TriangleCalc

Mar 7th, 2022
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int triangle(double a, double b, double c, double* P, double* S){
  5.     // 0 -> ok, -1 -> nok
  6.     if (a<=0 ||b<=0 ||c<=0 || a+b<=c || a+c<=b || b+c<=a){
  7.         return -1;
  8.     }
  9.     *P = a + b + c;
  10.     double p = *P / 2.0;
  11.     *S = sqrt(p*(p-a)*(p-b)*(p-c));
  12.     return 0;
  13. }
  14.  
  15. int main(){
  16.     double a, b, c, P, S;
  17.     while (scanf("%lf %lf %lf", &a, &b, &c) != EOF){
  18.         if (triangle(a, b, c, &P, &S) == -1){
  19.             fprintf(stderr, "Invalid triangle sides!\n");      
  20.         } else {
  21.             printf("P = %.2lf, S = %.2lf\n", P, S);
  22.         }
  23.     }
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement