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