fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h> // fabs を使うために必要
  4.  
  5. typedef struct {
  6. double x;
  7. double y;
  8. } Point;
  9.  
  10. /* 関数プロトタイプ */
  11. Point scan_point(void);
  12. double area_of(Point p1, Point p2);
  13. double circumference_of(Point p1, Point p2);
  14.  
  15. int main(void) {
  16. printf("左上隅と右下隅の座標を入力してください。\n");
  17.  
  18. printf("左上隅の座標:\n");
  19. Point p1 = scan_point();
  20.  
  21. printf("右下隅の座標:\n");
  22. Point p2 = scan_point();
  23.  
  24. double area = area_of(p1, p2);
  25. double circ = circumference_of(p1, p2);
  26.  
  27. printf("座標1 (%.2f, %.2f)\n", p1.x, p1.y);
  28. printf("座標2 (%.2f, %.2f)\n", p2.x, p2.y);
  29. printf("面積: %.2f\n", area);
  30. printf("周囲の長さ: %.2f\n", circ);
  31.  
  32. return 0;
  33. }
  34.  
  35. Point scan_point(void) {
  36. Point p;
  37. printf("座標を入力してください (X Y): ");
  38. if (scanf("%lf %lf", &p.x, &p.y) != 2) {
  39. fprintf(stderr, "入力が正しくありません。0,0 を返します。\n");
  40. p.x = p.y = 0.0;
  41. }
  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. }
Success #stdin #stdout #stderr 0s 5324KB
stdin
1 1
stdout
左上隅と右下隅の座標を入力してください。
左上隅の座標:
座標を入力してください (X Y): 右下隅の座標:
座標を入力してください (X Y): 座標1 (1.00, 1.00)
座標2 (0.00, 0.00)
面積: 1.00
周囲の長さ: 4.00
stderr
入力が正しくありません。0,0 を返します。