fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. /* 三角形の構造体 */
  5. typedef struct {
  6. double a;
  7. double b;
  8. double c;
  9. } Triangle;
  10.  
  11. /* 面積を計算する関数(ヘロンの公式) */
  12. double calc_area(Triangle t) {
  13. double p = (t.a + t.b + t.c) / 2.0;
  14. return sqrt(p * (p - t.a) * (p - t.b) * (p - t.c));
  15. }
  16.  
  17. int main(void) {
  18. Triangle t;
  19. double s;
  20.  
  21. /* 辺の長さを代入 */
  22. t.a = 5;
  23. t.b = 5;
  24. t.c = 5;
  25.  
  26. /* 面積計算 */
  27. s = calc_area(t);
  28.  
  29. /* 出力 */
  30. printf("a : %.0f\n", t.a);
  31. printf("b : %.0f\n", t.b);
  32. printf("c : %.0f\n", t.c);
  33. printf("三角形の面積 : %f\n", s);
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
a : 5
b : 5
c : 5
三角形の面積 : 10.825318