fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. typedef struct {
  5. double x;
  6. double y;
  7. } Point;
  8.  
  9. Point scan_point(void) {
  10. Point p;
  11. scanf("%lf %lf", &p.x, &p.y);
  12. return p;
  13. }
  14.  
  15. double area_of(Point p1, Point p2) {
  16. double width = fabs(p2.x - p1.x);
  17. double height = fabs(p1.y - p2.y);
  18. return width * height;
  19. }
  20.  
  21. double circumference_of(Point p1, Point p2) {
  22. double width = fabs(p2.x - p1.x);
  23. double height = fabs(p1.y - p2.y);
  24. return 2 * (width + height);
  25. }
  26.  
  27. int main(void) {
  28. Point p1, p2;
  29. double area, circumference;
  30. printf("左上隅: ");
  31. p1 = scan_point();
  32. printf("右下隅: ");
  33. p2 = scan_point();
  34.  
  35. area = area_of(p1, p2);
  36. circumference = circumference_of(p1, p2);
  37.  
  38. printf("面積 = %.2f\n", area);
  39. printf("周囲の長さ = %.2f\n", circumference);
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5276KB
stdin
1 4
3 2
stdout
左上隅: 右下隅: 面積 = 4.00
周囲の長さ = 8.00