fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. typedef struct {
  6. double x;
  7. double y;
  8. } Point;
  9.  
  10. Point scan_point(void);
  11. double area_of(Point p1, Point p2);
  12. double circumference_of(Point p1, Point p2);
  13.  
  14. int main(void) {
  15. Point p1 = scan_point();
  16. Point p2 = scan_point();
  17. double area = area_of(p1, p2);
  18. double circ = circumference_of(p1, p2);
  19. printf("座標1 (%.2f, %.2f)\n", p1.x, p1.y);
  20. printf("座標2 (%.2f, %.2f)\n", p2.x, p2.y);
  21. printf("面積: %.2f\n", area);
  22. printf("周囲の長さ: %.2f\n", circ);
  23.  
  24. return 0;
  25. }
  26.  
  27. Point scan_point(void) {
  28. Point p;
  29. printf("座標を入力してください (X Y): ");
  30. if (scanf("%lf %lf", &p.x, &p.y) != 2) {
  31. fprintf(stderr, "入力が正しくありません。0,0 を返します。\n");
  32. p.x = p.y = 0.0;
  33. }
  34. return p;
  35. }
  36.  
  37. double area_of(Point p1, Point p2) {
  38. double width = fabs(p2.x - p1.x);
  39. double height = fabs(p2.y - p1.y);
  40. return width * height;
  41. }
  42.  
  43. double circumference_of(Point p1, Point p2) {
  44. double width = fabs(p2.x - p1.x);
  45. double height = fabs(p2.y - p1.y);
  46. return 2.0 * (width + height);
  47. }
  48.  
Success #stdin #stdout #stderr 0s 5320KB
stdin
Standard input is empty
stdout
座標を入力してください (X Y): 座標を入力してください (X Y): 座標1 (0.00, 0.00)
座標2 (0.00, 0.00)
面積: 0.00
周囲の長さ: 0.00
stderr
入力が正しくありません。0,0 を返します。
入力が正しくありません。0,0 を返します。