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