fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. typedef struct {
  5. double x;
  6. double y;
  7. } Point;
  8.  
  9. /* X座標とY座標を入力してPoint型の変数を返す */
  10. Point scan_point(void)
  11. {
  12. Point p;
  13.  
  14. printf("X座標を入力してください: ");
  15. scanf("%lf", &p.x);
  16.  
  17. printf("Y座標を入力してください: ");
  18. scanf("%lf", &p.y);
  19.  
  20. return p;
  21. }
  22.  
  23. /* 長方形の面積を計算する */
  24. double area_of(Point p1, Point p2)
  25. {
  26. double width;
  27. double height;
  28.  
  29. width = fabs(p2.x - p1.x);
  30. height = fabs(p2.y - p1.y);
  31.  
  32. return width * height;
  33. }
  34.  
  35. /* 長方形の周囲の長さを計算する */
  36. double circumference_of(Point p1, Point p2)
  37. {
  38. double width;
  39. double height;
  40.  
  41. width = fabs(p2.x - p1.x);
  42. height = fabs(p2.y - p1.y);
  43.  
  44. return 2.0 * (width + height);
  45. }
  46.  
  47. int main(void)
  48. {
  49. Point p1, p2;
  50. double area, circumference;
  51.  
  52. printf("左上隅の座標を入力します。\n");
  53. p1 = scan_point();
  54.  
  55. printf("\n右下隅の座標を入力します。\n");
  56. p2 = scan_point();
  57.  
  58. area = area_of(p1, p2);
  59. circumference = circumference_of(p1, p2);
  60.  
  61. printf("\n面積 = %.2f\n", area);
  62. printf("周囲の長さ = %.2f\n", circumference);
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
左上隅の座標を入力します。
X座標を入力してください: Y座標を入力してください: 
右下隅の座標を入力します。
X座標を入力してください: Y座標を入力してください: 
面積 = 0.00
周囲の長さ = 0.00