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