Advertisement
xxxYOUNGxxx

Triangle

Dec 26th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #pragma warning(disable:4996)
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. #define PI 3.14159265
  8.  
  9. struct Triangle {
  10.     double a, b, c;      
  11.     double alpha, beta, gamma;
  12.     char a_filled, b_filled, c_filled;
  13.     char alpha_filled, beta_filled, gamma_filled;
  14. };
  15.  
  16. void input(struct Triangle* t) {
  17.     printf("(a): ");
  18.     scanf("%lf", &t->a);
  19.     if (t->a > 0) t->a_filled = 1;
  20.  
  21.     printf("(b): ");
  22.     scanf("%lf", &t->b);
  23.     if (t->b > 0) t->b_filled = 1;
  24.  
  25.     printf("(c): ");
  26.     scanf("%lf", &t->c);
  27.     if (t->c > 0) t->c_filled = 1;
  28.  
  29.     printf("alpha: ");
  30.     scanf("%lf", &t->alpha);
  31.     if (t->alpha > 0) t->alpha_filled = 1;
  32.  
  33.     printf("beta: ");
  34.     scanf("%lf", &t->beta);
  35.     if (t->beta > 0) t->beta_filled = 1;
  36.  
  37.     printf("gamma: ");
  38.     scanf("%lf", &t->gamma);
  39.     if (t->gamma > 0) t->gamma_filled = 1;
  40.  
  41. }
  42.  
  43. void output(const struct Triangle* t) {
  44.     printf("\na = %g, b = %g, c = %g\n", t->a, t->b, t->c);
  45.     printf("alpha = %g, beta = %g, gamma = %g\n", t->alpha, t->beta, t->gamma);
  46. }
  47.  
  48. bool isValidTriangle(struct Triangle* t) {
  49.     return (((t->a + t->b > t->c) && (t->a + t->c > t->b) && (t->b + t->c > t->a)) || (t->alpha + t->beta + t->gamma > 180));
  50. }
  51.  
  52. void missel(struct Triangle* t) {
  53.     if (!isValidTriangle) {
  54.         if (t->a_filled && t->b_filled && t->alpha_filled) {
  55.  
  56.             t->c = sqrt(pow(t->a, 2) + pow(t->b, 2) - 2 * t->a * t->b * cos(t->alpha * PI / 180));
  57.             t->c_filled = 1;
  58.  
  59.             t->beta = asin(sin(t->alpha * PI / 180) * t->b / t->c) * 180 / PI;
  60.             t->beta_filled = 1;
  61.  
  62.             t->gamma = 180 - t->alpha - t->beta;
  63.             t->gamma_filled = 1;
  64.         }
  65.         else if (t->a_filled && t->c_filled && t->beta_filled) {
  66.  
  67.             t->b = sqrt(pow(t->a, 2) + pow(t->c, 2) - 2 * t->a * t->c * cos(t->beta * PI / 180));
  68.             t->b_filled = 1;
  69.  
  70.             t->alpha = asin(sin(t->beta * PI / 180) * t->a / t->b) * 180 / PI;
  71.             t->alpha_filled = 1;
  72.  
  73.             t->gamma = 180 - t->alpha - t->beta;
  74.             t->gamma_filled = 1;
  75.         }
  76.         else if (t->b_filled && t->c_filled && t->gamma_filled) {
  77.  
  78.             t->a = sqrt(pow(t->b, 2) + pow(t->c, 2) - 2 * t->b * t->c * cos(t->gamma * PI / 180));
  79.             t->a_filled = 1;
  80.  
  81.             t->beta = asin(sin(t->gamma * PI / 180) * t->b / t->a) * 180 / PI;
  82.             t->beta_filled = 1;
  83.  
  84.             t->alpha = 180 - t->beta - t->gamma;
  85.             t->alpha_filled = 1;
  86.         }
  87.         else {
  88.             printf("Triangle doesn't exists\n");
  89.         }
  90.     }
  91.     else {
  92.         printf("Triangle doesn't exists\n");
  93.     }
  94. }
  95.  
  96. int main() {
  97.  
  98.     struct Triangle t;
  99.     memset(&t, 0, sizeof(t));
  100.     input(&t);
  101.     output(&t);
  102.     missel(&t);
  103.  
  104.     if (!isValidTriangle) output(&t);
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement