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