fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. // 1. Point型構造体の定義
  5. typedef struct {
  6. double x;
  7. double y;
  8. } Point;
  9.  
  10. // 2. 関数のプロトタイプ宣言
  11. Point scan_point(void);
  12. double area_of(Point p1, Point p2);
  13. double circumference_of(Point p1, Point p2);
  14.  
  15. // 3. main関数
  16. int main(void) {
  17. Point p1, p2;
  18. double area, circumference;
  19.  
  20. printf("左上隅と右下隅の座標を入力してください\n");
  21. p1 = scan_point();
  22. p2 = scan_point();
  23.  
  24. area = area_of(p1, p2);
  25. circumference = circumference_of(p1, p2);
  26.  
  27. printf("面積: %.2lf\n", area);
  28. printf("周辺の長さ: %.2lf\n", circumference);
  29.  
  30. return 0;
  31. }
  32.  
  33. // 4. 各関数の本体(中身)
  34.  
  35. // X座標とY座標を入力して、そのPoint構造体の変数を返す関数
  36. Point scan_point(void) {
  37. Point p;
  38. // 警告対策として、scanfの戻り値を判定(またはキャスト)しています
  39. if (scanf("%lf", &p.x)) {}
  40. if (scanf("%lf", &p.y)) {}
  41. return p; // セミコロン忘れを修正しました
  42. }
  43.  
  44. // 面積を計算して返す関数
  45. double area_of(Point p1, Point p2) {
  46. double width = fabs(p2.x - p1.x);
  47. double height = fabs(p2.y - p1.y);
  48. return width * height;
  49. }
  50.  
  51. // 周囲の長さを計算して返す関数
  52. double circumference_of(Point p1, Point p2) {
  53. double width = fabs(p2.x - p1.x);
  54. double height = fabs(p2.y - p1.y);
  55. return 2.0 * (width + height);
  56. }
  57.  
  58.  
Success #stdin #stdout 0s 5308KB
stdin
0
0
1
1
stdout
左上隅と右下隅の座標を入力してください
面積: 1.00
周辺の長さ: 4.00