fork(2) download
  1. #include <stdio.h>
  2. #include <math.h> // fabs() を使うために必要
  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.  
  18. // 注意: プログラムはここで入力を待ちます。プロンプト行は出力しませんが、
  19. // 入力後に座標を "(%.2f, %.2f)" の形式で1回だけ表示します。
  20. p1 = scan_point(1);
  21. p2 = scan_point(2);
  22.  
  23. double area = area_of(p1, p2);
  24. double circum = circumference_of(p1, p2);
  25.  
  26. printf("面積:%.2f\n", area);
  27. printf("周囲の長さ:%.2f\n", circum);
  28.  
  29. return 0;
  30. }
  31.  
  32. Point scan_point(int n) {
  33. Point p;
  34. // ユーザに分かるように簡単に入力形式だけ示す(任意)
  35. printf("座標%d を入力してください(例: 0 0):\n", n);
  36. if (scanf("%lf %lf", &p.x, &p.y) != 2) {
  37. // 入力エラー時は0に初期化して戻す(簡易エラーハンドリング)
  38. p.x = p.y = 0.0;
  39. }
  40. // 読み取った座標を小数2桁で一度だけ表示する
  41. printf("座標%d (%.2f, %.2f)\n", n, p.x, p.y);
  42. return p;
  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. double circumference_of(Point p1, Point p2) {
  52. double width = fabs(p2.x - p1.x);
  53. double height = fabs(p2.y - p1.y);
  54. return 2.0 * (width + height);
  55. }
  56.  
Success #stdin #stdout 0s 5300KB
stdin
0 0
3 4
stdout
左上隅と右下隅の座標を入力してください。
座標1 を入力してください(例: 0 0):
座標1 (0.00, 0.00)
座標2 を入力してください(例: 0 0):
座標2 (3.00, 4.00)
面積:12.00
周囲の長さ:14.00