fork download
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. typedef struct {
  7. double x;
  8. double y;
  9. } Point;
  10.  
  11. Point scan_point(void)
  12. {
  13. Point p;
  14.  
  15. scanf("%lf %lf", &p.x, &p.y);
  16.  
  17. return p;
  18. }
  19.  
  20. double area_of(Point p1, Point p2)
  21. {
  22. double width = fabs(p2.x - p1.x);
  23. double height = fabs(p1.y - p2.y);
  24.  
  25. return width * height;
  26. }
  27.  
  28. double circumference_of(Point p1, Point p2)
  29. {
  30. double width = fabs(p2.x - p1.x);
  31. double height = fabs(p1.y - p2.y);
  32.  
  33. return 2.0 * (width + height);
  34. }
  35.  
  36. int main(void)
  37. {
  38. Point p1, p2;
  39.  
  40. printf("左上隅と右下隅の座標を入力してください。\n");
  41.  
  42. printf("座標1(x y): ");
  43. p1 = scan_point();
  44.  
  45. printf("座標2(x y): ");
  46. p2 = scan_point();
  47.  
  48. printf("座標1(%.2f, %.2f)\n", p1.x, p1.y);
  49. printf("座標2(%.2f, %.2f)\n", p2.x, p2.y);
  50. printf("面積: %.2f\n", area_of(p1, p2));
  51. printf("周囲の長さ: %.2f\n",
  52. circumference_of(p1, p2));
  53.  
  54. return 0;
  55. }
  56.  
  57.  
Success #stdin #stdout 0s 5320KB
stdin
2 8
6 3
stdout
左上隅と右下隅の座標を入力してください。
座標1(x y): 座標2(x y): 座標1(2.00, 8.00)
座標2(6.00, 3.00)
面積: 20.00
周囲の長さ: 18.00