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(p2.y - p1.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(p2.y - p1.y);
  24. return 2 * (width + height);
  25. }
  26.  
  27. int main(void) {
  28. Point p1, p2;
  29. double area, circumference;
  30.  
  31. printf("左上隅の座標を入力:\n");
  32. p1 = scan_point();
  33. printf("座標1(%lf %lf)", p1.x, p1.y);
  34.  
  35. printf("右下隅の座標を入力:\n");
  36. p2 = scan_point();
  37. printf("座標2(%lf %lf)", p2.x, p2.y);
  38.  
  39. area = area_of(p1, p2);
  40. circumference = circumference_of(p1, p2);
  41.  
  42. printf("面積: %.2f\n", area);
  43. printf("周囲の長さ: %.2f\n", circumference);
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0.01s 5316KB
stdin
0.00 0.00
1.00 1.00
stdout
左上隅の座標を入力:
座標1(0.000000 0.000000)右下隅の座標を入力:
座標2(1.000000 1.000000)面積: 1.00
周囲の長さ: 4.00