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