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