fork(1) download
  1. #include <stdio.h>
  2. #include <math.h> // fabs()を使うため
  3.  
  4. // Point構造体の定義
  5. typedef struct {
  6. double x;
  7. double y;
  8. } Point;
  9.  
  10. // 関数プロトタイプ宣言
  11. Point scan_point(int n);
  12. double area_of(Point p1, Point p2);
  13. double circumference_of(Point p1, Point p2);
  14.  
  15. // メイン関数
  16. int main(void) {
  17. Point p1, p2;
  18.  
  19. printf("左上隅と右下隅の座標を入力してください。\n");
  20.  
  21. p1 = scan_point(1);
  22. p2 = scan_point(2);
  23.  
  24. double area = area_of(p1, p2);
  25. double circum = circumference_of(p1, p2);
  26.  
  27. printf("面積:%.2f\n", area);
  28. printf("周囲の長さ:%.2f\n", circum);
  29.  
  30. return 0;
  31. }
  32.  
  33. // ---- 座標を入力してPoint構造体を返す関数 ----
  34. Point scan_point(int n) {
  35. Point p;
  36. scanf("%lf %lf", &p.x, &p.y);
  37. printf("座標%d (%.2f, %.2f)\n", n, p.x, p.y);
  38. return p;
  39. }
  40.  
  41. // ---- 面積を計算する関数 ----
  42. double area_of(Point p1, Point p2) {
  43. double width = fabs(p2.x - p1.x);
  44. double height = fabs(p2.y - p1.y);
  45. return width * height;
  46. }
  47.  
  48. // ---- 周の長さを計算する関数 ----
  49. double circumference_of(Point p1, Point p2) {
  50. double width = fabs(p2.x - p1.x);
  51. double height = fabs(p2.y - p1.y);
  52. return 2 * (width + height);
  53. }
  54.  
Success #stdin #stdout 0s 5320KB
stdin
0 0
3 4
stdout
左上隅と右下隅の座標を入力してください。
座標1 (0.00, 0.00)
座標2 (3.00, 4.00)
面積:12.00
周囲の長さ:14.00