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